为什么通知会重复?

时间:2015-09-28 11:17:27

标签: java android-alarms

我有一个使用struct node* new_node = malloc(sizeof(struct node)); 广播接收器的应用,并开始通知。 一切都很好,直到(直到我才知道,我才知道)。

当我在一定时间内开始闹铃时,一切正常,但通知会重复几次。我使用了AlarmManagerInteger i = 0;

如果有多个闹钟,请先重复。 当通知第二次返回时,有日志。 那是什么?

"Message of Notification" + i++

接收器

09-28 13:55:55.598  16489-16489/? D/dalvikvm﹕ Late-enabling CheckJNI
09-28 13:55:55.668  16489-16489/com.example.nes.app E/Trace﹕ error opening trace file: No such file or directory (2)
09-28 13:55:55.668  16489-16489/com.example.nes.app D/ActivityThread﹕ setTargetHeapUtilization:0.25
09-28 13:55:55.668  16489-16489/com.example.nes.app D/ActivityThread﹕ setTargetHeapIdealFree:8388608
09-28 13:55:55.668  16489-16489/com.example.nes.app D/ActivityThread﹕ setTargetHeapConcurrentStart:2097152

alarmManager.class

public class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent service1 = new Intent(context, MyAlarmService.class);

        context.startService(service1);
    }
}

以及如何调用Alarm Manager

public class MyAlarmService extends Service {
    NotificationManager mManager;
    SharedPreferences sPref;
    String eventCode = "EV_KEY";
    int i = 0;

    @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(),onResume.class);

        Notification notification = new Notification(R.mipmap.ic_launcher, getResources().getString(R.string.timeToSmoke), System.currentTimeMillis());
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.flags |= Notification.DEFAULT_VIBRATE ;
        notification.defaults|= Notification.DEFAULT_SOUND;
        notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
        intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        i++;

        PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
        notification.setLatestEventInfo(this.getApplicationContext(), getResources().getString(R.string.time), "Text"+String.valueOf(i), pendingNotificationIntent);
        Random r = new Random();
        int id = r.nextInt(10000 - 50) + 50;
         mManager.notify(id, notification);

    }


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

1 个答案:

答案 0 :(得分:0)

解决。我只是将所有代码从Alarm Manager.class切换到Broadcast recever。 但这个问题仍然存在。为什么第一个变体会出现这样的错误?