繁体   English   中英

应用程式外的Android Internet连接检查器

[英]Android Internet Connection checker OUTSIDE the app

每当连接互联网时,我的应用程序都需要执行某些操作,反之亦然。 不过,我需要做的这超出了申请。 有很多的想法,并帮助这里计算器,但它总是的应用程序。 我知道AlarmManager在应用程序外部运行,但是如果我每隔5分钟检查一次Internet连接,那只会耗尽电池电量。 我想要的是检查应用程序外部的连接以下载某些内容。

我还发现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)

        ));
    }

}

这是意向服务

公共类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清单

    <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清单中,将其更改为:

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

然后将该服务的onHandleIntent更改为:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM