2011-03-26 155 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); 
    }