What functions or codes require GET_TASKS permission in Android? - android

I think the GET_TASKS permission is an orphan line in my AndroidManifest.xml. I want to remove it safely. Do you know any function or code that requires this permission? Thank you.
<uses-permission android:name="android.permission.GET_TASKS" />

From the android reference
Allows an application to get information about the currently or
recently running tasks.
An example is public List<ActivityManager.RecentTaskInfo> getRecentTasks (int maxNum, int flags) as it throws SecurityException if the caller does not hold the GET_TASKS permission.
Note that according to the documentation
This constant was deprecated in API level 21. No longer enforced.
and
As of LOLLIPOP, this method is no longer available to third party
applications: the introduction of document-centric recents means it
can leak personal information to the caller. For backwards
compatibility, it will still return a small subset of its data: at
least the caller's own tasks (though see getAppTasks() for the correct
supported way to retrieve that information), and possibly some other
tasks such as home that are known to not be sensitive.

class CheckRunningActivity extends Thread{
ActivityManager am = null;
Context context = null;
public CheckRunningActivity(Context con){
context = con;
am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
}
public void run(){
Looper.prepare();
while(true){
// Return a list of the tasks that are currently running,
// with the most recent being first and older ones after in order.
// Taken 1 inside getRunningTasks method means want to take only
// top activity from stack and forgot the olders.
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
String currentRunningActivityName = taskInfo.get(0).topActivity.getClassName();
if (currentRunningActivityName.equals("PACKAGE_NAME.ACTIVITY_NAME")) {
// show your activity here on top of PACKAGE_NAME.ACTIVITY_NAME
}
}
Looper.loop();
}
}

Related

How some apps still can get current apps processes and kill them?

Background
In the past, I've found the next method of killing an app's background processes, given its package name:
public static boolean killApp(final Context context, final String packageName) {
final ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
final List<ActivityManager.RunningAppProcessInfo> pids = am.getRunningAppProcesses();
for (int i = 0; i < pids.size(); i++) {
final ActivityManager.RunningAppProcessInfo info = pids.get(i);
if (info.processName.equals(packageName)) {
android.os.Process.killProcess(info.pid);
if (new File("/system/bin/kill").exists()) {
InputStream inputStream = null;
try {
inputStream = Runtime.getRuntime().exec("kill -9 " + info.pid).getInputStream();
final byte[] buffer = new byte[100];
inputStream.read(buffer);
} catch (final IOException e) {
}
StreamsUtil.closeStream(inputStream);
}
am.killBackgroundProcesses(packageName);
return true;
}
}
am.killBackgroundProcesses(packageName);
return false;
}
The problem
Ever since a specific Android version (5.1), the function to get the list of running processes only returns the current app's processes, so it's quite useless to use it.
What I've found
Most apps on the Play Store indeed fail to show a list of processes, and instead, show just the current app's process or a list of services at most.
It seems that some apps still manage to show background apps processes and even be able to kill them. As an example, I've found AVG's app that's capable of doing so, here .
Before they can do it, they tell the user to enable the usage stats settings for the app, which I remember of using for checking general information of apps launch time.
Another app that succeeded killing background processes, yet without any user confirmation , is "fast task killer". It also shows a toast of all processes being killed. I could be wrong, but it seems that it's always the same number of tasks.
I also think there is a relatively easy way to get the list of processes using the "ps" function, but only if the device is rooted (otherwise it will return just the current app's processes).
There was a temporary solution with a library, found here (published here), but this doesn't seem to work on Android 7.1.2 , and most probably on previous versions.
The question
How do apps get the list of apps that have background processes, and how do they kill them?
Is it possible to do so without using the UsageStatsManager class ?

GPU Version, Renderer, Vendor in Android

I want to find GPU Vendor, its version and renderer.
I have used this code:-
renderer = GLES10.glGetString(GL10.GL_RENDERER);
vendor = GLES10.glGetString(GL10.GL_VENDOR);
version = GLES10.glGetString(GL10.GL_VERSION);
works on Android 4.3 and above, but when I ran this code on Android 2.3.6 it returns null.
However, the check following code check the GPUVersion which is returning "2.0" with 2.3.6:-
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
GPUVersion = configurationInfo.getGlEsVersion();
but still the above 3 lines returns null.
Is there any way to get the above information for all the devices.
Sometimes the only way to get a value across different Android versions is to split methods depending on which version you are running. You can do something like:
public static String getGlVersion(Context ctx) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
ConfigurationInfo configurationInfo = am.getDeviceConfigurationInfo();
return configurationInfo.getGlEsVersion();
} else {
return GLES10.glGetString(GLES10.GL_VERSION);
}
}
Be aware, though, that this might not work for all versions and you might need to split the method into more cases or change the min SDK_INT used for the comparison (I suspect for example that for Honeycomb it already needs the first method, but have not tested it).

Kill another Background Application in android

Problem: I want to kill a background application process.
We call the below methods inside a background thread/services and it's not working.
We have tried a few methods available on net but not succeeding to kill the background process/application.
My Device has a root permisision already.
Code here
1st Method:
int value = findPIDbyPackageName("com.google.android.youtube");
android.os.Process.sendSignal(value, 9);
2nd Method:
ActivityManager activityManager = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses("com.google.android.youtube");
3rd Method:
ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
activityManager.restartPackage("com.google.android.youtube");
4th Method :
android.os.Process.killProcess(pid);
5th Method : `
List<ApplicationInfo> packages;
PackageManager pm;
pm = getPackageManager();
packages = pm.getInstalledApplications(0);
ActivityManager mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
for (ApplicationInfo packageInfo : packages) {
if((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM)==1)continue;
if(packageInfo.packageName.equals("mypackage")) continue; // here my package defines your application package entered in manifest
else if(packageInfo.packageName.equals("third Party application Package Name")) // if you dont have this package name then prefer playstore url of this app to get packagename
mActivityManager.killBackgroundProcesses(packageInfo.packageName);
}
6th
We launched a third party app like youtube, Subway surf from our own android app.
We are using startActivityForResultmethod for launching the app.
launchApp("com.imangi.templerun");
protected void launchApp(String packageName) {
mIntent = getPackageManager().getLaunchIntentForPackage(packageName);
if (mIntent != null) {
try {
startActivityForResult(mIntent, 101);
} catch (ActivityNotFoundException err) {
Toast t = Toast.makeText(this, "App not found", Toast.LENGTH_SHORT); t.show();
}
}
}
To close the application, the method finsihActivity(ResposneCode) is available.
But we're not able to use it in service.
You are not allowed to kill processes which don't belong to your app. The system will decide when to kill and what to kill when necessary.
App intervention is just to tell the system a message, Please help me to kill this process, blah blah..., that's all.
Process.killProcess() should work, this is your method number 4.
Read carefully what it says.
Kill the process with the given PID. Note that, though this API allows
us to request to kill any process based on its PID, the kernel will
still impose standard restrictions on which PIDs you are actually able
to kill. Typically this means only the process running the caller's
packages/application and any additional processes created by that app;
packages sharing a common UID will also be able to kill each other's
processes.
Under root permission you should be able to remove the kernel restrictions, unfortunately I do not know how to do this, I suggest you to search for native solution for that, may be some C codes, there are plenty of them in the net.
Also in your method 1 you been trying to send SIGNAL_KILL, try sending SIGNAL_QUIT

Listener for Phone Events and Voice command

My app uses a microphone(AudioRecord) in a background mode,How to make the listener for phone events and voice commands (like google voice). It is necessary for me to release a microphone(AudioRecord) for use.
I found a solution to phone events: http://www.botskool.com/geeks/how-listen-phone-events-android.
to use: TelephonyManager , PhoneStateListener.
But not to voice commands. Help pls.
There is no specific way you can do this (unfortunately) and it will only be the application that attempts to use the mic resource that will get an error.
What you can do is monitor what the user is doing in the background and react accordingly. Here is the code to check if Google Now has become the foreground application:
public static boolean googleNowForeground(final Context ctx) {
final ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
if (activityManager.getRunningTasks(1).get(0) != null) {
final PackageManager pm = ctx.getPackageManager();
try {
final PackageInfo foregroundAppPackageInfo = pm.getPackageInfo(
activityManager.getRunningTasks(1).get(0).topActivity.getPackageName(), 0);
if (foregroundAppPackageInfo != null) {
if (foregroundAppPackageInfo.packageName.matches(Constants.GOOGLE_NOW_PACKAGE_NAME)) {
return true;
}
}
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
return false;
}
This is the least 'expensive' way I've found to perform such actions, but the method above is not guaranteed to be called in future OS versions, not to mention having to hard-code the package names you are concerned with....
The 'expense' of monitoring for foreground applications that you know will conflict with your application may be somewhat minimal in comparison to permanently recording audio, but you should code your implementation wisely to minimise the monitor within certain device condition parameters.
I've investigated other methods to monitor intent broadcasts that are associated with mic resources, but they've been less successful than the above.
Requesting that the user create an 'exclude list' for the conflicting applications will allow you to dynamically monitor if they become the foreground application and react accordingly.
Hope that helps....

android listen for app launch

I need to develop a service which listen for every activity start.
Must I do something like this?
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();
for (int i = 0; i < runningAppProcessInfo.size(); i++) {
Log.v("Proc: ", runningAppProcessInfo.get(i).processName);
}
And do I need to do it every X seconds? Does it affect battery consumption?
As far as I know there is a class IActivityController.Stub in android.app package.
But this is an {#hide} interface (as someone said there have some method to access #hide api).
We can set a Listener to listen Activity switch like this:
mAm = ActivityManagerNative.getDefault();
try {
mAm.setActivityController(new ActivityController());
} catch (RemoteException e) {
System.err.println("** Failed talking with activity manager!");}
and Class ActivityManagerNative is #hide also.
ActivityController is a class extends IActivityController.Stub .
How to access #hide Api:
you can get the android source code to build an have-#hide-api Android.jar to use.
by reflection.
As far as I know there is currently no way to listen to an app's launch, Unless it is the first time that it is launching.
ACTION_PACKAGE_FIRST_LAUNCH (Broadcast Action: Sent to the installer package of an application when that application is first launched (that is the first time it is moved out of the stopped state).
So I guess your solution is the best for this right now.

Categories

Resources