我是初学者,我非常想要了解广播接收器的工作原理。我已经建立了一个不起作用的例子,但我无法想象为什么。
我的用例:
当活动“TestApp”启动时,它必须激活广播接收器“Receiver”,这个启动另一个活动“Main”,它在同一个清单中定义。
这是我的清单xml
中的接收者定义<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也不会启动。
答案 0 :(得分:4)
没有测试过这个,但是接收器应该是应用节点的子节点吗?
<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>
答案 1 :(得分:2)
首先,您应该(取消)在onStart / onStop注册接收器:
@Override
public void onStart() {
super.onStart();
registerReceiver(mBroadcastReceiver, new IntentFilter("your name"));
}
然后你可以使用PeddingIntent
调用Activity Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
PS,您可以使用Log
代替System.out.println.
答案 2 :(得分: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);
}