Я создал приложение, в котором пользователь может выбрать время заезда и выезда, и я хочу, чтобы пользователь отображал уведомление в строке состояния в оба раза, даже если приложение закрыто.
Я использовал службу для запускауведомление о времени и уведомление показывается, когда я звоню им по отдельности, но когда я устанавливаю будильник для обоих вместе, уведомление выскакивает только во время оформления заказа, но говоря это как время заезда.Я не могу понять причину.
Мой класс, где я связываю службу
checkin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
calendar = Calendar.getInstance();
currentHour = calendar.get(Calendar.HOUR_OF_DAY);
currentMinute = calendar.get(Calendar.MINUTE);
timePickerDialog = new TimePickerDialog(Reservation.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minutes) {
selectedhour=hourOfDay;
selectedminute=minutes;
if (hourOfDay >= 12) {
amPm = "PM";
} else {
amPm = "AM";
}
checkin.setText(String.format("%02d:%02d", hourOfDay, minutes) + amPm);
}
}, currentHour, currentMinute, false);
timePickerDialog.show();
}
});
checkout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
calendar = Calendar.getInstance();
currentHour = calendar.get(Calendar.HOUR_OF_DAY);
currentMinute = calendar.get(Calendar.MINUTE);
timePickerDialog = new TimePickerDialog(Reservation.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minutes) {
selectedcouthour=hourOfDay;
selectedcoutmin=minutes;
if (hourOfDay >= 12) {
amPm = "PM";
} else {
amPm = "AM";
}
checkout.setText(String.format("%02d:%02d", hourOfDay, minutes) + amPm);
}
}, currentHour, currentMinute, false);
timePickerDialog.show();
}
});
.....
private void setnotification() {
Calendar calendar = Calendar.getInstance();
int thisYear = calendar.get(Calendar.YEAR);
Log.d("nn", "# thisYear : " + thisYear);
int thisMonth = calendar.get(Calendar.MONTH);
Log.d("nn", "@ thisMonth : " + thisMonth);
int thisDay = calendar.get(Calendar.DAY_OF_MONTH);
Log.d("nn", "$ thisDay : " + thisDay);
// Calendar c = Calendar.getInstance();
calendar.set(thisYear, thisMonth, thisDay);
calendar.set(Calendar.HOUR_OF_DAY, selectedhour);
calendar.set(Calendar.MINUTE, selectedminute);
calendar.set(Calendar.SECOND, 0);
Calendar calendar1 = Calendar.getInstance();
int thisYear1 = calendar1.get(Calendar.YEAR);
Log.d("nn", "# thisYear : " + thisYear);
int thisMonth1 = calendar1.get(Calendar.MONTH);
Log.d("nn", "@ thisMonth : " + thisMonth);
int thisDay1 = calendar1.get(Calendar.DAY_OF_MONTH);
Log.d("nn", "$ thisDay : " + thisDay);
// Calendar c = Calendar.getInstance();
calendar1.set(thisYear, thisMonth, thisDay);
calendar1.set(Calendar.HOUR_OF_DAY, selectedcouthour);
calendar1.set(Calendar.MINUTE, selectedcoutmin);
calendar1.set(Calendar.SECOND, 0);
//Calendar c1 = Calendar.getInstance();
// Ask our service to set an alarm for that date, this activity talks to the client that talks to the service
scheduleClient.setAlarmForNotification(calendar,"Checkin");
scheduleClient.setAlarmForNotification(calendar1, "Checkout");
мой класс обслуживания, который выскакивает уведомление
public class NotifyService extends Service {
NotificationManager manager;
Notification myNotication;
String status;
//Button Start;
NotificationCompat.Builder notification;
PendingIntent pIntent;
//NotificationManager manager;
Intent resultIntent;
TaskStackBuilder stackBuilder;
/**
* Class for clients to access
*/
public class ServiceBinder extends Binder {
NotifyService getService() {
return NotifyService.this;
}
}
// Unique id to identify the notification.
private static final int NOTIFICATION = 123;
// Name of an intent extra we can use to identify if this service was started to create a notification
public static final String INTENT_NOTIFY = "com.example.smartpark.service.INTENT_NOTIFY";
// The system notification manager
private NotificationManager mNM;
@Override
public void onCreate() {
Log.i("NotifyService", "onCreate()");
//mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("LocalService", "Received start id " + startId + ": " + intent);
status=intent.getStringExtra("status");
// If this service was started by out AlarmTask intent then we want to show our notification
if (intent.getBooleanExtra(INTENT_NOTIFY, false)) {
Toast.makeText(getApplicationContext(),"not",Toast.LENGTH_LONG).show();
showNotification();
}
// We don't care if this service is stopped as we have already delivered our notification
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() {
// This is the 'title' of the notification
/*CharSequence title = "Alarm!!";
// This is the icon to use on the notification
int icon = R.drawable.ic_dialog_alert;
// This is the scrolling text of the notification
CharSequence text = "Your notification time is upon us.";
// What time to show on the notification
long time = System.currentTimeMillis();
Notification notification = new Notification(icon, text, time);
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Reservation.class), 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, title, text, contentIntent);
// Clear the notification when it is pressed
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Send the notification to the system.
mNM.notify(NOTIFICATION, notification);
// Stop the service when we are finished
stopSelf();
} */
/* notification = new NotificationCompat.Builder(this);
//Title for Notification
notification.setContentTitle("Learn2Crack Updates");
//Message in the Notification
notification.setContentText("New Post on Android Notification.");
//Alert shown when Notification is received
notification.setTicker("New Message Alert!");
//Icon to be set on Notification
notification.setSmallIcon(R.drawable.ic_notification_overlay);
//Creating new Stack Builder
stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(Reservation.class);
//Intent which is opened when notification is clicked
resultIntent = new Intent(this, Reservation.class);
stackBuilder.addNextIntent(resultIntent);
pIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pIntent);
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, notification.build());
}*/
Intent intent = new Intent("com.example.smartpark.Reservation");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, 0);
Notification.Builder builder = new Notification.Builder(this);
builder.setAutoCancel(false);
builder.setTicker("this is ticker text");
builder.setContentTitle("Alert from Smartpark");
builder.setContentText("Your "+status+" time is just reached");
builder.setSmallIcon(R.drawable.ic_notification_overlay);
builder.setContentIntent(pendingIntent);
builder.setOngoing(true);
builder.setSubText("Time Alert..."); //API level 16
builder.setNumber(100);
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(uri);
builder.build();
myNotication = builder.getNotification();
myNotication.flags |= Notification.FLAG_AUTO_CANCEL;
manager.notify(11, myNotication);
}
Я ожидаю, что уведомлениевсплыть точно в нужное время с правильным контекстом.