[英]how to use android broadcast receiver
以下是服務,活動和廣播接收器的示例。 活動是對服務進行更改的設置。 廣播接收器收聽設置更改和更新服務。 學習者2學習者
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>
如果我正確理解您的代碼,則該塊
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
具有用於聲明package
的標記<manifest>
。
例如
<manifest package="com.hello.world" ...
當您在清單中聲明接收方時, .
像.xReceiver
那樣,意味着xReceiver.java
應該位於包com.hello.world
。
如果您使用的是其他軟件包,請指定全名或相對於<manifest
聲明的package
更多信息在這里
您以錯誤的方式發送廣播。 您應該按以下方式發送:
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.