我是刚接触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也不会启动。
你是对的谢谢你 – bline 2011-03-26 20:09:16