2011-03-26 154 views
0

我是剛接觸android,急於嘗試瞭解廣播接收機是如何工作的。我已經建立了一個不起作用的例子,但我無法想象爲什麼。廣播接收機android

我的用例:

當活動「TestApp」啓動時,它具有激活廣播接收器「接收器」,這一個啓動另一個活動「主」,這是在相同的清單中定義。

這裏是我的清單XML接收difinition

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".Main" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="stas.test.intent.action.blablub"/> 
      <category android:name="android.intent.category.DEFAULT"/> 
     </intent-filter> 
     <receiver android:name=".Receiver" 
     android:enabled="true"> 
      <intent-filter> 
       <action android:name="stas.test.intent.action.myreceiver"/> 
      </intent-filter> 
     </receiver> 
    </activity> 

</application> 

這是由接收器啓動的活動:

action android:name="stas.test.intent.action.blablub" (Main.java) 

這裏的接收器的代碼

public class Receiver extends BroadcastReceiver{ 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Intent newIntent = new Intent(); 
     newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     newIntent.setAction("stas.test.intent.action.blablub"); 
     newIntent.addCategory("android.intent.category.DEFAULT"); 
     System.out.println("dd"); 
     context.startActivity(newIntent); 
    } 
    } 

的這裏的調用接收器的啓動活動

public class TestApp extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Intent intent = new Intent(); 
     intent.setAction("stas.test.intent.action.myreceiver"); 
     getApplicationContext().sendBroadcast(intent); 
    } 
} 

當我啓動TestApp時,Receiver將永遠不會啓動,Main也不會啓動。

回答

3

沒有測試過這個,但不是接收器應該是應用程序節點的子節點嗎?

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".Main" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="stas.test.intent.action.blablub"/> 
      <category android:name="android.intent.category.DEFAULT"/> 
     </intent-filter> 
    </activity> 
    <receiver android:name=".Receiver" 
     android:enabled="true"> 
      <intent-filter> 
       <action android:name="stas.test.intent.action.myreceiver"/> 
      </intent-filter> 
    </receiver> 

</application> 
+0

你是對的謝謝你 – bline 2011-03-26 20:09:16

2

首先,你應該(UN)在在onStart /的onStop登記接收:

@Override 
    public void onStart() { 
     super.onStart(); 

     registerReceiver(mBroadcastReceiver,    new IntentFilter("your name")); 
    } 

然後ü可以用PeddingIntent

呼叫活動
Intent intent = new Intent(context, MainActivity.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

PS,你可以使用Log代替System.out.println.

0
It is better to register and unregister broadcast receiver on activity resume and pause. 
@Override 
    protected void onResume() { 
     // TODO Auto-generated method stub 
     super.onPause(); 
     this.registerReceiver(this.mReceiver); 
    } 
@Override 
    protected void onPause() { 
     // TODO Auto-generated method stub 
     super.onPause(); 
     //unregister our receiver 
     this.unregisterReceiver(this.mReceiver); 
    }