【发布时间】:2014-07-18 11:57:58
【问题描述】:
我发布了我的需求,问题和代码,我不知道我的代码有什么问题,任何帮助..
我的要求:我想每 10 分钟从 GPS 获取纬度、经度
问题:我一直在获取纬度,经度
我的代码:
1].我在活动中使用此代码,我正在从活动中启动广播接收器
//Pending Intent
Intent in = new Intent(this,GPSReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, in, 0);
//Alaram manager
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
am.setRepeating(AlarmManager.RTC, cal.getTimeInMillis(), 10*60*1000, pi);
2].BroadCast 类“GPSReceiver.class”,从这里我开始服务名称为 GPSService
public class GPSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context,GPSService.class);
context.startService(service);
}
}
3].Service Class "GPSService",用于获取位置更新
public class GPSService extends Service {
@Override
public IBinder onBind(Intent arg0) {
return null;
}
//Flag to know GPS Status
boolean isGPSEnabled = false;
LocationManager locationmanager;
String locationgpsprovider = LocationManager.GPS_PROVIDER;
double latitude;
double longtitude;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//To get Location service
locationmanager = (LocationManager) getSystemService(LOCATION_SERVICE);
//To get GPS Status
isGPSEnabled = locationmanager.isProviderEnabled(locationgpsprovider);
if (!isGPSEnabled) {
Toast.makeText(this, "Please check your GPS connection", Toast.LENGTH_LONG).show();
}else {
locationmanager.requestLocationUpdates(locationgpsprovider, 0, 0, ll);
}
return START_NOT_STICKY;
}
LocationListener ll = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(Location location) {
Log.i("Hei", "10 minutes up");
latitude = location.getLatitude();
longtitude = location.getLongitude();
}
};
【问题讨论】:
-
您是否注意到 GPSReceiver 是否被调用?
-
感谢您的回复...没有调用,有什么原因吗?
-
嗨,广播只调用一次,有什么想法吗?
标签: android android-activity android-location