I have an application that keeps a global instance of an ExoPlayer instance to facilitate audio streams in the background.
After opening lots of apps, the audio stops playing.
It happens as follows:
open Activity that starts playing audio
press back button to close Activity
the audio is still playing and keeps doing so if you leave the device alone (as intended)
However, when you open a dozen or more apps after the last step, the ExoPlayer stops playing at some point.
My guess is that a memory cleanup happens and thus ExoPlayer gets deallocated. I tried to get more information from the logs, but that has provided little help so far.
Keeping a reference of the ExoPlayer inside an android.app.Service doesn't make a difference.
The device I am testing on is a Nexus 5 with Android 5.1.x, but the issue happens on other devices too.
I couldn't find a solution in the ExoPlayer documentation pages nor on StackOverflow or Google. Does anyone know the correct way to prevent the ExoPlayer from stopping playback?
To make sure a Service stays alive as much as possible without being killed by the system, you need to make sure you start it as a foreground service.
This means there will be a notification informing the user of the active service so he can be aware of it. Because of that, you must start the service with a corresponding notification. Here is the example from the docs:
Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.notification_title),
getText(R.string.notification_message), pendingIntent);
startForeground(ONGOING_NOTIFICATION_ID, notification);
Related
I create a notification in Service (launched from the BroadcastReceiver) this way:
this.notification = new NotificationCompat.Builder(getApplicationContext())
.setContentTitle("Foo")
.setContentText("Bar")
.setSmallIcon(R.drawable.logo)
.setContentIntent(pIntent)
.build();
this.initializeNotificationSound(volumne, Uri.parse(melody));
this.initializeNotificationVibration();
notification.flags = Notification.FLAG_INSISTENT;
this.notificationManager.cancelAll();
this.notificationManager.notify(0, notification);
This notification is set when a particular SMS is received. I want to create a notification which will play a defined melody and will not be interrupted by the standard SMS notification.
This functionality works pretty good, however in lollipop, following scenario happens:
SMS is received
My notification is launched
After some time, notification sound is stopped and default SMS ringtone is played
This happens only when ringtone mode is silent. Otherwise, it works as expected, because of the cancelAll() method, which interrupts the SMS notification.
Thanks in advance for your response.
You can not change that. If you notice, when more than one notifications are created at the same time, even from a sole application(e.g. Facebook), the sound they make is played simultaneously. This has to do with the notification management of android system itself. Maybe the problem you are facing happens due to some code changes made to the specific Android system version (API level), maybe to the cancelAll() method. One solution would be to find the appropriate line of code for checking current system version. For example:
if(System_version <= lollipop) {
//code works fine. Keep as it is
}
else{
//find the appropriate method and call it here.
}
I'm looking to stop/dismiss a foreground notification for a service for a mediaplayer, much similar to Google's implementation for Google Music.
For instance in Google Music, if you are playing music then the the notification cannot be swiped away. However if you pause the music it can.
This is completely different to how it is implemented on Android 4.4, where the notification starts only when you leave the app and removes itself when you go back into the app. I can't see how to implement this either considering the requirements for a service to have a notification.
Any help would be much appreciated.
How do i remove a foreground notification in Android Lollipop?
You can remove your own foreground Notification by calling stopForeground() from your Service, that called startForeground().
For instance in Google Music, if you are playing music then the the notification cannot be swiped away. However if you pause the music, you can swipe it away.
Presumably, they are updating the setOngoing() value for the Notification based upon whether or not the music is playing.
You can also remove the notification in a tricky way:
start 1st service with startForeground(1, new Notification());
start 2nd service with startForeground(1, new Notification()); and immediately stop it
As a result 1st service is still running in foreground but the notification is gone thanks to stopping 2nd service. I've found it by accident :).
I have a requirement in an android app, where the app should open automatically in the following 2 cases
-when the user with android device reaches some particular location.
-When the app receives some push notification from the server.
Im new to android, Is there any possibility that the app opens up automatically in the above 2 cases.
For location based app use Android's LocationManger
This class provides access to the system location services. These services allow applications to obtain periodic updates of the device's geographical location, or to fire an application-specified Intent when the device enters the proximity of a given geographical location.
For Push message use GCM
I bumped into this problem (the second one) a few days ago and came up with a "temporary" solution (maybe not the cleanest and coolest). Even though this question is old, somebody might have the same problem and find some value here.
Regarding your second question on how you can open and android app automatically when a push notification is received, from inside the service class just put this:
Intent dialogIntent = new Intent(this, myActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
Keep in mind that the code from above will cut off the notification sound so it might be a good idea to check the Screen state and play some sound if it's locked:
/* Check if the device is locked, play ring tone and vibrator */
if (!(((PowerManager) getSystemService(Context.POWER_SERVICE)).isScreenOn())){
vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
Sound = MediaPlayer.create(getApplicationContext(), R.raw.sound);
try { Sound.start(); }
catch(Exception e){ Log.i("intentService", "Error Playing sound");}
vibrator.vibrate(3000);
}
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
Also, don't forget to release mediaplayer !! just do some research and you'll find an answer, there are different ways but one way could be to wait until the sound is played:
Sound.isPlaying()
And then release:
Sound.release()
P.S: You can set the notificationSound to null and remove the "if" condition so the ringtone will always be played (even when the screen is ON and the app is opened automatically)
setSound(null)
I want to create application that can play a streaming music . When I press home my app can running in background but when I open another application that use more memory my app will stop and killed by android system . Anyone have another idea to run my music player app in background?
thank you
You have to implement a foreground service:
https://developer.android.com/guide/components/services.html#Foreground
A foreground service is a service that's considered to be something
the user is actively aware of and thus not a candidate for the system
to kill when low on memory. A foreground service must provide a
notification for the status bar, which is placed under the "Ongoing"
heading, which means that the notification cannot be dismissed unless
the service is either stopped or removed from the foreground.
Example:
Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.notification_title),
getText(R.string.notification_message), pendingIntent);
startForeground(ONGOING_NOTIFICATION_ID, notification);
Look the activity lifecycle :)
http://developer.android.com/guide/topics/fundamentals/activities.html
I think you must do a service.
I am creating an app which plays audio in the background. I have created a background service and a persistent notification to indicate that the music is currently playing.
However, when I create the notification it feels to strong (e.g. the same as when a new txt or email comes in.) and slides down in the status bar. Instead, I would like the notification to just appear in the status bar without anything fancy. I see other apps which do this, but I cannot figure out how it is done.
I have experimented with different notification flags, but I haven't found one that works. I also don't see anything in the NotificationManager which would help.
Anyone know how to do this?
I wound up just re-using the same instance of the Notification object passed to NotificationManager rather than passing in a new one with the same ID.