When the the onReceive() called the GPS start for 2-3 second and gone, why?
Because your process was terminated.
Domanda
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>
Soluzione 2
When the the onReceive() called the GPS start for 2-3 second and gone, why?
Because your process was terminated.
Altri suggerimenti
Thanx guys for your help, all i needed was to make a Service
the onReceive method will be
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()))
{
Intent i= new Intent(context, MyService.class);
context.startService(i);
}
}
MyService class
public class MyService extends Service implements LocationListener {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//TODO do something useful
LocationManager LM2=(LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LM2.requestLocationUpdates("gps",5000, 0, this);
return Service.START_STICKY;
}
@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
}
}
and i need to register my service in the manifest.xml
<service
android:name="com.my.package.MyService"
android:icon="@drawable/icon"
android:label="Service name"
>
</service>