I have an activity and one service. When i press the home button the activity paused and the service starts. I want, when i starts again my application to stops the service and my activity starts....Thanks.
Change your design.
There is no need to switch between playing the music in an Activity and in a Service. Instead you should keep playing in the Service, and continually update the Activity from there using for example BroadcastReceivers.
These broadcastreceivers should be registered in the activity's OnResume and unregistered in the activity's OnPause.
See Using a Service with MediaPlayer from the official android documentation
Stop the service in OnResume(). Start the service in OnPause().
If you don't want/can't change the design of your app, you must use this flag with your Intent in your service:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
If you don't want a new instance of your Activity, set launchMode for your Activity in the AndroidManifest file:
android:launchMode="singleTask"
Related
I have an activity which is called if the app receives a push notification. The activity is started with FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TOP. The activity, let's call it 'A' shows UI and finishes after a while. In this point have a problem with activity stack.
Scenario:
The app is in the background with another activity 'B'
Then the app receives a push notification and starts Activity A.
After related things done, the app finishes Activity A
Then returns to Activity B and stays in the foreground even the app was in the background before the push notification is received.
After debugging, I figured out that the system calls onResume method of Activity B after finishing Activity A.
How can I do the app keep staying in background if the app started from background? Should I change intent flags of the activity A?
In your case you can achieve this in two ways
1- From manifest file with activity tag android:noHistory="true"
2- From code when you are staring the activity set flags like below
Intent mIntent = new Intent(context, Youractivity.class);
mIntent.setFlags(mIntent.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(mIntent);
For more information checkout developers link
One other thing you can do is instead of this.finish() in notificationActivity is to use this.finishAffinity();. This will close the app instead coming to foreground.
When receiving a Firebase push notification, while the App is backgrounded, Firebase automatically displays a notification.
The pending Intent included in this notification seems to always include the FLAG_ACTIVITY_NEW_TASK flag, which will cause the App to be restarted when the notification is clicked, even if the App is already alive in the background.
Is there any way to prevent this behaviour and simply have onNewIntent(intent) on the main Activity invoked instead?
The launchMode attribute of the activity affects how the activity is launched.
see:
https://developer.android.com/guide/topics/manifest/activity-element.html#lmode
singleTop, singleTask, or singleInstance should be used to prevent the notification intent from creating a new activity instance.
The flag FLAG_ACTIVITY_NEW_TASK doesn't influence a new activity being created, but makes the launched activity the root of a new task.
see:
https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK
and:
https://developer.android.com/guide/components/tasks-and-back-stack.html
Hope this helps.
in AndroidManifest.xml set your Activity launchMode to singleTask should fix this:
<activity
......
android:launchMode="singleTask"
>
In my app I have a SIP calling feature.When the app is not opened and it is cleared from the system memory and it receives incoming call it opens the call activity from a SIP service. When the call is disconnected, I finish the activity.But the issue is, the activity is still in the memory (when home button is pressed).I want that activity to not be in the memory.Like whats app call behavior that don't keep activity in the memory when the app was killed i.e., not in the memory.
From SIP service I call it like this.
Intent intentIncomingCall = new Intent(getApplicationContext(), CallActivity.class);
intentIncomingCall.putExtra("DisplayName", callerDisplayName);
intentIncomingCall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentIncomingCall);
and on call disconnect, I just simple stop the service and call finish() on activity.
I have tried adding flags and exit(), things but nothing works.
android:allowTaskReparenting="false"
android:excludeFromRecents="true"
android:launchMode="singleTask"
android:permission="android.permission.USE_SIP"
android:screenOrientation="portrait"
android:taskAffinity=""
Try adding above properties to activity in manifest
Add line below finish() method :
finish();
System.exit(0);
when callClosed callback,
System.exit(0);
or
android.os.Process.killProcess(android.os.Process.myPid());
When a pending intent is sent, does it create a new activity? What if I have an activity already running? Is there a way to specify an already running activity, and have a method in that activity run once I send the intent?
What I want to do is have a button in a notification bar that acts a "stop" button, which will call the stop method in the already running application.
It depends on the Activity's declaration in the manifest, or the Intent flags that you include.
For example, if you use FLAG_ACTIVITY_SINGLE_TOP (or the activity has launchMode set to "singleTop") then onNewIntent() will be called on the existing activity instead of creating a new one.
In your example, you should pass an extra in the intent to indicate that you want to execute the "stop" action, then check for it in onNewIntent().
This is well explained in the official documentation about launch modes: http://developer.android.com/guide/components/tasks-and-back-stack.html#TaskLaunchModes
EDIT: However, since the ultimate objective was playing audio in background, using a Service is a more appropriate option. Check http://developer.android.com/guide/topics/media/mediaplayer.html#mpandservices
To control the service from the notification (i.e. play, pause, stop) you need to supply PendingIntents created from with startService().
I want to start multiple instance of the same Activity class from a Service. The reason I'm doing this, is because I have a Service that runs a "scan" daily, and if it finds any malfunctions it should display a popup for each malfunction.
The Activity that I'm starting is more like a Dialog, has a Dialog theme to display info about the malfunction.
Manfiest:
<activity
android:name=".ui.dialogs.MalfunctionActivity"
android:theme="#style/MyDialog"
android:launchMode="standard">
Intent to start the activity from Service:
Intent displayMalf=new Intent(this, MalfunctionActivity.class);
displayMalf.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(displayMalf);
PROBLEM: to start the Activity from a Service I need the FLAG_ACTIVITY_NEW_TASK which somehow cancels the launchMode="standard" from the manifest, and gives me just one Activity even if I try to start multiple diffrent instances.
Is there anyway in which I can achieve this?
It was so simple. There is the flag FLAG_ACTIVITY_MULTIPLE_TASK which according to the documentation :
Used in conjunction with FLAG_ACTIVITY_NEW_TASK to disable the behavior of bringing an existing task to the foreground. When set, a new task is always started to host the Activity for the Intent, regardless of whether there is already an existing task running the same thing.
Was exactly what I need. Thanks and sorry for answering on my question. It is not a habit. :)
Service will take the flag FLAG_ACTIVITY_NEW_TASK to start the activity but here you can try like this:
Set the instance of the handler of the activity of which you want multiple instances, in the service.
When you want the new instance of the activity use handler.sendMessage(msg) and on receiving this msg in your activity, start this activity again.
I guess your app works in the background and will display the popups even if the app is not in the foreground at the moment, right?
Otherwise I would use normal popup's (AlertViews) instead of starting new activities all the time.
If the app works in the background, you could tell the user with the first popup that your app has found one or more malfunctions and that he should activate the app for more details