I have some code that will launch a different application using intents but what can I do to close or kill the other app?
Here is the launch code (works great):
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setComponent(
new ComponentName(resolveInfo.activityInfo.applicationInfo.packageName,
resolveInfo.activityInfo.name));
I tried to kill the background processes but no luck:
ActivityManager activityManager = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses("com.pandora.android");
I also tried this to kill it:
context.stopService(new Intent(context, Class.forName("com.bla.bla")));
Update:
I want to kill other applications because I launch other applications and users have requested this additional feature (auto kill is a natural extension of auto launch). Please answer the question of how to accomplish this in code. I know about Advanced Task Mgr so it is possible but how?
I ended up finding the real answer to this question. Please see Automate closing of applications in Android for the solution!
but what can I do to close or kill the other app?
You don't. Your user can exit that "other app" when they wish to.
Basic answer, you can't. As of Froyo, the only facility available is ActivityManager.killBackgroundProcess(), which:
(a) Only allows you to kill processes that are, as it said, in the background and not visible to the user. That is, it does not allow you to disrupt the normal execution of the other app.
(b) Is not needed anyway, because Android will kill the background processes for you if it needs their memory.
You simply do not need to do what you at this point seem to think you do. "Because my users have asked for it" is not a reason to be going around killing other people's processes.
The only proper way to kill another "App" would be to send some kind of Intent to the app (ex. Pandora) or it's service that will ask it to exit.
Task killers use facilities outside of the normal means of Android like Process.killProcess() that talks to the Linux Kernel and not Android. The problem with that approach is it does not tell Android to close the program, but instead the Linux Kernel directly. If a background service is killed, for example, without calling Context.stopService(), Android may restart it later automatically. Also, killProcess doesn't just close the Activity, it kills all the components and doesn't allow them to shutdown normally.
If you started Pandora with Activity.startActivityForResult() you could try closing it with Activity.finishActivity(), but there's no guarantee that it will work the way you want it to.
You can use Process.killProcess()? you need to know the PID. To get the PID, you can exec "ps", but that's not going to be robust as the text output format could be different across devices or releases. You could provide your own native library to gather the process names and IDs into a standard format for your own purpose.
Related
I am a beginner in android development. I want to know that is it possible to know the app which is opened currently. I came to know that finding the apps which are running currently through Activity Manager (getRunningTasks()) is now removed from the Android studio. So I want to know is there any other way to know? I just want to know the app which is opened and running currently on the mobile but not the apps running in the background Could somebody please help me in this case?
You can use AccessibilityService to get event notification. But AccessibilityService are specifically for accessibility uses. If you use the service for other purposes, then the application will more likely to be downed/removed for PlayStore due to policies. Another viable option is UsageStatsManager, but with some limitations.
UsageStatsManager is not push event based system. You have to poll in few mills(depends upon the use-case)
Usage Access Permission grant/deny is not straight forward. You have to start Setting Activity with Settings#ACTION_USAGE_ACCESS_SETTINGS action and have to rely on the users understanding of how to grant permission(Since permission list may contain other applications).
I have to work on a project Where I have to upload users' locations every 15 minutes. For that, I searched a lot and found Recurring work with PeriodicWorkRequest. But the problem is that the WorkManager might not work when the app is closed/killed per the answer given here. Then I found about Service in android.
So I want to know If I want to send users' locations every 15 min even when the app is killed then how to approach this?
If an application is Force Stopped, the OS cancel all the Job related to that application. This is not a WorkManager only problem. The OS interprets a Force Stop as an user request to the OS that they don't want this application to run anymore.
Even if you use JobScheduler or a Service, the application is gone. But a force stop should be a user decision.
Some OEMs have implemented in the past some changes to the Android OS so that a swipe out of an application from the launcher was interpreted as a force stop with all the negative effects on scheduled jobs. This is where the problems start.
WorkManager is this case has implemented some mitigation, but the application cannot do anything if it is force stopped till the user launch it again.
If have a problem with a specific OEMs, please open an issue on the Android issuetracker as this maybe a CDD violation. Google can contact the OEM and request that they fix the ROM. This is going to take time, in the meanwhile, you can take a look at sites like don't kill my app to understand what are the constraints on a specific device and use a library like autostarter to help the user to navigate to the right setting.
I spend 1 month searching about this theme.
How to kill processes of other apps in android.
All solutions that I found not solved my problem:
Preclude a user start a app that not is in my list.
I try:
"killBackgroundProcesses(packageName);"
and
"forceStopPackage.invoke(am, process.processName);"
But nothing happens. So, how can I put in background or end the activity? I'm developing to 4.4.x android version. Any comment will be welcome. Hugs.
[#Edit]
I know: to kill processes in android is a bad idea. So what I do to blocking my users of see others apps?
To be able to kill other applications from your application has to be a "root application" or a system application, meaning that you get special privileges. (Without special privileges, you can only kill a process that has the same userID as the one that is doing the killing. If you are trying to kill your own process it should work.)
Here is a site with details on permissions: http://developer.android.com/guide/topics/security/permissions.html
Most third party apps do not get special privileges.
If everyone were allowed to kill each other's applications, it would be chaotic.
Quote:
" Android has no mechanism for granting permissions dynamically (at run-time) because it complicates the user experience to the detriment of security."
Before I begin my question, I want to preface this with: I KNOW IT'S A BAD IDEA TO FORCE A SERVICE TO RUN FOREVER... I simply do not care.
This application is not for consumer use. It is not going on the app store. It is for my use only.
Alright, well I have this unused HTC Sensation running 4.0.3 (ICS) sitting around, and I have volunteered it to a local theatre for a task. The task is for it to ring on cue whenever it is needed in the show. We don't want a sim card in it because someone might accidentally call the phone during the show when it is not supposed to ring.
So I created a fake phone application that receives a signal via TCP from a server that I have set up to send signals to devices over the LAN. Right now I have the listener running in an infinite loop in a service. I am, however, still experiencing the service not responding to the TCP signals.
I would really appreciated it if some android guru's could give me some hints/tips for making this service as reliable as possible, good/bad coding techniques aside I want to do everything possible to make this service unkillable. This phone has only one job now, and that is to always be listening for incoming messages, no matter what.
Things I have done so far:
Created a Service (and launched a separate thread from that service)
Used startForeground(id, notification);
Activated DeviceAdmin and created a wakelock
anything else you guys can think of?
First idea that pops into my mind would be setting up an AlarmManager that checks every 5 seconds whether or not the service is running.
This describes a method to see whether a service is running.
And if the service is not running you can just restart it.
Using this and the startForeground()-method may work.
Kind regards
There is no way to ensure that Android will never kill a service. If you make it a foreground service it reduces the odds, but you can't insure it.
Based on this quote from the Service javadoc...
Other application components running in the same process as the service (such as an Activity) can, of course, increase the importance of the overall process beyond just the importance of the service itself.
... if you leave the activity that starts your service in the foreground, that will pretty much guarantee that the Service won't be stopped [I posted a comment starting to suggest this idea that may have been cut off.]
To add another level of reliability, hold a wake lock in your activity, using this doc as a guide:
https://developer.android.com/reference/android/os/PowerManager.WakeLock.html
Example code:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
wl.acquire();
// ..screen will stay on during this section..
wl.release();
is it somehow possible to show all broadcast events/intents that are triggered in android? I just want to know if I can quickly figure out if an application is using intents/broadcasts I can hook into. For most of the stock android applications I can read the source but is time consuming.
List all historical broadcasts and registered broadcast receivers with the following terminal command:
$ adb shell dumpsys activity broadcasts
Please check this open source project to see all broadcasts on your android phone:Broadcasts Monitor Pro
There's logcat, which you can get to from the ddms (either the dedicated app, or the eclipse view), it should list all broadcasts, as well as the properties that are associated with the intent. However, as the other commenters on your post have stated, you really shouldn't be doing that unless the intent has been published. It may change, or cause other forms of havoc. Although I disagree with the statement, some people may even go so far as to say that it's illegal if the licenses aren't compatible. (Although I'm of the party that it's not, seeing as you're not statically linking to the code, nor even putting it in your package).
The best tool I found was Intent Intercept that catches the intent on the fly and allow you to inspect the content