Android广播接收器已调用但未运行

时间:2014-11-18 11:32:54

标签: android broadcastreceiver android-service alarmmanager

我创建了一个广播的接收器和报警管理器(以下代码):

AlarmManager

Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR, 11);
        cal.set(Calendar.MINUTE, 18);
        cal.set(Calendar.SECOND, 00);

        // Create a new PendingIntent and add it to the AlarmManager
        Intent intent = new Intent(this, MyReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                1234567890, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

MyReciever

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        System.out.println("SERVICE RECIEVED");
        Intent service1 = new Intent(context, MyAlarmService.class);
        context.startService(service1);
    }

}

MyAlarmService

public class MyAlarmService extends Service {

    private NotificationManager mManager;

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
    }

    @SuppressWarnings("static-access")
    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);

        mManager = (NotificationManager) this.getApplicationContext()
                .getSystemService(
                        this.getApplicationContext().NOTIFICATION_SERVICE);
        Intent intent1 = new Intent(this.getApplicationContext(),
                MainActivity.class);

        Notification notification = new Notification(R.drawable.ic_launcher,
                "Alarm", System.currentTimeMillis());
        intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
                | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
                this.getApplicationContext(), 0, intent1,
                PendingIntent.FLAG_UPDATE_CURRENT);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.setLatestEventInfo(this.getApplicationContext(),
                "Alarm", "Open the app now",
                pendingNotificationIntent);

        mManager.notify(0, notification);
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }
}

清单

 <service
            android:name=".MyAlarmService"
            android:enabled="true" />

        <receiver android:name=".MyReceiver"/>

我遇到的问题是Alarm Manager正在运行并调用MyReceiver类,但没有发生任何事情。我知道它调用该函数是因为在log cat中显示以下内容:

11-18 11:18:00.415: I/ActivityManager(765): START u0 {flg=0x4 cmp=pacakge/.MyReceiver (has extras)} from pid -1

包值是包名。

有人可以解释它出错的地方吗?警报调用有效,但功能无法运行。

2 个答案:

答案 0 :(得分:1)

尝试使用

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
            1234567890, intent, PendingIntent.FLAG_UPDATE_CURRENT);

而不是

    PendingIntent pendingIntent = PendingIntent.getActivity(this,
            1234567890, intent, PendingIntent.FLAG_UPDATE_CURRENT);

答案 1 :(得分:0)

API 5中已弃用

onStart

将其更改为onStartCommand。请遵循此link

希望有所帮助