Android 广播接收器不工作。在舱单上声明

Android 广播接收器不工作。在舱单上声明,android,broadcastreceiver,intentfilter,Android,Broadcastreceiver,Intentfilter,我有一个广播接收器和一个服务。当互联网连接建立后,它必须启动下载一些数据的服务 我的广播接收机 public class NetworkStateListener extends BroadcastReceiver { @Override public void onReceive(Context c, Intent intent) { Toast.makeText(c,"started",Toast.LENGTH_SHORT).show(); c.startServi

我有一个广播接收器和一个服务。当互联网连接建立后,它必须启动下载一些数据的服务

我的广播接收机

public class NetworkStateListener extends BroadcastReceiver
{

@Override
public void onReceive(Context c, Intent intent)
    {
    Toast.makeText(c,"started",Toast.LENGTH_SHORT).show();
    c.startService(new Intent(c,DataDownloader.class));

    // TODO: Implement this method
    }

 }
这是一些舱单代码

<receiver android:name="com.Example.SGSN.worker.NetworkStateListener" 
        android:enabled="true">
        <intent-filter android:priority="999">
            <action android:name="android.net.wifi.WifiManager.NETWORK_STATE_CHANGED_ACTION"/>
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
            <action android:name="android.net.wifi.WifiManager.SUPLICANT_STATE_CHANGED_ACTION"/>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
        </intent-filter>
    </receiver>

和请求的权限

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

甚至没有一个动作被触发。我不知道我在哪里犯了错误。我也在寻找答案,但我的问题没有解决。 当我在活动中动态注册BroadcastReceiver时,它就会工作。我需要背景资料。请有人帮忙。

您应该删除:

android:process=“:channel”

UPD

此处为100%有效的舱单代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.andrey.test">

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

    <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:label="@string/title_activity_main2">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".MyReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.net.wifi.WifiManager.NETWORK_STATE_CHANGED_ACTION" />
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
                <action android:name="android.net.wifi.WifiManager.SUPLICANT_STATE_CHANGED_ACTION" />
                <action android:name="android.net.conn.CONNECTIVITY_ACTION" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

您应该删除:

android:process=“:channel”

UPD

此处为100%有效的舱单代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.andrey.test">

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

    <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:label="@string/title_activity_main2">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".MyReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.net.wifi.WifiManager.NETWORK_STATE_CHANGED_ACTION" />
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
                <action android:name="android.net.wifi.WifiManager.SUPLICANT_STATE_CHANGED_ACTION" />
                <action android:name="android.net.conn.CONNECTIVITY_ACTION" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

在清单中,您是否在应用程序标记内设置了接收器标记

比如:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver android:name=".NetworkChangeReceiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
        </intent-filter>
    </receiver>
</application>

在清单中,您是否在应用程序标记内设置了接收器标记

比如:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver android:name=".NetworkChangeReceiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
        </intent-filter>
    </receiver>
</application>

从Android Nougat(API 24)开始。清单中声明的广播接收器将不再收到“android.net.conn.CONNECTIVITY_CHANGE”事件的警报。帮助节省电池:)。Android的旧版本不受影响

要在牛轧糖中收听此事件,您必须以编程方式注册广播

从安卓牛轧糖(API 24)开始。清单中声明的广播接收器将不再收到“android.net.conn.CONNECTIVITY_CHANGE”事件的警报。帮助节省电池:)。Android的旧版本不受影响


要在牛轧糖中收听此事件,您必须以编程方式注册广播

试试这样的实现:

首先,您的服务将监听互联网连接的变化:

public class SyncService extends Service {

public static Intent getStartIntent(Context context) {
    return new Intent(context, SyncService.class);
}


@Override
public void onCreate() {
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, final int startId) {
    Timber.i("Starting sync...");

    if (!NetworkUtil.isNetworkConnected(this)) {
        Timber.i("Sync canceled, connection not available");
        stopSelf(startId);
        return START_NOT_STICKY;
    }

    //Here you can define what you need to do when internet is available
    //Can download from internet and sync to local for example

    return START_STICKY;
}

@Override
public void onDestroy() {
    if (mSubscription != null) mSubscription.unsubscribe();
    super.onDestroy();
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

public static class SyncOnConnectionAvailable extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)
                && NetworkUtil.isNetworkConnected(context)) {
            Timber.i("Connection is now available, triggering sync...");
            context.startService(getStartIntent(context));
        }
    }
}
广播接收器将启动该服务,然后根据您在其onStartCommand()中的指示进行操作

接下来在清单中声明服务,如下所示:

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

<service android:name=".SyncService"/>

<receiver
    android:name=".SyncService$SyncOnConnectionAvailable"
    android:enabled="false">
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>
这只是一个可能派上用场的NetworkUtil类:

public class NetworkUtil {

    public static boolean isNetworkConnected(Context context) {
        ConnectivityManager cm =
            (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
    }

}

该服务将从MainActivity onCreate()中的这两行触发。根据您的需要调整此示例。我使用了在中找到的ribot样板代码示例中的部分示例

试试这样的实现:

首先,您的服务将监听互联网连接的变化:

public class SyncService extends Service {

public static Intent getStartIntent(Context context) {
    return new Intent(context, SyncService.class);
}


@Override
public void onCreate() {
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, final int startId) {
    Timber.i("Starting sync...");

    if (!NetworkUtil.isNetworkConnected(this)) {
        Timber.i("Sync canceled, connection not available");
        stopSelf(startId);
        return START_NOT_STICKY;
    }

    //Here you can define what you need to do when internet is available
    //Can download from internet and sync to local for example

    return START_STICKY;
}

@Override
public void onDestroy() {
    if (mSubscription != null) mSubscription.unsubscribe();
    super.onDestroy();
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

public static class SyncOnConnectionAvailable extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)
                && NetworkUtil.isNetworkConnected(context)) {
            Timber.i("Connection is now available, triggering sync...");
            context.startService(getStartIntent(context));
        }
    }
}
广播接收器将启动该服务,然后根据您在其onStartCommand()中的指示进行操作

接下来在清单中声明服务,如下所示:

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

<service android:name=".SyncService"/>

<receiver
    android:name=".SyncService$SyncOnConnectionAvailable"
    android:enabled="false">
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>
这只是一个可能派上用场的NetworkUtil类:

public class NetworkUtil {

    public static boolean isNetworkConnected(Context context) {
        ConnectivityManager cm =
            (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
    }

}


该服务将从MainActivity onCreate()中的这两行触发。根据您的需要调整此示例。我使用了在中找到的ribot样板代码示例中的部分示例

你为什么使用这个-
android:process=“:channel”
?我不知道具体是什么,我在一个教程视频中看到的。你在receiver=>receiver中有一个输入错误。你在主活动中启动服务了吗?不,在广播接收器中你为什么使用这个-
android:process=“:channel”
?我不知道确切的原因,我在一个教程视频中看到了它。你在receiver=>ReceiverYou在主要活动中启动服务吗?不,在BroadcastReceiverremoved android:process=“:channel”中有一个输入错误,但结果没有改善:-(我已经运行了该代码,它正在工作。我将复制整个清单。但在这里它没有显示任何Toast通知,服务也没有运行。请检查
android:name=“SOMETHING”
-引用您的广播接收器。检查包名和路径。两者都已正确删除android:process=“:channel”但结果没有改善:-(我已经运行了该代码,它正在工作。我将复制整个清单。但在这里,它没有显示任何Toast通知,服务也没有运行。请检查
android:name=“SOMETHING”
-引用您的广播接收器。检查了包名和路径。两者都正确是的,但这里只复制了重要的代码行,而这里只复制了重要的代码行