广播接收器不工作。在清单中声明

Broadcast Reciever not working. Declared in manifest

我有一个 BroadcastReciever 和一个服务。建立 Internet 连接后,它必须启动下载一些数据的服务。

我的广播接收器

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"/>

连一个动作都没有被触发。我不知道我在哪里提交 mistake.I 也搜索了答案,但我的问题没有解决。 BroadcastReceiver 在我动态地在 activity 中注册时工作。我需要它在后台。请有人帮忙。

您应该删除:

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>

截至 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>

这将在检测到网络变化时随时通知服务。

现在您需要从任何 activity 启动服务,我相信这将是您的主要 activity,因为您希望服务在后台 运行。这个例子只会在主 activity 按照我的例子启动时触发。您可以根据自己的喜好进行调整。

主要activity:

public class MainActivity {

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //this is where you start the service and it will start listening for connectivity changes 
        if (getIntent().getBooleanExtra(EXTRA_TRIGGER_SYNC_FLAG, true)) {
            startService(SyncService.getStartIntent(this));
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}

这只是一个可以派上用场的 NetworkUtil class:

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() 中的这两行代码触发。根据您的需要调整此示例。我使用了 https://github.com/ribot/android-boilerplate/ 处的 ribot 样板代码示例中的部分示例。