发送第一个通知后,AlarmManager不会更新推送通知时间(小时和分钟)

时间:2015-01-09 23:59:49

标签: android push-notification alarmmanager

我使用AlarmManager每60秒重复推送通知服务。因此,每60秒,服务发送一个新的推送通知,但错误的时间(小时和分钟):例如,如果服务在00:39开始,它会发送时间= 00:39的推送通知;然后,在60秒后(所以当时钟在00:40时),服务在第一次通知的同一时间发送新的推送通知,并且它将在“永久”的同时发送推送通知。
您可以在此图像中查看服务的行为方式 enter image description here

这里我的代码(“ServicesDemo.java”调用名为“MyService.java”的服务): ServicesDemo.java

public class ServicesDemo extends Activity implements OnClickListener {
  Button buttonStart, buttonStop;
  AlarmManager alarmManager;
  PendingIntent pendingIntent;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    buttonStart = (Button) findViewById(R.id.buttonStart);
    buttonStop = (Button) findViewById(R.id.buttonStop);

    buttonStart.setOnClickListener(this);
    buttonStop.setOnClickListener(this);
  }

  public void onClick(View src) {
    switch (src.getId()) {
    case R.id.buttonStart:
        Intent myIntent = new Intent(ServicesDemo.this , MyService.class);
        alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        pendingIntent = PendingIntent.getService(ServicesDemo.this, 0, myIntent, 0);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 60000 , pendingIntent);  //set repeating every 60 seconds
      break;
    case R.id.buttonStop:
      alarmManager.cancel(pendingIntent);
      break;
    }
  }
}

MyService.java

public class MyService extends Service {
    private static final String TAG = "MyService";
    NotificationManager NM;
    Notification notify;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        NM=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notify=new Notification(android.R.drawable.
                stat_notify_more,"Notification",System.currentTimeMillis());
        PendingIntent pending=PendingIntent.getActivity(
                getApplicationContext(),0, new Intent(),0);
        // Hide the notification after its selected
        notify.flags |= Notification.FLAG_AUTO_CANCEL;
        notify.setLatestEventInfo(getApplicationContext(),"Title","Body",pending);
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDestroy");

    }

    @Override
    public void onStart(Intent intent, int startid) {
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onStart");
        NM.notify(0, notify);
    }
}

我对此感到沮丧。有谁知道如何解决这个问题?我们将非常感谢每一位帮助:)

1 个答案:

答案 0 :(得分:1)

感谢FoggyDaysuggestion,我已经解决了我的问题:由于推送通知仅在第一次构建到onCreate()方法时,很明显它的时间从未改变过。我还使用了一种不赞成使用的方式来构建推送通知,但问题并非由此引起。
因此我的代码可能会以两种方式改变:1)以这种(已弃用的)方式:

public class MyService extends Service {
    private static final String TAG = "MyService";
    NotificationManager NM;
    Notification notify;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {

    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDestroy");

    }

    @Override
    public void onStart(Intent intent, int startid) {
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onStart");

        NM=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notify=new Notification(android.R.drawable.
                stat_notify_more,"Notification",System.currentTimeMillis());
        PendingIntent pending=PendingIntent.getActivity(
                getApplicationContext(),0, new Intent(),0);
        // Hide the notification after its selected
        notify.flags |= Notification.FLAG_AUTO_CANCEL;
        notify.setLatestEventInfo(getApplicationContext(),"Title","Body",pending);
        NM.notify(0, notify);
    }
}

2)......并以这种(更好的)方式:

public class MyService extends Service {
    private static final String TAG = "MyService";
    NotificationManager NM;
    NotificationCompat.Builder mBuilder;
    NotificationManager mNotificationManager;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {

    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDestroy");
        mNotificationManager.cancelAll();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onStart(Intent intent, int startid) {
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onStart");
        mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.icon)
                .setContentInfo("Ciao!")
                .setSubText("Hey!")
                .setTicker("Hoy!")
                .setContentTitle("My notification")
                .setContentText("Hello World!");

        Intent resultIntent = new Intent(this, ResultActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(ResultActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);
        mBuilder.setAutoCancel(true);
        mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(0, mBuilder.build());
    }
}

我不知道将onCreate()方法留空是否是一个好习惯,但它有效:但是,我接受有关它的其他建议