Status bar notification with sound doing call
I'm doing an app that need to notify the user doing a call that the call is taking to long... I got every thing up an running and an the notification is made at the right time (I can see it at the status bar) but with no sound. If I make the notification call when there is no call the sound is played.
My notification looks like this:
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.icon;
CharSequence tickerText = "Hello From CallTimerService";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "ss";
Intent notificationIntent = new Intent(this, CallTimer.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notification.defaults = Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(2, notification);
I also tried changing the notification.audioStreamType using the AudioManager.STREAM_ but with no luck.
Do any body now how to do this? or just an good idea what to try next....
Asked by: Daryl403 | Posted: 24-01-2022
Answer 1
I have an option in my app to play a sound when the user is in a phone call. What you have to do is use the media player to play the sound. Here is my code:
if(callStateIdle){
notification.sound = Uri.parse(notificationSound);
}else{
new playNotificationMediaFileAsyncTask().execute(notificationSound);
}
Here is the Async Task:
private static class playNotificationMediaFileAsyncTask extends AsyncTask<String, Void, Void> {
protected Void doInBackground(String... params) {
MediaPlayer mediaPlayer = null;
try{
mediaPlayer = new MediaPlayer();
mediaPlayer.setLooping(false);
mediaPlayer.setDataSource(_context, Uri.parse(params[0]));
mediaPlayer.prepare();
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener(){
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
mediaPlayer = null;
}
});
return null;
}catch(Exception ex){
Log.e("ERROR: " + ex.toString());
mediaPlayer.release();
mediaPlayer = null;
return null;
}
}
protected void onPostExecute(Void result) {
//Do Nothing
}
}
This has worked well for me so far.
Answered by: Alfred851 | Posted: 25-02-2022Answer 2
Try using RingtoneManager. The following worked for me: RingtoneManager.getRingtone(context, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)).play();
Similar questions
Notification on new Email with Android
is there a way for an app to get notified when a new email has been received? My goal is, to rise an alert (play a sound, vibrate, whatever) if an email with a specific topic gets received (think of it like an "filter" or a "rule"), but I dont want to check the email server by myself. I think I'm searching for something like "android.telephony.PhoneStateListener" but for Email.
android - Is there any way to receive a notification when the user powers off the device?
I need to know when the user powers off his/her phone. Are there any broadcasts (or similar) that notify when the user's phone is powered off?
java - How to create notification icon badge on Android apps (like iPhone)
I am very new to programming and would like to know what is the best way to go about creating a notification icon badge similar to the ones on the iPhone apps. This would be basically for creating a badge for the notifications that end up in the notification bar.
android - Disable the ability to drag down the notification bar
I am doing an android applicaiton and I know its possible to show or hide the notification bar.
but is there a way to show it but Disable the ability to drag it down?
Thanks
Android Unable to launch the Inbox from a notification
I have the following code that creates a notification when an SMS message is received by the phone. It displays the notification correctly; however, when the user clicks the notification, nothing happens. It should open up the SMS inbox so the user can view their message. Thanks in advance.
mNotificationManager = (NotificationManager) arg0.getSystemService(Context.NOTIFICATION_SERVICE);
Uri uri = Uri.pars...
How to handle Alarm notification in Android?
I'm developing an media player application for Android, for which I need to handle any Alarm notification, and based on that I'll pause my playback. When the Alarm in snoozed or dismissed, I'll then resume the playback.
I googled a lot for the Alarm handling, but what I found was the way to enable Alarm notifications through code, set the intent and then handle it. However, no where could I locate just handling th...
java - How to bring up list of available notification sounds on Android
I'm creating notifications in my Android application, and would like to have an option in my preferences to set what sound is used for the notification. I know that in the Settings application you can choose a default notification sound from a list. Where does that list come from, and is there a way for me to display the same list in my application?
Update missed calls notification on android
I need to cancel the missed calls notification for a certain number. I've seen the NotificationMgr class on com.android.phone but i'm unable to call it trough reflection. Is there any other way?
android - How do I remove Notification from status bar?
I am displaying a Notification in status bar depending up on my condition. Up to this, it is OK.
Now problem in my application is when I come back to application, still the Notification is displayed in the status bar.
I don't want the Notification when I come back from the application. For this give me some suggestions.
android - Remove the notification icon from the status bar
I am displaying an icon in status bar.Now I want to remove that icon immediately when I open that content, after some time if we receive any alert, that icon will be displayed again. How can I do this?
Using custom drawables in an Android Notification
Is there any way to use an image that I'm generating on the fly as a Notification icon?
Notification on new Email with Android
is there a way for an app to get notified when a new email has been received? My goal is, to rise an alert (play a sound, vibrate, whatever) if an email with a specific topic gets received (think of it like an "filter" or a "rule"), but I dont want to check the email server by myself. I think I'm searching for something like "android.telephony.PhoneStateListener" but for Email.
Android: Looking for an easy way to play notification sounds in a loop manually
I am fiddling around with a dialog that pops up and displays an alarm information. (I know, it has been documented that in Android it's "better" style to use notifications instead, but just like the alarm clock I want it to display a dialog on top of whatever you do to really get the users immediate attention - as the user expects this behavior I don't think it's bad style!)
Now - the only easy way I found is using...
android - Is there any way to receive a notification when the user powers off the device?
I need to know when the user powers off his/her phone. Are there any broadcasts (or similar) that notify when the user's phone is powered off?
android - Notification question
I want to show a notification when a call is active.
I have done the first easy part. Notification starts when my call intent starts.
Now to the tricky part, how can I tell my Notification that the call has ended?
java - resuming an activity from a notification
I have a notification in the status bar for my app:
Notification notification = new Notification(R.drawable.icon, null, System.currentTimeMillis());
Intent notificationIntent = new Intent(this.parent, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this.parent, 0, notificationIntent, 0);
...
notification.flags = Notification.FLAG_ONGOING_EVENT;
...
android - Changing notification settings in native email app
Is there a way to change notification settings in the native email app (possibly also gmail)?
I'm thinking about locale plugin which would allow to change notification settings in each app independently. For example disable sound notifications in email app and leave in gmail...
Close activity via click on Android notification list
I'm looking to find out how to stop an activity instead of resuming upon the click of the item on the notification list. Any ideas?
java - How to create notification icon badge on Android apps (like iPhone)
I am very new to programming and would like to know what is the best way to go about creating a notification icon badge similar to the ones on the iPhone apps. This would be basically for creating a badge for the notifications that end up in the notification bar.
android - Disable the ability to drag down the notification bar
I am doing an android applicaiton and I know its possible to show or hide the notification bar.
but is there a way to show it but Disable the ability to drag it down?
Thanks
Still can't find your answer? Check out these communities...
Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android