Reputation: 524
I have service
that implements LocationListener
. I m starting that service from my activity. App works fine but when I disable data connection or wifi
my service
stops and when I enable it, service starts.
I want to know why does this happen? Please help.
Here is my service
public class MyService extends Service implements LocationListener {
LocationManager LocationMngr;
@Override
public void onCreate() {
super.onCreate();
Log.i("PML", "MyService Started...");
Toast.makeText(getApplicationContext(),
"In Service's onCreate Method" ,Toast.LENGTH_LONG).show();
startLocationListener();
}
private void startLocationListener() {
this.LocationMngr = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
this.LocationMngr.requestLocationUpdates
(LocationManager.NETWORK_PROVIDER, 2*60*1000, 0, this);
}
public void onLocationChanged(Location location) {
//saving location
}
public void onProviderDisabled(String provider) { }
public void onProviderEnabled(String provider) { }
public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override
public void onDestroy() {
super.onDestroy();
this.LocationMngr.removeUpdates(this);
Log.i("PML","MyService Stopped...");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
my broadcast receiver
public class BootReceiver extends BroadcastReceiver {
final String ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
@Override
public void onReceive(Context context, Intent intent) {
AndroidServiceManager manager = new AndroidServiceManager(context);
//This intent is received when android device restarts
if(Intent.ACTION_BOOT_COMPLETED.equalsIgnoreCase(intent.getAction())) {
Toast.makeText(context,"Device Rebooted",Toast.LENGTH_LONG).show();
if(!manager.isMyServiceRunning(context)) {
manager.startMyService();
}
}
//This intent is received when android device shuts down
if(Intent.ACTION_SHUTDOWN.equalsIgnoreCase(intent.getAction())) {
//some database operation
}
//This intent is received when airoplane mode changes
if(Intent.ACTION_AIRPLANE_MODE_CHANGED
.equalsIgnoreCase(intent.getAction().intern())) {
//some database operation
}
if(ACTION.equalsIgnoreCase(intent.getAction())) {
boolean network = manager.isNetworkAvailable(context);
if (network) {
if(!manager.isMyServiceRunning(context))
manager.startMyService();
} else {
manager.stopMyService();
}
}
}
}
My manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xyz.ui"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/sample"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<activity
android:name="com.xyz.UserActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.xyz.MyService"
android:enabled="true"
android:exported="false" />
<receiver
android:name="com.xyz.BootReceiver">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
<action android:name="android.intent.action.AIRPLANE_MODE"/>
</intent-filter>
</receiver>
</application>
And here is the logcat
when I disable internet or wifi I get following entry in logcat
05-01 17:23:24.484: I/PML(27308): MyService Stopped...
when I enable internet or wifi I get following entry in logcat
05-01 17:23:58.296: I/PML(27308): MyService Started...
Upvotes: 2
Views: 1011
Reputation: 524
I found out my mistake.. it was because I have this code in my broadcast receiver..
if(ACTION.equalsIgnoreCase(intent.getAction())) {
boolean network = manager.isNetworkAvailable(context);
if (network) {
Log.i("PML", "Start service if not running");
if(!manager.isMyServiceRunning(context))
manager.startMyService();
} else {
Log.i("PML", "Stop service");
manager.stopMyService();
}
}
so when I added Log.i
in above code and checked my logcat
after disabling and enabling wifi
, I found mistake.
Upvotes: 1