gpt4 book ai didi

java - 应用程序外的 Android 互联网连接检查器

转载 作者:行者123 更新时间:2023-11-29 19:58:04 24 4
gpt4 key购买 nike

我的应用程序需要在连接互联网时执行某些操作,反之亦然。但是,我需要在我的应用程序外部 执行此操作。这里有很多关于 stackoverflow 的想法和帮助,但它总是WITHIN 应用程序。我知道 AlarmManager 在应用程序之外工作,但如果我尝试每 5 分钟检查一次是否有互联网连接,它只会耗尽电池电量。我想要的是检查应用程序外部的连接以下载一些东西。

我还发现 Intent Service 也可以在应用程序之外工作。我试图将其放入 Wakeful Broadcast Receiver 中。但是,它仍然没有被调用。

有人能帮帮我吗?谢谢!

这是我的唤醒广播接收器

public class ConnectivityOutsideAppReceiver extends WakefulBroadcastReceiver {

private static final String LOG_TAG = ConnectivityOutsideAppReceiver.class.getSimpleName();

private ConnectivityManager connectivityManager;
private static boolean connection = false;

@Override
public void onReceive(Context context, Intent intent){

ComponentName comp = new ComponentName(context.getPackageName(),
ConnectivityOutsideAppService.class.getName());

// Start the service, keeping the device awake while it is
// launching.
startWakefulService(context, (intent.setComponent(comp)

));
}

}

这是 Intent 服务

公共(public)类 ConnectivityOutsideAppService 扩展 IntentService {

private static final String LOG_TAG = ConnectivityOutsideAppService.class.getSimpleName();
private ConnectivityManager connectivityManager;
private static boolean connection = false;
private Context context;

public ConnectivityOutsideAppService() {
super(ConnectivityOutsideAppService.class.getSimpleName());
this.context = context;
}

@Override
protected void onHandleIntent(Intent intent) {
connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
checkConnectionOnDemand();

Log.e(LOG_TAG, " ## onHandleIntent##");
if (connection == true
&& intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
false)) {
Log.e(LOG_TAG, " ## connection == true ##");
connection = false;
} else if (connection == false
&& !intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
false)) {
Log.e(LOG_TAG, " ## connection == false ##");
connection = true;
}

ConnectivityOutsideAppReceiver.completeWakefulIntent(intent);
}

public static boolean hasConnection() {
return connection;
}

private void checkConnectionOnDemand() {
Log.e(LOG_TAG, " ## checkConnectionOnDemand ##");
final NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info == null || info.getState() != NetworkInfo.State.CONNECTED) {
if (connection == true) {
Log.e(LOG_TAG, " ## connection == true ##");
connection = false;
}
} else {
if (connection == false) {
Log.e(LOG_TAG, " ## connection == false ##");
connection = true;
}
}
}
}

这是我的 Android list

    <uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<service
android:name="com.timetrackermobilelog.BusinessServices.ConnectivityOutsideAppService"
android:exported="false"/>

<receiver android:name="com.timetrackermobilelog.Utilities.ConnectivityOutsideAppService"
android:enabled="true"
android:process=":remote">

<intent-filter android:priority="1000" >
<action android:name="com.timetrackermobilelog.Utilities.ConnectivityOutsideAppService" />
<category android:name="com.Utilities" />
</intent-filter>
</receiver>

我已经尝试在 Activity 中注册接收器,但效果不佳。

IntentFilter intentFilter = new IntentFilter("com.pointwest.timetrackermobilelog.Utilities.ConnectivityOutsideAppReceiver");
ConnectivityOutsideAppReceiver connectivityOutsideAppReceiver = new ConnectivityOutsideAppReceiver();
registerReceiver(connectivityOutsideAppReceiver, intentFilter);

有什么我遗漏的吗?

最佳答案

感谢@Mike M. 只需修改几行即可给出答案。

在 Android Manifest 中,将其更改为:

<intent-filter android:priority="1000" >
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>

还有Service的onHandleIntent,改成:

connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

关于java - 应用程序外的 Android 互联网连接检查器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36539711/

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