我需要獲取位置更新形式onReceive方法。當onReceive()調用GPS開始2-3秒後,爲什麼?從BOOT_COMPLETED Receiver請求位置更新?
如何解決?我想獲取位置更新。請幫助。
注:的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
}
}
的Manifest.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>
,因爲廣播接收器是短暫的。改爲使用服務。 – njzk2
thanx bro。這真的很有用:) – Yousif