这是我的代码的过滤器:
发件人:
public static String ACTION = "some_broadcast";
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Creating an Intent to be sent through a broadcast
Intent intent = new Intent(ACTION);
intent.putExtra("something", 1);
sendBroadcast(intent);
}
});
}
}
在接收端(因为它是 systemui 类之一,我只能在现有代码中添加行而不真正改变其结构):
public class PhoneStatusBarPolicy {
...
private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("some_broadcast")) {
update(intent);
}
else if ...
}
};
public PhoneStatusBarPolicy(Context context) {
...
IntentFilter filter = new IntentFilter();
filter.addAction("some_broadcast");
context.registerReceiver(mIntentReceiver, filter, null, mHandler);
...
}
private final void update (Intent intent) {
//Does something
}
...
}
即使我读到一旦我注册了接收器,就不需要将它添加到清单中,我已经将它添加到他的 systemui 中,AndroidManifest.xml
因为它从未到达它:
<receiver
android:name="com.android.systemui.statusbar.phone.PhoneStatusBarPolicy"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="some_broadcast" />
</intent-filter>
</receiver>
在模拟器上运行应用程序后,正在调用函数“更新”并执行应有的操作,但我收到“不幸的是系统 UI 已停止”</p>
任何人都可以帮助我吗?非常感谢!