Android error with importanceReasonComponent.getShortClassName(); ? - android

I have :
String list[];
ActivityManager m = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> process = m.getRunningAppProcesses();
list = new String[process.size()];
for(int i=0;i<process.size();i++)
{
list[i]=process.get(i).importanceReasonComponent.getShortClassName();
}
if I try to run the app it force closes, please tell me what's the problem

Without looking at the logcat, we can't tell you what's wrong. That said, looking at the documentation, importance is only set for Services and ContentProviders.
You should check that process.get(i).importanceReasonCode != RunningAppProcessInfo.REASON_UNKNOWN before trying to get the component (which is probably null if the reason is REASON_UNKNOWN).

Related

How to stop opening default apps in android?

I am trying to make an android app for users in which user can activate Child protection. for example, User opens my app, he can view all the apps he has
(including default ones). Now if the user blocks default message or calls app then everything should be same, just these selected app must not open, even after anyone tap on these applications multiple time. It should look like nothing happening. I don't want to have a privacy PIN code or pattern on that app. I just want to stop the children by opening any app of mobile selected by the user through my application. Is this possible? if yes then any idea about how can i achieve this thing?
This problem is very similar to the one answered here, that will be my source for this answer. I can't try this code, but I hope it can give you a rough idea to make your first try and see yourself if it work or not.
The idea
The basic idea is to create a Background Service that continuously checks if there are open apps. If those apps are on the list the user locked, the show a lock screen. I don't know if it is possible to close an opened app¹ (I hope it's not!), but for your parental control functionality, a lock screen should have the same effect.
Select the apps to lock.
To retrieve from the system all apps that are installed
PackageManager packageManager = getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> appList = packageManager.queryIntentActivities(mainIntent, 0);
Collections.sort(appList, new ResolveInfo.DisplayNameComparator(packageManager));
List<PackageInfo> packs = packageManager.getInstalledPackages(0);
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
ApplicationInfo a = p.applicationInfo;
/* Uncomment this to exclude system apps
if ((a.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
continue;
} */
appList.add(p.packageName);
}
Now appList is a List<ResolveInfo>, and you can show to the user a ListView to let her choose the apps to be locked. Save these somewhere (SharedPreferences is an idea).
The service
What the service has to do is to:
Check the currently opened app:
ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1);
ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
activityOnTop = ar.topActivity.getClassName();
Check if activityOnTop is in the locked-apps list.
for(String appName : appList) {
if(appName.equals(activityOnTop)) {
// This app is locked!
LockApp(); // Defined later
break;
}
}
If so, start an Intent to your LockScreen activity (that won't allow any action)
private void LockApp() {
Intent lockIntent = new Intent(mContext, LockScreen.class);
lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(lockIntent);
}
I give a lot of things for granted: you have to start a Service somewhere, you have to build a couple of activities, and this process does not avoid the problem of a smart kid that closes the process in background ... But I think this can work as a starting point.
¹ It apparently is possible. Give a look to this answer, an idea is to substitute the code in LockApp() with something like that.
This doesn't change that I think a Lock Screen is more elegant.

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 ?

Android application info based on it's process name

I'm trying to find some info about apps I find using shell's top command. All I have is a process name (containing package name). Icon and app name would be perfect. I can't find any suitable soultion via google. Any help would be aprreciated;)
To be preemptive, I use top because it's the only way I found to show current processor usage. If someone's familiar with some more API friendly soultion, I'd be grateful.
Example process names I get are:
com.android.deskclock for desktop clock
com.creativemobile.DragRacing for game Drag Racing
Here how you get details. Since you have the package name, you can use it to get the corresponding application name, version, and icon.
List<PackageInfo> packagess = getPackageManager().getInstalledPackages(0);
for(int i=0;i<packagess.size();i++) {
PackageInfo pack = packagess.get(i);
if ((!getSysPackages) && (pack.versionName == null)) {
continue ;
}
//this is the application name
pack.applicationInfo.loadLabel(getPackageManager()).toString();
//this is the package name
pack.packageName;
//this is the version name
pack.versionName;
//this is the version code
pack.versionCode;
//this is the application icon
pack.applicationInfo.loadIcon(getPackageManager());
}
You can try out below code :
private String getTopActivity() {
ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1);
ActivityManager.RunningTaskInfo ar = RunningTask.get(30);
return ar.topActivity.getClassName().toString();
}
you have to give the permission of get task in the AndroidManifest.xml

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.

Monitor Currently Running Application

I am encountering a problem that i can't not solve for the moment.
The purpose of the code is to monitor which applications are running at current moment.
I used the following code and logged the resulting package name, it worked.
ActivityManager am = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
String packageName = am.getRunningTasks(1).get(0).topActivity.getPackageName();
Log.i("TTWYMonitor", packageName);
But I use that code in a BroadcastReceiver, nothing happened.
In manifest,I declared an intent receiver android:name=".MonitorApplication.
What should I do, then?
Please give any suggestion.
Yahel : Thanks and sorry for my informal question.
replace the "Activity" in getSystemService's parameters with "Context":
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
I tested it and works fine for me!

Categories

Resources