如何使用android广播接收器

如何使用android广播接收器,android,android-intent,broadcastreceiver,Android,Android Intent,Broadcastreceiver,下面是服务、活动和广播接收器的示例。活动是对服务进行更改的设置。广播接收器侦听设置中的更改并更新服务。学习者 public class xService extends Service { @Override public void onCreate() { super.onCreate(); } @Override public IBinder onBind(Intent intent) { return mBinde

下面是服务、活动和广播接收器的示例。活动是对服务进行更改的设置。广播接收器侦听设置中的更改并更新服务。学习者

public class xService extends Service {

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

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

    public static void setEnableNotification(int command) {
        Log.i("EnableNotification","Updated");
        if (command == 0)
            enableNotification = true;
        ...
    }
}
以下方法是发送广播的活动的一部分:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ....
    IntentFilter filter = new IntentFilter();
        filter.addAction(NotifyServiceReceiver.ACTION);       
    registerReceiver(receiver,filter);
}   
final String ACTION="broadcast_settings";
public void onClick(View view) {
    Intent intent = new Intent(ACTION);

    switch (view.getId()) {
    case R.id.switchNotification:
        if(switchNotification.isChecked()==true)
        {       
            intent.putExtra("EnableNotification", 0);
            Log.i("Security365","Notification is enabled.");
        }
        else if ....
        sendBroadcast(intent);
        break;
    }
下面的部分是我的广播接收器:

public class xReceiver extends BroadcastReceiver {

    final String ACTION="broadcast_settings";

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(ACTION)){

            int enableNotification = intent.getIntExtra("EnableNotification", 0);

            if (enableNotification == 0)
                Serurity365Service.setEnableNotification(0);
        ...
        }
    }
}
主要节日

    <receiver android:name=".xReceiver" android:enabled="true"></receiver>

您以错误的方式发送广播。您应按以下方式发送:

    public void onClick(View view) {
        Intent intent = new Intent("your_action");

        switch (view.getId()) {
        case R.id.switchNotification:
            if(switchNotification.isChecked()==true)
            {       
                intent.putExtra("EnableNotification", 1);
                Log.i("Security365","Notification is enabled.");
            }
            else if ....
            sendBroadcast(intent);
            break;
        }
}
并将其作为:

 public class xReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

     if(intent.getACtion().equals("your_action"){
        int enableNotification = intent
                .getIntExtra("EnableNotification", 1);

        if (enableNotification == 0)
            Serurity365Service.setEnableNotification(0);
        ...
   }
} 
}
还要更新Androidmanifest.xml:

<receiver android:name=".xReceiver" android:enabled="true">
 <intent-filter>
   <action android:name="your_action" />
 </intent-filter>
</receiver>

如果我正确理解了您的代码,块

 if(switchNotification.isChecked()==true)
    {       
        intent.putExtra("EnableNotification", 1);
        Log.i("Security365","Notification is enabled.");
    }
正在将
sendBroadcast
中使用的意图中的
EnableNotification
设置为
1

在您的广播接收器中,您有

 int enableNotification = intent
            .getIntExtra("EnableNotification", 1);

    if (enableNotification == 0)
        Serurity365Service.setEnableNotification(0);
因此,它表示要检索额外的
EnableNotification
,如果没有值,则返回默认值
1
,然后您永远不会输入
if(EnableNotification==0)
语句

为确保广播接收器正常工作,请在
onReceive
方法中的接收器开头添加日志语句

编辑

另外,
AndroidMANIFEST.xml
有一个标签
,它声明了一个

比如说


谢谢。但是,“你的行动”是什么?您能举个例子吗?这是您在广播中发送的字符串,用于区分不同的操作。哦,是的,你还需要更新清单中的接收者。再次检查我的答案。我已经更新了它,还有另一条语句用于if(enableNotification==1)您已经在if(intent.getAction().equals(ACTION)){
之前添加了Log.i,对吗?