Java 关闭应用程序后保持连接广播接收器运行
当我需要使用互联网连接时,我想设置一个广播接收器,如果应用程序关闭,它将继续工作 我正在为此使用一项服务,但它不起作用。当应用程序在屏幕上或暂停时,它可以正常工作,但如果我关闭它,它将停止工作。我不太确定我是否应该使用其他东西,或者我是否做错了什么 这是我的代码:Java 关闭应用程序后保持连接广播接收器运行,java,android,Java,Android,当我需要使用互联网连接时,我想设置一个广播接收器,如果应用程序关闭,它将继续工作 我正在为此使用一项服务,但它不起作用。当应用程序在屏幕上或暂停时,它可以正常工作,但如果我关闭它,它将停止工作。我不太确定我是否应该使用其他东西,或者我是否做错了什么 这是我的代码: package com.example.ana.exampleapp; import android.app.Service; import android.content.BroadcastReceiver; import and
package com.example.ana.exampleapp;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.util.Log;
public class TestsService extends Service {
private static BroadcastReceiver networkChangeReceiver;
private static String TAG = "Service";
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
Log.v(TAG, "Service created");
boolean keep = needTokeepWaiting()
if(!keep)
stopSelf();
}
@Override
public void onDestroy() {
if (networkChangeReceiver != null)
unregisterReceiver(networkChangeReceiver);
networkChangeReceiver = null;
Log.v(TAG, "Service destroyed");
}
private void registerNetworkChangeReceiver() {
networkChangeReceiver = new BroadcastReceiver() {
private String TAG = "NetworkReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "Connection changed received");
boolean keep = needTokeepWaiting()
if(!keep)
stopSelf();
}
};
IntentFilter filter = new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(networkChangeReceiver, filter);
}
}
还有我的男朋友:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ana.exampleapp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RegisterActivity"></activity>
<activity android:name=".FinishRegisterActivity"></activity>
<activity android:name=".TestActivity"></activity>
<activity android:name=".InformationActivity"></activity>
<service android:name=".TestsService"></service>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
我所做的与此处建议的类似,但不起作用:
我也曾尝试在没有服务和在清单中注册的情况下执行此操作,但也没有成功:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ana.exampleapp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RegisterActivity">
</activity>
<activity android:name=".FinishRegisterActivity" >
</activity>
<activity android:name=".TestActivity">
</activity>
<activity android:name=".InformationActivity" >
</activity>
<receiver android:name=".NetworkChangeReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
如果您不想在应用程序关闭时停止服务,您应该重写
onStartCommand()
方法,执行以下操作:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_REDELIVER_INTENT;
}
您可以返回,或者,任何对您更有效的方法我刚刚发现问题。Android 4.4.x中有一个bug,关闭应用程序时会杀死后台服务。我在安卓4.4.2上测试我的应用程序。这里有一个详细的解释:
因此,我的代码是正确的,正如我在Android 4.2.2中尝试过的那样,它是有效的(我尝试过重写
onStartCommand()
,所以这可能是需要的)。对于那个广播,您不需要一个持续运行的服务。您只需在清单中注册一个
“android.net.conn.CONNECTIVITY\u更改”
。我已经尝试过了,但当我关闭应用程序时,它停止工作。我已经添加了我在清单中注册的方式,也许我做错了什么。我看到你已经解决了服务的问题,但你真的不需要它一直运行。您的
已被禁用。将enabled
属性的值更改为true
,或者将其删除,并确保name
指向有效的BroadcastReceiver
类。你需要在安装后至少启动一次应用程序,使其脱离停止状态,但在此之后,只要连接发生变化,你的接收器就会启动。它不工作。我想当我关闭应用程序时,服务可能没有停止,只是广播接收器停止工作,尽管我不确定。我的意思是故意的应用程序关闭。用户在“最近的应用”屏幕上刷出应用后。您应该确定服务是否停止。尝试使用断点或查看日志猫,以检查是否调用了onDestroy()
,onDestroy()
。
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_REDELIVER_INTENT;
}