挂起的itent不会触发接收器“onReceive”

时间:2015-01-31 21:40:53

标签: android android-intent broadcastreceiver android-manifest android-pendingintent

我有以下代码在调试时执行:

private void initPendingIntent() {
/* Retrieve a PendingIntent that will perform a broadcast */
    Intent alarmIntent = new Intent(getActivity(), AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, alarmIntent, 0);
}

private PendingIntent pendingIntent;


private void initViews(LayoutInflater inflater, ViewGroup container) {
    rootView = inflater.inflate(R.layout.fragment_settings, container, false);

    rootView.findViewById(R.id.startAlarm).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            start();
        }
    });


public void start() {
    AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
    int interval = 2000;

    manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
    Toast.makeText(getActivity(), "Alarm Set", Toast.LENGTH_SHORT).show();
}

然而执行并没有停在我放在这里的断点处:

public class AlarmReceiver extends RoboBroadcastReceiver {

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

        // For our recurring task, we'll just display a message
        Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
    }
}

和我的清单:

<receiver android:name=".boradcasts.AlarmReceiver"
          android:enabled="false">
</receiver>

1 个答案:

答案 0 :(得分:0)

<receiver android:name=".boradcasts.AlarmReceiver"
      android:enabled="false">
</receiver>

false更改为true =)