I am trying to implement a Alarm functionality in my mobile apps. Below is my use case
User can set reminder from Activity "A" once the reminder is set, User will receive a notification 10min before the set time. Now user can remove the reminder from another activity , which is let suppose "B".
Apart from that I also want that user can reschedule his/her reminder setting from notification window.
Below is the code I have written for use case 1
private void setAlarm(){
Intent intent = new Intent(A.this, RepeatAlarm.class);
mAlarmSender = PendingIntent.getService(A.this,
0, new Intent(A.this, RepeatAlarm.class), 0);
// We want the alarm to go off 30 seconds from now.
long firstTime = SystemClock.elapsedRealtime();
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
firstTime, 30*1000, mAlarmSender);
// Tell the user about what we did.
Toast.makeText(A.this, R.string.repeating_scheduled,
Toast.LENGTH_LONG).show();
}
private void stopAlarm(){
// And cancel the alarm.
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(mAlarmSender);
// Tell the user about what we did.
Toast.makeText(B.this, R.string.repeating_unscheduled,
Toast.LENGTH_LONG).show();
}
==============
public class RepeatAlarm extends Service {
NotificationManager mNM;
@Override
public void onCreate() {
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
showNotification();
Thread thr = new Thread(null, mTask, "RepeatAlarm");
thr.start();
}
@Override
public void onDestroy() {
mNM.cancel(R.string.alarm_service_started);
Toast.makeText(this, R.string.alarm_service_finished, Toast.LENGTH_SHORT).show();
}
/**
* The function that runs in our worker thread
*/
Runnable mTask = new Runnable() {
public void run() {
long endTime = System.currentTimeMillis() + 15*1000;
while (System.currentTimeMillis() < endTime) {
synchronized (mBinder) {
try {
mBinder.wait(endTime - System.currentTimeMillis());
} catch (Exception e) {
}
}
}
RepeatAlarm.this.stopSelf();
}
};
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return mBinder;
}
/**
* Show a notification while this service is running.
*/
private void showNotification() {
CharSequence text = getText(R.string.alarm_service_started);
开发者_如何学编程 Notification notification = new Notification(R.drawable.icon, text,
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, B.class), 0);
// Set default sound
notification.defaults |= Notification.DEFAULT_SOUND;
//Set the default Vibration
notification.defaults |= Notification.DEFAULT_VIBRATE;
// Set the light pattern
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
//Custom notification view
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);
contentView.setImageViewResource(R.id.image, R.drawable.icon);
contentView.setTextViewText(R.id.text, "Notification");
notification.contentView = contentView;
notification.contentIntent = contentIntent;
// Send the notification.
// We use a layout id because it is a unique number. We use it later to cancel.
mNM.notify(R.string.alarm_service_started, notification);
}
/**
* This is the object that receives interactions from clients. See RemoteService
* for a more complete example.
*/
private final IBinder mBinder = new Binder(){
@Override
protected boolean onTransact(int code, Parcel data, Parcel reply,
int flags) throws RemoteException {
return super.onTransact(code, data, reply, flags);
}
};
}
Now I am trying to attempt the 2nd case, for that I put 2 buttons on the notification layout, called Reschedule and Cancel, but in this case my view is not showing in the notification window, I have googled from last 2 days but no-luck, Now it is very alarming situation for me, I have to complete it any way by end of the day. So any suggestion or workaround will extremely helpful for me.
Thanks
Ashish
精彩评论