2012-02-08 20 views
0

以下は、サービス、アクティビティ、放送受信機の例です。アクティビティとは、サービスを変更する設定です。ブロードキャスト受信機は、設定および更新サービスの変更を待ち受ける。 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> 

答えて

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メソッドでログステートメントを追加します。

EDIT

またAndroidMANIFEST.xmlpackageを宣言するタグ<manifest>を持っています。

例えば

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

あなたがマニフェストにレシーバを宣言すると、.xReceiverのよう.xReceiver.javaがパッケージcom.hello.worldに置かなければならないことを意味します。

あなたが別のパッケージを持っている場合は、完全な名前を指定するか、または<manifest

詳細情報の中で宣言packageにrelatiive here

+0

Log.iがonReceiveに追加され、動作しません。 – Milan

+0

if(enableNotification == 1) – Milan

+0

'if(int.getAction()。equals(ACTION)){'の前にLog.iを追加しましたか? – ccheneson

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

+0

これは、さまざまなアクションを区別するためにブロードキャストで送信する文字列です。 http://thinkandroid.wordpress.com/2010/02/02/custom-intents-and-broadcasting-with-receivers/ – waqaslam

+0

私はまだそれを動作させることはありません..何かが不足している? – Milan

関連する問題