Is it possible to detect exit of an application? - java

My android application allows to launch other installed applications from this.This shows some allowed apps. If the user try to launch a disallowed application then show a message and go back to my activity (from where each application launch) using running tasks. My application act as a home launcher.So intent to this activity if the is a blocked application.For eg: It is possible to launch Camera from Gallery in Samsung device.If the camera is not an allowed one shows blocked message and exited to my acivity.But when relaunching Gallery the blocked message shows again beause the top activity(Camera activity) lied in the stack.
But the exiting of these blocked application did not work perfectly.
Is it possible to get close/exit event of an application?
How can i finish an application as whole(By finishing all its applications).
How to launch an application without having any history of previous launch?
Thanks in Advance

Is it possible to get close/exit event of an application?
Yes it is possible inside your LauncherActivity You can override onDestroy this method will be called on application exit.
How can i finish an application as whole(By finishing all its applications)?
I believe you want to stop your all running activities here. This can be achieved in multiple ways.
android.os.Process.killProcess(android.os.Process.myPid());
or
Intent intent = new Intent(getApplicationContext(), YourHomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will clear all the activities and will brings the user to the HomeActivity. While calling this you can add a flag in intent and using that value you can finish the HomeActivity also. Use finish() method to finish the activity.
How to launch an application without having any history of previous
launch?
You can use the same above Solution to achieve this. The second one.
Hope this will help.
There is an onTerminate method in application class, but this cannot be used in production environment. See more about this here

Related

how to launch app in hidden icon mode at home menu android

I have app that after installtion need to be hidden in home menu.
the app is for push notifications only.
I read this but android.intent.action.BOOT_COMPLETED not work.
there I read also Think Twice Code Once answer.
so currently I am searching about way to interact with user at least once after installation without activity or with activity in the way that app icon not appear in home menu. or change reciver or service state immediately after installtion without user interaction
any idea?
thanks!
The app needs to be run manually at least once by the user, otherwise the app will remain in the "stopped state" and you will not receive any broadcast Intents. You will need to have an Activity and that Activity will need to have an entry in the manifest with ACTION=MAIN and CATEGORY=LAUNCHER. What you could do is disable the Activity programmatically after it is launched the first time. This would prevent the Activity from being shown on the HOME screen. Here's an example:
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(new ComponentName(this, my.packagename.MainActivity.class),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
NOTE: If you do this, and the user "force stops" your app (Android settings->Apps->YourApp->Force Stop) then there will be no way to restart your app other than uninstalling and reinstalling it.

Foregound Service start action from MainActivity

I am working on an Android fall detection application. When user falls, alarm with timer turns on and if user not clik cancel within 15s, app send SMS to contact. Everything works fine when app is open but I don't know how my foreground service should work. Is it possible to make foreground service work like that- after fall detected foreground service open application and run timer from Main Activity?
Code from foreground service opening MainActivity:
Intent myIntent= new Intent(this, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(myIntent);
There are two obstacles to your use case:
Firstly, starting from Android 10, there are new restrictions on starting an activity when your app is in the background (and as it's written on the android developer page, even with a foreground service, your app will be considered in the background if no activities are in foreground). So you won't be able to "pop" a view to the user, except if you target a version < 29 (but it's not very recommended).
Secondly, since the beginning of 2019, the Google Play Developer Policy has been updated and you cannot use anymore the SEND_SMS permission, unless if you can justify in the publish console that your app is an SMS handler and you will need the user to register it as the default one. So you won't be able to send an automatic text message directly.
You can try to change the notification message to alert the user and try to have him click on the notification to open an activity.

Android Activities issue startActivity (i want only show)

I have an app that when it launches, starts the main activity, all its okay, this activity sends and listen to info from an XMPP Server. If I change activity this send/read doesn't stop but when I go back to main activity, ** I reconnect to Server. Its okay but the Server read Disconnect-Connect when I go back to main activity from any other activity.
My question is, how I can launch the main activity from other without Disconnect-Connect issue (?)** I think maybe exists a method like startActivity but without restart It (I don't use finish)... Only go back to activity showing it, but never stop or start It... Something like show(enable/disable) . The activity works and the second plane and never stop/restart. This activity has a lot of threads that cant be rebooting all time.
Why dont you make you client-server communication in Application class, so you always have the same instance, and you will be able to manually control lifecycle of communication?

Controlling activity instance visibility through a service?

I have an activity which can be launched from two sources. Each source launches it's own instance of the activity:
1. From inside an application
2. From an external service
My problem is with the service. The service needs to be able to control the visibility of the activity on the Android screen (hide and show the activity).
Currently I start the activity from the service with the following intent:
Intent appIntent = new Intent("Custom_Action");
appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
startActivity(appIntent);
My question is, how can I control the visibility of the activity. Maybe through binding the activity to the service?
I think you're misunderstanding how services and activities relate. A service runs in the background, doing it's thing. It has no UI and no way for the user to interact with it. The activity can send the service messages and query its state but the service has no way to push information to an activity. If you want the service to notify the user of something you can post a notification. Then have that notification launch your activity which will then connect to the service. There is no way to force open an activity from a service because there is no way to know if your user is even looking at their device at that time.

How to force an application to stay open?

I have seen apps like Lookout, JuiceDefender, and MagicJack run in the background indefinitely, unless force closed by a user directly through the task manager. (And even then, in Gingerbread, it wouldn't close unless you browsed to the application that was running under "Downloaded Apps" in the settings and force closed it once you were at the menu where you have options to manage the app like "Clear Memory" and "Force Close".
I am wondering how this is accomplished? I need to do something similar for an app of mine but I don't know how to avoid the Android OS's automatic task killing.. And don't say it's not possible because if that were true, JuiceDefender, MagicJack, and Lookout would not work.
What you can have is a service that stays alive indefinitely. You achieve that returning Service.START_STICKY on your Service's onStartCommand method.
Whenever the os needs resources and chooses to kill your app, your service will be respawned as soon as the resources are available again.
Bear in mind that having an application that is continuously alive will result in consuming the phone's battery. You should (at least) notify the user with a notification that your app is still alive in the background.
On top of that, you can register a broadcast receiver for the BOOT_COMPLETED event in order to restart your service while the device gets restarted. Yet, bear in mind that this could result in eating the phone's battery and so be careful on what you are doing in the service.
I believe these apps are launching a Service when their Activity get started (i.e when onCreate() is called).
A Service keeps running when the application get paused. When the Service is launched, you may return START_STICKY in your onStartCommand.
Also, to prevent a Service from being killed by Android's memory killer, you can specify that your Service is important to the user by calling startForeground(). Android Developers website states that :
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.
I am creating an app and I have to use one or more of the following super functions inside OnCreate():
onDestroy()
onPause()
onResume()
onSaveInstanceState()
to close an app completely from the memory. And also do not use Activity.finish() method. Usually Android does a pretty good job in closing the app when memory is needed, called pop out of stack and not recommended to forcefully stay in memory, unless there is a very very good reason to. Hope it helps.
You can also check the Android DOC website for more information and examples to your request.
You need to start a service. Services runs in background and is useful to push alerts.
This some links about it:
http://developer.android.com/guide/components/services.html
http://www.vogella.com/articles/AndroidServices/article.html
In the service onStartCommand method return "START_STICKY".
http://developer.android.com/guide/components/services.html
/Thomas

Categories