Note that there are some explanatory texts on larger screens.

plurals
  1. POService stops when I disable data connection or wifi
    text
    copied!<p>I have <code>service</code> that implements <code>LocationListener</code>. I m starting that service from my activity. App works fine but when I disable data connection or <code>wifi</code> my <code>service</code> stops and when I enable it, service starts. I want to know why does this happen? Please help.</p> <p>Here is my service </p> <pre><code>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; } } </code></pre> <p>my broadcast receiver</p> <pre><code>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(); } } } } </code></pre> <p>My manifest</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xyz.ui" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="10" android:targetSdkVersion="17" /&gt; &lt;uses-permission android:name="android.permission.INTERNET"/&gt; &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/&gt; &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt; &lt;uses-permission android:name="android.permission.READ_PHONE_STATE"/&gt; &lt;uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/&gt; &lt;application android:allowBackup="true" android:icon="@drawable/sample" android:label="@string/app_name" android:theme="@style/AppTheme" &gt; &lt;activity android:name="com.xyz.UserActivity" android:label="@string/app_name" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;service android:name="com.xyz.MyService" android:enabled="true" android:exported="false" /&gt; &lt;receiver android:name="com.xyz.BootReceiver"&gt; &lt;intent-filter &gt; &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt; &lt;action android:name="android.intent.action.ACTION_SHUTDOWN" /&gt; &lt;action android:name="android.intent.action.AIRPLANE_MODE"/&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; &lt;/application&gt; </code></pre> <p></p> <p>And here is the <code>logcat</code></p> <p>when I disable internet or wifi I get following entry in logcat</p> <pre><code>05-01 17:23:24.484: I/PML(27308): MyService Stopped... </code></pre> <p>when I enable internet or wifi I get following entry in logcat</p> <pre><code>05-01 17:23:58.296: I/PML(27308): MyService Started... </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload