AlarmManager 在发送第一个通知后不更新推送通知时间(小时和分钟)
AlarmManager doesn't update push notification time (hours and minutes) after the first notification sent
我正在使用 AlarmManager 每 60 秒重复一次推送通知服务。因此,每 60 秒,服务发送一个新的推送通知,但时间(小时和分钟)错误:例如,如果服务开始于 00:39,它发送一个时间为 [=30] 的推送通知=];然后,在 60 秒后(所以当时钟打开时 00:40),服务会在与第一个通知相同的时间发送新的推送通知,并且它会在相同的时间 "forever" 发送推送通知.
您可以在此图中看到该服务的行为方式
这里是我的代码("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);
}
}
我对此感到沮丧。有谁知道如何解决这个问题?我们将不胜感激:)
感谢FoggyDay's ,我已经解决了我的问题:由于推送通知仅在第一次构建到 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 但是,我接受有关它的其他建议
我正在使用 AlarmManager 每 60 秒重复一次推送通知服务。因此,每 60 秒,服务发送一个新的推送通知,但时间(小时和分钟)错误:例如,如果服务开始于 00:39,它发送一个时间为 [=30] 的推送通知=];然后,在 60 秒后(所以当时钟打开时 00:40),服务会在与第一个通知相同的时间发送新的推送通知,并且它会在相同的时间 "forever" 发送推送通知.
您可以在此图中看到该服务的行为方式
这里是我的代码("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);
}
}
我对此感到沮丧。有谁知道如何解决这个问题?我们将不胜感激:)
感谢FoggyDay's
所以我的代码可能会以两种方式更改:
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 但是,我接受有关它的其他建议