我service
实施了LocationListener
。我从我的活动开始提供这项服务。应用程序工作正常,但当我禁用数据连接或wifi
我的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;
}
}
我的广播接收器
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
当我禁用互联网或wifi时,我会在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...
答案 0 :(得分:1)
我发现了我的错误......那是因为我在广播接收器中有这个代码..
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();
}
}
所以当我在上面的代码中添加Log.i
并在禁用和启用logcat
后检查了我的wifi
时,我发现了错误。