我在我的应用程序中包含了一个NotifyService,通过日期选择器在选定的特定日期显示特定通知...问题是最新通知只被触发而不是上一个...我还试过给出一个唯一的通知我仍然没有工作......
继承人的主要活动 -
public class MainActivity extends Activity {
private ScheduleClient scheduleClient;
private DatePicker picker;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scheduleClient = new ScheduleClient(this);
scheduleClient.doBindService();
picker = (DatePicker) findViewById(R.id.scheduleTimePicker);
}
public void onDateSelectedButtonClick(View v){
int id = MyApp.preferences.getInt("notif", 0);
id++;
MyApp.preferences.edit().putInt( "notif" , id).apply();
int day = picker.getDayOfMonth();
int month = picker.getMonth();
int year = picker.getYear();
Calendar c = Calendar.getInstance();
c.set(year, month, day);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
// Ask our service to set an alarm for that date, this activity talks to the client that talks to the service
scheduleClient.setAlarmForNotification(c);
Toast.makeText(this, "Notification set for: "+ day +"/"+ (month+1) +"/"+ year, Toast.LENGTH_SHORT).show();
}
@Override
protected void onStop() {
if(scheduleClient != null)
scheduleClient.doUnbindService();
super.onStop();
}
}
ScheduleClient -
public class ScheduleClient {
private ScheduleService mBoundService;
private Context mContext;
private boolean mIsBound;
public ScheduleClient(Context context) {
mContext = context;
}
public void doBindService() {
mContext.bindService(new Intent(mContext, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
}
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mBoundService = ((ScheduleService.ServiceBinder) service).getService();
}
public void onServiceDisconnected(ComponentName className) {
mBoundService = null;
}
};
public void setAlarmForNotification(Calendar c){
mBoundService.setAlarm(c);
}
public void doUnbindService() {
if (mIsBound) {
// Detach our existing connection.
mContext.unbindService(mConnection);
mIsBound = false;
}
}
}
时间表服务 -
public class ScheduleService extends Service{
public class ServiceBinder extends Binder {
ScheduleService getService() {
return ScheduleService.this;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("ScheduleService", "Received start id " + startId + ": " + intent);
// We want this service to continue running until it is explicitly stopped, so return sticky.
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
// This is the object that receives interactions from clients. See
private final IBinder mBinder = new ServiceBinder();
public void setAlarm(Calendar c) {
new AlarmTask(this, c).run();
}
}
AlarmTask -
public class AlarmTask implements Runnable{
private final Calendar date;
private final AlarmManager am;
private final Context context;
public AlarmTask(Context context, Calendar date) {
this.context = context;
this.am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
this.date = date;
}
@Override
public void run() {
Intent intent = new Intent(context, NotifyService.class);
intent.putExtra(NotifyService.INTENT_NOTIFY, true);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
// Sets an alarm - note this alarm will be lost if the phone is turned off and on again
am.set(AlarmManager.RTC, date.getTimeInMillis(), pendingIntent);
}
}
最后是NotifyService -
public class NotifyService extends Service {
/**
* Class for clients to access
*/
public class ServiceBinder extends Binder {
NotifyService getService() {
return NotifyService.this;
}
}
// Unique id to identify the notification.
public static final String INTENT_NOTIFY = "com.blundell.tut.service.INTENT_NOTIFY";
private NotificationManager mNM;
private Notification notif;
@Override
public void onCreate() {
Log.i("NotifyService", "onCreate()");
mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("LocalService", "Received start id " + startId + ": " + intent);
if(intent.getBooleanExtra(INTENT_NOTIFY, false))
showNotification();
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
// This is the object that receives interactions from clients
private final IBinder mBinder = new ServiceBinder();
/**
* Creates a notification and shows it in the OS drag-down status bar
*/
private void showNotification() {
CharSequence title = "Alarm!!";
int icon = R.drawable.ic_dialog_alert;
CharSequence text = "Your notification time is upon us.";
Notification.Builder notification = new Notification.Builder(NotifyService.this);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, SecondActivity.class), 0);
int Notif = MyApp.preferences.getInt("notif", 1);
notification.setContentTitle(title)
.setContentText(text)
.setSmallIcon(icon)
.setContentIntent(contentIntent);
notif = notification.getNotification();
notif.flags |= Notification.FLAG_AUTO_CANCEL;
mNM.notify(Notif, notif);
stopSelf();
}}
所以在这里,当我选择一天例如2017年3月31日,设置Notif然后再设置另一个到1/1/2018然后第一个被删除我猜...任何帮助将不胜感激:)
在你的NotifyService
类中,属于notify()
对象mNM的NotificationManager
方法需要一个不同的id用于通知,否则它将替换现有的通知。阅读NotificationManager.notify()
docs here