Reputation: 71
I need that the user will provide a time and when this time will arrive a notification wiil fired. how can I do that?
public void setReminder(Long taskId, Calendar when) {
Intent i = new Intent(mContext, OnAlarmReceiver.class);
i.putExtra(RemindersDbAdapter.KEY_ROWID, (long)taskId);
PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_ONE_SHOT);
mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);
}
Upvotes: 0
Views: 61
Reputation: 2403
// Calculating the difference
long currentmillis = System.currentTimeMillis();
final Calendar sync= Calendar.getInstance();
sync.set(sync.get(Calendar.YEAR), sync.get(Calendar.MONTH), sync.get(Calendar.DAY_OF_MONTH), mHour, mMinute,0);
long millissync = sync.getTimeInMillis();
long alertmillis = (millissync - currentmillis);
if(alertmillis>=1000)
setAlarm(alertmillis);
Upvotes: 1
Reputation: 2403
public void setAlarm(long startTime)
{
// We have to register to AlarmManager
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
if(startTime>=100000)
{
startTime /= 1000;
int secs = (int)(startTime);
calendar.add(Calendar.SECOND, secs);
}
else
{
int secss = (int)startTime;
calendar.add(Calendar.MILLISECOND, secss);
}
// We set a one shot alarm
long triggerTime = calendar.getTimeInMillis();
//Intent myIntent = new Intent(SettingScreen.this, blue.com.api.tab.BroadCast.MyAlarmService.class);
// pendingIntent = PendingIntent.getService(SettingScreen.this, 0, myIntent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP,triggerTime, pendingIntent);
}
Upvotes: 1
Reputation: 2403
Step 1: You have to create a service for that here my service is MyAlarmService.class as shown below
public class MyAlarmService extends Service {
@Override
public void onCreate() {
// Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent intent) {
//Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
//Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
try
{
Context context = getApplicationContext();
// Displaying Notification
NotificationManager manger = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon, "Notification", System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), Target.class), 0);
notification.setLatestEventInfo(getApplicationContext(), "Notification", "Notification subject", contentIntent);
notification.flags = Notification.FLAG_INSISTENT;
int NOTIFICATION_ID=(int)System.currentTimeMillis();
manger.notify(NOTIFICATION_ID, notification);
}
catch(Exception e)
{
e.printStackTrace();
}
// TODO Auto-generated method stub
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
}
Step 2 : You have to create a pending Intent as shown below
Intent myIntent = new Intent(SynchronizeData.this, MyAlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(this.getApplicationContext(), 0, myIntent, 0);
Step 3 : Create set Alarm
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,triggerTime, pendingIntent);
// trigger time is your time interval in milliseconds
Note: Dont forgot to declare the service in AndroidManifest.xml
Upvotes: 1
Reputation: 15679
It's not as easy as it sounds:
set up an Alarm with Androids AlarmManager.
recieve your Alarm with a BroadcastReciever
Trigger inside the Broadcast the NotificationManager
Upvotes: 0