Search code examples
androidgpsbroadcastreceiverbootcompleted

Request location updates from BOOT_COMPLETED Receiver?


I need to get location updates form onReceive method. When the the onReceive() called the GPS start for 2-3 second and gone, why??

HOW to fix it?? i want to get location updates. help please.

NOTE: onReceive method is called when restarting my phone

The java code:

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>

Solution

  • When the the onReceive() called the GPS start for 2-3 second and gone, why?

    Because your process was terminated.