2
我有service
實施LocationListener
。我從我的活動開始提供這項服務。應用程序工作正常,但當我禁用數據連接或wifi
我的service
停止,當我啓用它,服務啓動。 我想知道爲什麼會發生這種情況?請幫忙。服務停止時,我禁用數據連接或WiFi
這裏是我的服務
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;
}
}
我的廣播接收器
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();
}
}
}
}
我的清單
<?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>
這裏是logcat
當我關閉網絡或無線網絡我獲得以下的logcat的入門
05-01 17:23:24.484: I/PML(27308): MyService Stopped...
當我使互聯網或WiFi我獲得以下的logcat的
05-01 17:23:58.296: I/PML(27308): MyService Started...
您在應用程序中使用過任何廣播和接收器。 – 2013-05-01 12:25:45
是我已經在我的應用程序中使用廣播接收器.. – 2013-05-01 12:28:47
好吧..向我們展示廣播接收器代碼.. – 2013-05-01 12:33:21