BroadcastReveiver没有注册

时间:2014-11-10 22:19:07

标签: android parse-platform push-notification broadcastreceiver

我无法弄清楚为什么我一直收到错误说:

11-10 17:14:04.904: E/AndroidRuntime(8151): Caused by: java.lang.IllegalArgumentException: Receiver not registered: com.refect.served.fragment.activities.DrinkReleaseStation$PushBroadcastReceiver@541e0ce4

我在onCreate()方法上清楚地注册了我的接收器。但是当我尝试取消注册它时,它会崩溃并给我错误。这是我的其余代码:

public class DrinkReleaseStation extends FragmentActivity {

    private PushBroadcastReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.admin_new_bar);

        String barId = getIntent().getExtras().getString("BarId");
        orders = new ArrayList<ParseObjectModel>();

        saveInstallation(barId);

        ordersListView = (ListView) findViewById(R.id.lv_bars);
        adapter = new DrinkManagerAdapter(this, orders);
        ordersListView.setAdapter(adapter);

        fab = (Button) findViewById(R.id.btn_add_new_bar);
        //fab.setDrawable(getResources().getDrawable(R.drawable.ic_navigation_accept));

        registerReceiver();

    } //end onCreate

@Override
public void onResume() {
    super.onResume();
    registerReceiver();
}

@Override
public void onDestroy() {
    super.onDestroy();
    unregisterReceiver(receiver);
    unsubscribeInstallation();
}

@Override
public void onPause() {
    super.onPause();
    unregisterReceiver(receiver);
    unsubscribeInstallation();
}

    private void registerReceiver() {
        receiver = new PushBroadcastReceiver();

        IntentFilter filter = new IntentFilter();
//      filter.addAction(Intent.ACTION_BOOT_COMPLETED);
//      filter.addAction(Intent.ACTION_USER_PRESENT);
        filter.addAction("com.google.android.c2dm.intent.RECEIVE");
        registerReceiver(receiver, filter);
        Log.d("ReleaseStation", "Receiver Registered");
    }

    /**
     * BroadcastReceiver for receiving 
     * push messages from Parse
     * @author Austin
     *
     */
    public class PushBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

            if(intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {
                Log.d("Order", intent.getAction());
                Toast.makeText(context, "New Order!.",
                        Toast.LENGTH_SHORT).show();
            }
        }
    }

}

1 个答案:

答案 0 :(得分:2)

您正在unregisterReceiver()onPause()中致电onDestroy()。保证在调用onDestroy()时,onPause()将被调用。因此,摆脱对onDestroy()的{​​{1}}号召唤,你应该处于更好的状态。

一般来说,坚持对:

  • 如果您在unregisterReceiver()中对其进行初始化,请在onCreate()
  • 中进行清理
  • 如果您在onDestroy()中对其进行初始化,请在onStart()
  • 中进行清理
  • 如果您在onStop()中对其进行初始化,请在onResume()
  • 中进行清理