Play ringtone from the application only for a specific time period - android

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.

Related

How can I show notification which will be removed after some time?

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);

Play my sound over the default notification sound when needed

On onReceive method when i get a new message, after i do some checkups and see that the address number is the one i want.
Is it possible to play my notification sound instead of the default message sound? And if a normal message comes then the default sound will play.
The thing is that the phone plays the sound first, and then my application, so there is a conflict there.
Ive searched for an answer but i couldnt find anything.
What i do is kind of unefficient. I play my sound 6 seconds after the default notifications plays like this:
private Runnable task = new Runnable() {
public void run() {
Uri alarmSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_ALARM);
Ringtone r = RingtoneManager.getRingtone(
getApplicationContext(), alarmSound);
r.play();
}
};
private void delayNotification() {
Handler handler = new Handler();
int millisDelay = 6000;
handler.postDelayed(task, millisDelay);
}
I know its not a good way to do it so thats y i came here:P
notification.sound =Uri.parse("android.resource://"+context.getPackageName()+"/"
+R.raw.tone);

How to setup a countdown timer on a service for 24/7

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.

how to display notififcations one by one?

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.

Changing Android system clock stops timers. How can I restart them?

I need to run a periodic task in an Android application. I currently use a timer like this:
final Handler guiHandler = new Handler();
// the task to run
final Runnable myRunnable = new Runnable() {
#Override
public void run() {
doMyStuff();
}
};
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
guiHandler.post(myRunnable);
}
}, 0, 30000); // run every 30 seconds
This does exactly what I need, but there is a problem: if I change the time on the emulator or phone, the timer stops running. This is what appears in the log when I change the time:
D/SystemClock( 331): Setting time of day to sec=1278920137
W/SystemClock( 331): Unable to set rtc to 1278920137: Invalid argument
Nothing about the timer being interrupted, but it clearly doesn't run anymore after the system clock has changed. I need the task to keep running all the time as long as the application is running.
How can I restart the timer if it gets stopped like this? There's no method on the Timer or TimerTask to check whether it's currently running, so I can't know when to reschedule it. Any ideas?
I think there are a few ways to do this. I wouldn't use the timer in either case.
You can use a handler to run your task in a postDelayed call. Your task would then have to re-register itself with the handler from within itself.
final int ONE_SECOND = 1000; // one second
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
... // do some stuff
if (expression) {
handler.postDelayed(this, ONE_SECOND);
}
}
}, ONE_SECOND);
This will keep the task running while your app is alive. You can also adjust the delayed rate in the postDelayed within the Runnable. This way is semi predictable as long as you make another Looper. Using the main thread may or may not be appropriate depending on what the task is.
There is also an AlarmManager, that you can gain access to via the Context interface, which is meant for recurring tasks tasks at more precise intervals. It's a little more complex to use but you get the flexibility of having use of the RTC and persisted repeatable tasks.
AlarmManager manager = mContext.getSystemService(Context.ALARM_SERVICE);
manager.setRepeating(AlarmManager.RTC,
<start_time_millis>,
<period_millis>,
pendingIntent);
For example, the pending intent can fire a broadcast intent that you can listen to elsewhere. You can create this pendingintent in the onCreate of your custom Application object and cancel the intent in the onTerminate().

Categories

Resources