如何使用android广播接收器

时间:2012-02-08 15:09:19

标签: android android-intent broadcastreceiver

以下是服务,活动和广播接收器的示例。活动是对服务进行更改的设置。广播接收器侦听设置的更改并更新服务。 learner2learner

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);
        ...
        }
    }
}

Mainfest

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

2 个答案:

答案 0 :(得分:1)

如果我正确理解您的代码,那么

 if(switchNotification.isChecked()==true)
    {       
        intent.putExtra("EnableNotification", 1);
        Log.i("Security365","Notification is enabled.");
    }

EnableNotification

中使用的意图中将1设置为sendBroadcast

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

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

    if (enableNotification == 0)
        Serurity365Service.setEnableNotification(0);

所以它说要检索额外的EnableNotification,如果没有值,则返回默认值1,然后再输入if (enableNotification == 0)语句。

为确保您的广播接收器正常工作,请使用onReceive方法在接收器的开头添加日志声明。

修改

此外,AndroidMANIFEST.xml的标记<manifest>声明了package

例如

<manifest package="com.hello.world" ...

当您在清单中声明接收方时,.中的.xReceiver表示xReceiver.java应位于包com.hello.world中。

如果您使用其他套餐,请指定全名或与package

中声明的<manifest相关联

更多信息here

答案 1 :(得分:0)

您正以错误的方式发送广播。你应该发送如下:

    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>