以下是我的代码的过滤器:
发信人:
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>
在模拟器上运行应用程序后,正在调用函数'update'并执行它应该执行的操作,但我收到了'不幸的是系统UI已停止“
有人能帮帮我吗? 非常感谢!
答案 0 :(得分:0)
您无需两次注册接收器。这可能是停止UI的原因。您可以使用以下代码。它对我有用。
您的清单文件和发件人似乎没问题。我会把代码放到接收器上。
public class PhoneStatusBarPolicy extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("some_broadcast")) {
update(intent);
}
else if ...
}
};
private final void update (Intent intent) {
//Does something
}
...
}