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

  显示原文与译文双语对照的内容
0 0

我正在使用AlarmManager每隔 60秒重复一次推送通知服务。 所以每 60秒,服务发送一个新的推送通知,但时间错误( 小时和分钟): 比如,服务从 00: 39开始发送推送通知,在39秒之后,服务会在 60秒后发送新的推送通知,并发送推送通知,同时发送推送通知。
你可以看到这里图像中服务的行为方式 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);
 }
}

我对它I 。 有人知道如何解决这个问题? 每个帮助都将得到极大的赞赏:

时间: 原作者:

0 0

感谢 FoggyDay,我已经解决了我的问题: 由于推送通知只是第一次构建到 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() 方法是空的,但是它工作起来:d,我接受它的他建议。

原作者:
...