2012-02-08 116 views
0

下面是一個服務,活動和廣播接收機的例子。活動是對服務進行更改的設置。廣播接收機收聽設置中的更改並更新服務。 learner2learner如何使用android廣播接收機

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> 

回答

1

如果我正確理解你的代碼,該塊

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

正在設置EnableNotification在從sendBroadcast

用在你的廣播接收器的意圖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

如果您在不同的包有,指定全名或relatiive以聲明的package<manifest

更多信息here

+0

在onReceive中添加了Log.i,它不工作。如果(enableNotification == 1) – Milan 2012-02-08 15:51:29

+0

我還有另一種說法 – Milan 2012-02-08 15:53:43

+0

您已經在if(intent.getAction()。equals(ACTION)){'right? – ccheneson 2012-02-08 16:31:10

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> 
+0

謝謝。但是,什麼是「your_action」?你能舉個例子嗎? – Milan 2012-02-08 15:21:44

+0

這是您在廣播中發送的字符串,以區分不同的操作。 http://thinkandroid.wordpress.com/2010/02/02/custom-intents-and-broadcasting-with-receivers/ – waqaslam 2012-02-08 15:26:03

+0

我還沒有得到它的工作..是否有其他缺失? – Milan 2012-02-08 15:47:03