Java 应用程序外部的Android Internet连接检查器

Java 应用程序外部的Android Internet连接检查器,java,android,android-manifest,android-intentservice,android-broadcastreceiver,Java,Android,Android Manifest,Android Intentservice,Android Broadcastreceiver,无论何时连接互联网,我的应用程序都需要执行某些操作,反之亦然。但是,我需要在我的应用程序之外执行此操作。这里有很多关于stackoverflow的想法和帮助,但是它总是在应用程序中。我知道AlarmManager在应用程序之外工作,但如果我尝试每5分钟检查一次是否有互联网连接,它将耗尽电池电量。我想要的是检查应用程序外部的连接以下载一些东西 我还发现,Intent服务也可以在应用程序之外工作。我试着把它放在清醒的广播接收器里。然而,它仍然没有被调用 外面有人能帮我吗?谢谢 这是我的清醒广播接收器

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

我还发现,Intent服务也可以在应用程序之外工作。我试着把它放在清醒的广播接收器里。然而,它仍然没有被调用

外面有人能帮我吗?谢谢

这是我的清醒广播接收器

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)

        ));
    }

}
这是意向服务

公共类ConnectionYoutSideAppService扩展了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>

有什么我遗漏的吗?

感谢@Mike M.只做了几行修改就给出了答案

在Android清单中,将其更改为:

<intent-filter android:priority="1000" >
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
            </intent-filter>
connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

在清单中注册您的接收者,而不是自定义操作。您调试了代码吗?您应该在哪里遇到问题?您好@MikeM。我会找出问题并返回给您。@VishalHalani。我已经这样做了,没有错误。未调用ConnectionVitYoutSideAppService。@MikeM。谢谢!成功了!我会在这里写下答案。恭喜您欧点!
connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);