I need to show a lot of notifications to StatusBar. But in order to not to fill up statusbar with my messages I have to automatically remove notification after 1 minute, for example. How can I do this?
Follow these steps
Create notification and create Alarm for required duration
Cancel the notification using NotificationManager.cancel(id) in BroadcastRecevier of Alarm
I did it with Timer:
Notification notification = nb.build();
notifManager.notify(id, notification);
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
notifManager.cancel(id);
timer.cancel();
}
}, REMOVE_TIME, 1000);
Related
I am developing an android app and want to know how can I receive an incoming call.
I have created a broadcast receiver and in its on receive method I have given a 15 second delay using countdown timer on call state ringing.Now, I want to receive an incoming call in countdown timer's finish method. I am not getting any way to implement it. can anyone suggest ?
thanks!!
instead of using a countdown timer, set a one-time Alarm instead which then fires your method to receive the call. You can do something like this:
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Intent cHandler = new Intent (this, CallHandlers.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, cHandler, PendingIntent.FLAG_CANCEL_CURRENT);
//Set an alarm that will trigger in 15 seconds
alarm.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis() + (15 * 1000), pi);
hi you can use a coundowntimer visit http://developer.android.com/reference/android/os/CountDownTimer.html
new CountDownTimer(15000, 1000) {
public void onTick(long millisUntilFinished) {
//here you can have your logic for call
}
public void onFinish() {
mTextField.setText("done!");
}
}
.start();
for working code example of countdowntimer visit http://www.filefactory.com/file/cbbbc38/n/SimpleCountDownTimerExample.zip
I recently just learned and developed a widget. I understand that the widget have an auto update at every 30 - 60 minutes minimum. Now I have been asked to reduced that auto update to 5 minutes.
So I have thought up for creating another Service Thread that is constantly running a countdown timer every 5 minutes and refreshes the app to check for any possible errors. These errors are actually ping tests results. If a server is down, I will execute a Toast Message to inform the user that this server is down.
So, how should I go about doing this? Or is there a better suggestion. Please enlighten me.
May this helps you:
Buddy use TimerTask to call after specific time interval
Timer timer = new Timer();
timer.schedule(new UpdateTimeTask(),1, TimeInterval);
and
class UpdateTimeTask extends TimerTask {
public void run()
{
// code here
}
}
Or You can Use AlaramManager also:
Set AlarmManager like this:
private static final int REPEAT_TIME_IN_SECONDS = 60; //repeat every 60 seconds
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(),
REPEAT_TIME_IN_SECONDS * 1000, pendingIntent);
Change AlarmManager.RTC to AlarmManager.RTC_WAKEUP if u want to wake up phone when it goes off. More about AlarmManager Click Here
Those two parameters also means that your alarm time will be System.currentTimeMilis() which is time in UTC.
I think better use runnable with postDelayed, like this:
private Handler handler = new Handler();
private Runnable runnable = new CustomRunnable();
private class CustomRunnable implements Runnable {
public void run() {
// your logic
handler.postDelayed(runnable, REFRESH_TIME);
}
}
REFRESH_TIME — your constant to refresh in millis. Just run once handler.postDelayed(runnable, REFRESH_TIME); where you want.
I have an android application that gets user location every 2 minutes using requestLocationUpdates service, so now once it reaches 25 minutes I want the running service to be stopped. Is it possible?
Thank you.
Make a timer (1500000 = 25 minutes in milliseconds):
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
//stop your service here
}
}, 0, 1500000);
he, i am new to android platform.
Now i am developing a small application based on notifications.
In my application i am maintaining a timer based on that time every time a notification is displayed.
But the problem is first time notification is displayed and the second notification is appended above the first notification , third notification is append above the second notification..................
I want to display the notifications one by one.
if any one has idea how to display the notification one by one .please reply me.
here is my code
myTimer = new Timer();
myTimer.schedule(new TimerTask()
{
#Override
public void run()
{
TimerMethod();
}
}, 10000,10000);
}
private void TimerMethod()
{
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable()
{
public void run()
{
mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
final Notification notifyDetails = new Notification(R.drawable.icon,"New Alert, Click Me!",System.currentTimeMillis());
Context context = getApplicationContext();
CharSequence contentTitle = "Notification Details...";
CharSequence contentText = "you have a new notification find clicking me";
Intent notifyIntent = new Intent(Notifi.this,Notifi.class);
PendingIntent intent =
PendingIntent.getActivity(Notifi.this, 0,
notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
}
};
Thanks in advance
Store a fixed amount of time say 5000ms in a variable.
Now, start the 1st notification and get the current system time. To this add that 5000ms and continue processing. At the end of 5000ms close the notification. If the 2nd notification must be displayed within 5secs of the 1st one, then use a if condition statement and check whether 5000ms have elapsed since the start time of 1st notification. If it has elapsed then display the 2nd one, else wait till the 1st one completes.
You can also simply check if any notification is open using a simple flag that will be set to 1 when a notification starts and to 0 when it closes. If it is 0, a new notification can be displayed.
[You may also be able to use 2 threads to do this. On 1st thread run all processes. The 2nd one just interrupts the 1st one at set periods after being started. But haven't tried out this]
Hope I was able to help you.
I need to play a ringtone from a application which I am able to achieve. Now, I want to play the ringtone only for the specific duration based on the user input.
If user selects 60sec, the audio should play 60sec only and then stop.
Is there any way to achieve this?
Cheers,
Prateek
Yes, timertask works well for this. Here's the code I use and it works:
long ringDelay = 3500;
Uri notification = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_ALARM);
final Ringtone alarmRingtone = RingtoneManager
.getRingtone(getApplicationContext(), notification);
alarmRingtone.play();
TimerTask task = new TimerTask() {
#Override
public void run() {
alarmRingtone.stop();
}
};
Timer timer = new Timer();
timer.schedule(task, ringDelay);
Of course. Use TimerTask to stop playback after the given period of time.