Reputation: 2946
Within my application a user can set a certain time, i wish for a notification to be displayed on the screen when the time reaches the set time (basically an alarm) out wheather the application is currently running or not.
How would i go about doing this?
Upvotes: 0
Views: 4681
Reputation: 6602
Check now, I have updated the code.
Here is the code for CountDownTimer. and when time completes it sends an notification. You can use it in Service or in a normal Activity.
public String ns = Context.NOTIFICATION_SERVICE;
public NotificationManager mNotificationManager;
Notification notification;
PendingIntent contentIntent;
Intent notificationIntent;
public class Main extends Activity{
private final long startTime = 50000;
private final long interval = 1000;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
int time=Integer.parseInt(editText.getText().toString());
time=time*1000;
startTime = time;
// place this code on button listener if you want.
countDownTimer = new MyTimer(startTime, interval);
}
}
public class MyTimer extends CountDownTimer
{
public MyTimer(long startTime, long interval)
{
super(startTime, interval);
}
@Override
public void onFinish()
{
mNotificationManager = (NotificationManager) getSystemService(ns);
notificationIntent = new Intent(this, MyActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification = new Notification(R.drawable.icon,"Time up..", System.currentTimeMillis());
notification.setLatestEventInfo(this, "CallingaCab", "YOUR TIME COMPLETED", contentIntent);
mNotificationManager.notify(0, notification);
startActivity(notificationIntent);
}
@Override
public void onTick(long millisUntilFinished)
{
//Perform what do you want on each tick.
}
}
}
Upvotes: 2
Reputation: 921
Please create service to extends Service class that service run on background and check what ever you want to. here you can find service example.
Upvotes: 0
Reputation: 1468
No use getting into a long answer, you obviously didn't search much...
http://developer.android.com/reference/android/app/AlarmManager.html
Upvotes: 2