简体   繁体   English

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

[英]Android Internet Connection checker OUTSIDE the app

My app needs to do something whenever the internet is connected and vice-versa. 每当连接互联网时,我的应用程序都需要执行某些操作,反之亦然。 However, I need to do this OUTSIDE my application. 不过,我需要做的这超出了申请。 There are a lot of ideas and help here on stackoverflow but it is always WITHIN the application. 有很多的想法,并帮助这里计算器,但它总是的应用程序。 I know that the AlarmManager works outside the application but it would just be draining the battery if I try to check if there's internet connection every 5 minutes. 我知道AlarmManager在应用程序外部运行,但是如果我每隔5分钟检查一次Internet连接,那只会耗尽电池电量。 What I wanted is to check the connection outside the app to download something. 我想要的是检查应用程序外部的连接以下载某些内容。

I also found out that Intent Service can work outside the application as well. 我还发现Intent Service也可以在应用程序外部运行。 I have tried to put this inside the Wakeful Broadcast Receiver. 我试图将其放入Wakeful Broadcast Receiver中。 However, it is still not being called. 但是,它仍然没有被调用。

Can someone out there help me? 有人可以帮我吗? Thanks! 谢谢!

Here's my 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)

        ));
    }

}

This is the Intent Service 这是意向服务

public class ConnectivityOutsideAppService extends IntentService { 公共类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;
        }
    }
}
}

This is my Android Manifest 这是我的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>

I have tried registering the receiver inside the Activity but it doesn't work as well. 我已经尝试在Activity中注册接收者,但效果不佳。

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

Is there anything that I missed? 有什么我想念的吗?

Thanks to @Mike M. in giving the answer with just a few lines of changes. 感谢@Mike M.做出了几处更改,就给出了答案。

In Android Manifest, change it to : 在Android清单中,将其更改为:

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

And onHandleIntent of the Service, change it to : 然后将该服务的onHandleIntent更改为:

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

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

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