我需要获取位置更新表单 onReceive 方法。当 onReceive() 调用 GPS 启动 2-3 秒后消失了,为什么?
如何解决?我想获取位置更新。请帮忙。
注意:重启手机时会调用 onReceive 方法
的java代码:
public class BootReceiver extends BroadcastReceiver implements LocationListener {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()))
{
LocationManager LM2=(LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
LM2.requestLocationUpdates("gps",5000, 0, this);
}
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
清单.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<receiver android:name="com.my.package.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>