0

这是我的代码的过滤器:

发件人:

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>

任何人都可以帮助我吗?非常感谢!

4

1 回答 1

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
      }

      ...
    }
于 2013-11-16T05:28:20.037 回答