gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:26:27 25 4
gpt4 key购买 nike

我正在使用 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);
}
}

我对此感到很沮丧。有谁知道如何解决这个问题?每一个帮助将不胜感激:)

最佳答案

感谢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() 方法留空是否是一个好习惯,但它确实有效 :D 但是,我接受有关它的其他建议

关于android - AlarmManager 在发送第一个通知后不更新推送通知时间(小时和分钟),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27871234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com