Stopping a service when home button is clicked? - java

I have a service which plays music when the page is loaded, and carries on to all the other pages as it should. It still played when the application was destroyed, so i put a onDestroy method and an onResume method.
#Override
public void onDestroy(){
super.onDestroy();
Intent ServiceMusic = new Intent(this, BackgroundMusic.class);
stopService(ServiceMusic);
}
#Override
public void onResume(){
super.onResume();
Intent ServiceMusic = new Intent(this, BackgroundMusic.class);
startService(ServiceMusic);
}
Now the problem is when the user clicks the home button the service carries on, so I implemented a onPause and it worked fine, just when the user goes to another page the music restarts.
#Override
public void onPause(){
super.onPause();
Intent ServiceMusic = new Intent(this, BackgroundMusic.class);
stopService(ServiceMusic);
}
How can i overcome this so the music carries on playing when going through the application but when the Home Button is clicked i pause the service?

I would remove the code that is in individual activities and use the registerActivityLifecycleCallbacks mechanism in your application subclass. This will allow you to keep the code in one place and be smarter about starting and stopping your service.
Be aware that with these callbacks, you'll be notified that an Activity has stopped before another activity has started, so you will need some kind of delay when you decide to stop your service. You can use a handler with a delayed runnable to account for this issue.
https://developer.android.com/reference/android/app/Application.html#registerActivityLifecycleCallbacks(android.app.Application.ActivityLifecycleCallbacks)

Related

android)The application may be doing too much work on its main thread

private void Filter_save() {
MainActivity firstActivity = (MainActivity) MainActivity.firstActivity;
filter_button = findViewById(R.id.button_filter);
filter_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
firstActivity.finish(); //Code to close the previous page main activity
startActivity(intent); //Go to Main Activity
finish();//Current activity closed
}
});
}
I/Choreographer: Skipped 31 frames! The application may be doing too much work on its main thread.
I'm making an app that displays markers on a map.
The following code controls which markers appear on the map.
There is no problem if you turn off the app by going back or just turn off the app with finish().
However, if you use intents, such as code, you get "The application may be doing too much work on these main threads," and the frame is increasing.
I have to refresh the previous page, main activity, so I am writing the code to turn it over to the intents.
"The application may be doing too much work on its main thread"
This slows down the app and eventually turns off.
Is there any way?

How to restart app and clear all data with Button [duplicate]

My current Android Application needs to call
ActivityManager.clearApplicationUserData()
to simulate the user clearing App storage
Which works fine.
A side effect of calling clearApplicationUserData() is that the App is (understandably) closed.
Which gives a poor user experience.
I am having difficulty in restarting my Application once I have called clearApplicationUserData().
I have tried using startActivity, Alarm Manager with Pending Intent, Foreground/Background service.
Nothing works.
Is it impossible to restart an Android App having called clearApplicationUserData()?
(1st answer: this answer only works on limited situations. it's not a complete answer)
public boolean clearApplicationUserData ()
Description
Return: true if the application successfully requested that the application's data be erased; false otherwise.
As the reference website stated, we have a returnee before the application is being closed. so, we are going to use this returnee in order to restart the app.
if(ActivityManager.clearApplicationUserData)
{
doRestart = true;
}
when Activity onDestroy() and onStop() are called restart app.
#Override
protected void onDestroy() {
super.onDestroy();
if(doRestart){
Intent intent = new Intent(this, Activity.class);
this.startActivity(intent);
}
}
#Override
protected void onStop() {
super.onStop();
if(doRestart){
Intent intent = new Intent(this, Activity.class);
this.startActivity(intent);
}
}
We put restart action in both onDestroy() and onStop() in order to make sure the app will be restarted again.
And also, I think it's a good idea to force stop activity before OS stops it.
if(ActivityManager.clearApplicationUserData)
{
doRestart = true;
finish(); <= i mean this
}
it's because it makes sure that onDestroy() and onStop() will be invoked.
My suggestion might sound trivial but, have you consider not calling ActivityManager.clearApplicationUserData()?
Here what the docs says about this method:
Permits an application to erase its own data from disk. This is
equivalent to the user choosing to clear the app's data from within
the device settings UI. It erases all dynamic data associated with the
app -- its private data and data in its private area on external
storage -- but does not remove the installed application itself, nor
any OBB files.
So in order to mimic this behavior you just need to clear you internal and external storage directories. No permissions are needed to access any of those.
(2nd answer: I need much more contribution on it)
After 8 hours of researching in Android OS and Android Developers Website in order to find a solution to restart activity when clearApplicationUserData is invoked. Finally, I would be able to find a nice/hacking solution.
This solution looks like Zidane dribble :)
Let's introduce the solution. at first, clearApplicationUserData clears all the clues of the application when is invoked like tasks, notifications, alarms and etc. therefore, explicit Activity calling is impossible.
implicit way is the only possible way of calling activity.
After a couple of tests I found that application manifest registered intent-filters wouldn't be removed and they are able to listen for incoming system broadcasts.
Approximately, 98% of system broadcasts wouldn't be received by cleared application and that 2% remained might not be broadcasted very soon.
so what to do? hmmm? come on man I must find a solution ...
bingo, ** I must trigger something in order to system broadcast it** <= looks hacking :)
so I decide to choose WIFI_STATE_CHANGED because
Easy Access permission
System Broadcasts it on delay <= this makes sure that the app is closed
before broadcast
manifest.xml
<receiver
android:name=".PackageDataClearedReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="100">
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
<action android:name="android.net.wifi.STATE_CHANGE" />
</intent-filter>
</receiver>
MainActivity.java
public class MainActivity extends AppCompatActivity {
ActivityManager am;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppCompatButton btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new Thread(new Runnable() {
#Override
public void run() {
am = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
if (am != null) {
ExecutorService pool = Executors.newFixedThreadPool(2);
final Collection<Future> futures = new HashSet<Future>();
futures.add(pool.submit(new Runnable() {
#Override
public void run() {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
wifiManager.setWifiEnabled(false);
am.clearApplicationUserData();
}
}));
for (Future future : futures) {
future.isDone();
}
}
}
}).start();
}
});
}
}
demo
keep in mind, it's just A minimum viable product that needs to be developed more in order to make it work perfectly.
Clearing the app data on the device through the API clearApplicationUserData() resets the app as if it were just installed. As you have found, any alarms and broadcasts registered with your app are also cleared. The most efficient way to keep your app in the foreground would be to clear the data yourself, as others have pointed out, rather than using the API. Here is an example: https://stackoverflow.com/a/9073473/949224
However, if you are determined to use the API (which does guarantee all data is cleared) and the app is force-stopped, I have a suggestion:
Create a small companion app that can be launched just prior to you clearing your app data. The companion app simply re-launches your app, possibly after a short time-out.
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("example.com.testrelaunchapp");
if (launchIntent != null) {
startActivity(launchIntent);//null pointer check in case package name was not found
} else {
Log.w( TAG, "Unable to resolve launch activity of relauncher companion app");
}
((ActivityManager)getSystemService(Context.ACTIVITY_SERVICE))
.clearApplicationUserData();
The companion app itself needs to close afterwards, and ideally should be hidden from the Activity Stack etc..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Intent launchIntent = getPackageManager().getLaunchIntentForPackage("example.com.yourmainapp");
if (launchIntent != null) {
Handler handler = new Handler(getMainLooper());
handler.postDelayed(new Runnable() {
#Override
public void run() {
Log.i( TAG, "About to act on launchIntent");
launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(launchIntent);
finish();
System.exit(0);
}
}, 1000);
}
}
I have seen this work with Android 6.0, but no guarantees it would be versatile and work across the board. There would be more to do to make the companion app UI-less if desired and to be hidden from the phone's launcher. You would also probably want to bundle the APK as a file within your own app and install it upon first run, which would need the user to enable installation from "Unknown sources" (non-Play store). It can be done through intents to the right System settings, if needed, but users would need a good explanation why this is needed.
So, as I was saying, the simpler approach is to clear the data and app permissions yourself.

How do I refresh Webview everytime the app is brought back from background?

I have a webview application, and I need to refresh(using webview.Reload()) the webview whenever the app has been running in the background (has not been killed) and is brought back.How would I do that?
As a user navigates through, out of, and back to your app, the Activity instances in your app transition through different states in their lifecycle. The Activity class provides a number of callbacks that allow the activity to know that a state has changed: that the system is creating, stopping, or resuming an activity, or destroying the process in which the activity resides.
#Override
public void onResume() {
super.onResume();
webView.reload();
}
https://developer.android.com/guide/components/activities/activity-lifecycle

How to update activity UI when app is in background/closed

I have a service which controls my mediaplayer object and when i close my app, a notification is still shown to control playback.
Now when a song is done playing i want update the UI in my activity and i did this with a broadcastreceiver, but this only works when my app is visible and not in the background/closed. (unregistered broadcastreceiver in onPause)
But how do i keep listening for these events when my application is not visible and when the user opens my application again it has the updated UI (new song).
Service
#Override
public void onCompletion(MediaPlayer mp) {
Log.d(TAG, "OnCompletion called!");
Intent broadCastReceiverIntentUpdateSong = new Intent(Constants.ACTIONS.BROADCAST_UPDATE_SONG);
sendBroadcast(broadCastReceiverIntentUpdateSong);
}
When your app starts, it should ask the Service for the current state of the player and show that.
While the app is running and in the foreground, it can listen for the broadcast events and update the UI (or its own internal state) accordingly.
When your app goes to the background, it doesn't need to do anything. When it comes again to the foreground (in onResume()) it can again ask the Servicefor the current state of the player.
You can have the Activity bind to the Service and use AIDL to get the current state OR you can just call startService() with an Intent that contains an ACTION or an EXTRA that indicates that you want to know the current state, and the Service can ract to that by sending a broadcast Intent containing the current state, which your Activity can listen for.

How to Stop service when app goes in background

I have a multi activity app. In the main activity a service is initiated that plays music. When I navigate through activities the music is still playing ( which is something that I want) but the music is still playing when I click home button and app goes in the background(which is something I don't want).
My first solution was to do 'stopService()' onPause of main activity but this prevented the music from playing in the other activities.
Tried the same in onStop method, same problem occurred.
Q: How can I stop the music(stop the service) from playing when the whole app goes in the background?
My service code:
public class MediaService extends Service {
private MediaPlayer player;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
player = MediaPlayer.create(this, R.raw.music);
player.setLooping(true);
player.start();
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
player.stop();
}
And I start/stop service with:
music_intent = new Intent(this, MediaService.class);
startService(music_intent);
stopService(music_intent);
P.S. Thanks for all the answers but as I said onStop methods stops the music when I change activities which is something that I don't want.
Found this solution in a similar question that uses Application.ActivityLifecycleCallbacks in Application class to check when app goes in Background and then send a Broadcast to the service to stop it.
More in: Stop MediaPlayer service when app goes in Background
According with the Activity lifecicle you should use onStop() to stop your audio from beign played
Remember that onStop()
May never be called, in low memory situations
where the system does not have enough memory to keep your activity's
process running after its onPause() method is called.
So it should solve your problem doing this in this way
#Override
public void onStop() {
super.onStop();
if(player.isPlaying()){
player.stop();
player.release();
}
}
use onStop() or onPause() to stop the service rather than onDestroy();
onStop() - when app goes backgroun(not visible)
onPause() - when any pop-up appears
Here is the actual answer, try this.
For some reason, the service is not stopping when the app goes to background
It stopped long before your stopService() call, as it stopped once onHandleIntent() returned, milliseconds after it was created.
What is not stopping is your Timer, which runs on a background thread and will continue running until you cancel it or your process terminates.
IMHO, this is an inappropriate use of IntentService. If you want to control the lifespan, use a Service, and stop background work in onDestroy().

Categories

Resources