gpt4 book ai didi

android - 当我禁用数据连接或 wifi 时服务停止

转载 作者:行者123 更新时间:2023-11-29 01:54:00 26 4
gpt4 key购买 nike

我有实现LocationListener服务。我从我的 Activity 开始这项服务。应用程序工作正常,但是当我禁用数据连接或 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();
}
}
}
}

我的 list

<?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...

最佳答案

我发现了我的错误..这是因为我的广播接收器中有这段代码..

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 并在禁用和启用 wifi 后检查我的 logcat 时,我发现了错误。

关于android - 当我禁用数据连接或 wifi 时服务停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16317902/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com