How to profile the App uses in Android? - android

I want to collect App activation sequence in an Android system.
E.g.
If a user first open Youtube App, then switch to Gmail App, then switch back to Youtube App and so on, then the sequence is like:
Youtube Gmail Youtube ...
Is there available App existing in Google Play or somewhere else to achieve this?
Is it straightforward to implement? Is it possible to achieve the goal with pure App solution?
Is it require rooting the device?

You need to poll this function on some regular interval.
You can further write a logic to remove subsequently duplicate Application Label.
private string getActiveApplicationLabel(){
String appLabel = null;
ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = this.getPackageManager();
while(i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName,
PackageManager.GET_META_DATA));
appLabel = c.toString();
}catch(Exception e) {
//Name Not FOund Exception
}
}
return appLabel;
}

Related

Receive other applications' usage time data

I'm working on an android project. I need to receive other applications' usage time. I simply just need how many application is running and what are they. How can I access these kinds of data?
I found some articles about receiving another's SQLite DB but I really don't need this.
You may follow the steps:
Retrieve the list of the tasks that are currently running by calling
getRunningTasks()
Go through each of the task and retrieve the PackageName
Get AppName from the PackagName
Add the AppName in a list
Here is the implementation:
private List<String> getRunningAppList()
{
Set<String> appNameList = new HashSet<>();
PackageManager pm = getPackageManager();
ActivityManager aManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
// Get the list of the tasks that are currently running
List<ActivityManager.RunningTaskInfo> infoList = aManager.getRunningTasks(Integer.MAX_VALUE);
for (ActivityManager.RunningTaskInfo taskInfo : infoList)
{
// Get the packageNAme
String pkgName = taskInfo.topActivity.getPackageName();
String appName = null;
// Get the appName
try
{
appName = pm.getApplicationLabel(pm.getApplicationInfo(pkgName,PackageManager.GET_META_DATA)).toString();
} catch (PackageManager.NameNotFoundException e)
{
e.printStackTrace();
}
// Adding the appName
appNameList.add(appName);
}
return new ArrayList<>(appNameList);
}
Using UsageStats, UsageStatsManager is simply the best approach for this topic.
https://developer.android.com/reference/android/app/usage/UsageStats
UsageStats.getLastTimeUsed() informs you about a specific application's last usage time by date format.

How to detect if any application requests the microphone in Android?

I'm developing an application that needs to know when other third party app requests the microphone.
Is there any way to detect it?
Thanks in advance!
After some research, it seems to be impossible to know when another third party application requests the MICROPHONE due to security reasons.
If you want to perform actions according to actual state of the media recorder, you may have to handle that as explained in this post:
How to detect if MediaRecorder is used by another application?
And about detecting other apps permissions access, I think this can provide an answer: How to detect permission access of other apps?
But if you want to check apps that needs RECORD_AUDIO permission, you can continue reading the following lines:
Based on the answer of Kopi-B at this question
To get the list of apps requesting microphone you can then proceed as follows:
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = getPackageManager().queryIntentActivities(mainIntent, 0);
//create a package names list
List<String> packageNames=new ArrayList<>();
for (Object obj : pkgAppsList) {
ResolveInfo resolveInfo = (ResolveInfo) obj;
PackageInfo packageInfo = null;
try {
packageInfo = getPackageManager().getPackageInfo(resolveInfo.activityInfo.packageName, PackageManager.GET_PERMISSIONS);
} catch (PackageManager.NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[] requestedPermissions = packageInfo.requestedPermissions;
//check the microphone permission
if (requestedPermissions!=null) {
for (String packagePermission : requestedPermissions) {
if (packagePermission == Manifest.permission.RECORD_AUDIO) {
packageNames.add(packageInfo.packageName);
break;
}
}
}
}

Starting application in another Android smartphone over bluetooth

As the title suggests, I am developing an app, which can connect and pair with another Android phone. I can successfully scan, connect, pair and transmit String data over bluetooth. I tested by sending String data and received it in another android smartphone and showed it in the toast.
The actual scenario
This application will be installed in both android phones. Once another android phone scans and connect to the phone over bluetooth, then I want to launch this app. I know we can accomplish this using BroadcastReceiver. But how can I implement this in terms of Bluetooth connection. Any code snippet or idea or suggestions are welcomed..
After you get the BT msg,Hope you followed this link ,Here after "// Send the obtained bytes to the UI activity"
Send a broadcast to start your app.
Loop with the list of apps installed and know which app to be launched and use the below to launch a app.
public String getPreInstalledPocPkgName(){
String pkgName = null;
try {
List<PackageInfo> pkgInfoList = mContext.getPackageManager().getInstalledPackages(0);
for(PackageInfo pkgInfo:pkgInfoList){
pkgName = pkgInfo.packageName;
if(pkgName==null || pkgName.equals("")){
continue;
}
for(String name:pttPkgList){
if(pkgName.equals(name)){
Log.d(TAG, "------------ app with package name ["+pkgName+"] already installed----------");
return pkgName;
}
}
}
} catch (Exception e) {
Log.e(TAG, "-------Error! while fetching previously installed app------", e);
clearConfigFromSharedPath();
}
Log.d(TAG, "------------No previously installed app----------");
return null;
}
}
String pttAppPkgName = getPreInstalledPocPkgName();
Or
pttAppPkgName = your app package.
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage(pttAppPkgName);
startActivity(intent);

How can I determine permission of Running apps programmatically in android?

I want to see the permission of running apps of android in my software.
For this reason ,I have the following code :
List<App> apps = new ArrayList<App>();
ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
PackageManager packageManager = getPackageManager();
List<RunningAppProcessInfo> l = am.getRunningAppProcesses();
Iterator<RunningAppProcessInfo> i = l.iterator();
PackageManager pm = this.getPackageManager();
int row_count = 0 ;
while(i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try
{
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
App app = new App();
app.setTitle(c.toString());
app.setPackageName(l.get(row_count).processName);
PackageInfo packageInfo = packageManager.getPackageInfo(l.get(row_count).processName, PackageManager.GET_PERMISSIONS);
String[] reqPermission= packageInfo.requestedPermissions;
app.set_Permission_Info(reqPermission);
// app.setVersionName(p.versionName);
// app.setVersionCode(p.versionCode);
// CharSequence description = p.applicationInfo.loadDescription(packageManager);
// app.setDescription(description != null ? description.toString() : "");
row_count++;
// app.setSize(p.s)
apps.add(app);
}
catch(Exception e){}
But there is a problem.
When I run my apps ,I find that the app name and app's package name are not consistent . Why has this problem introduced?
The main problem is described follow:
Let us suppose an apps named "EBOOK_Reader" and "Camera" is running in my device . The package name is "com.a.b" and "com.c.d" respectively. The problem of this code is the appropriate package name is not with appropriate apps name .
It shows the package name Of "com.a.b" to "Camera " and "com.c.d" to "EBOOK_Reader" which is not desired .
Any idea of how can the problem be solved?
ThankYou
This is correct and Running:
PackageManager mPm = getPackageManager();
List <PackageInfo> appList=mPm.getInstalledPackages(PackageManager.GET_PERMISSIONS|PackageManager.GET_RECEIVERS|
PackageManager.GET_SERVICES|PackageManager.GET_PROVIDERS);
for (PackageInfo pi : appList) {
System.out.println("Process Name: "+pi);
// Do not add System Packages
if ((pi.requestedPermissions == null || pi.packageName.equals("android")) ||
(pi.applicationInfo != null && (pi.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0))
continue;
for (String permission : pi.requestedPermissions) {
//Map<String, String> curChildMap = new HashMap<String, String>();
//System.out.println("############ "+permission);
try {
PermissionInfo pinfo = mPm.getPermissionInfo(permission, PackageManager.GET_META_DATA);
CharSequence label = pinfo.loadLabel(mPm);
CharSequence desc = pinfo.loadDescription(mPm);
System.out.println("$$$$$ "+label+"!!!!!! "+desc);
} catch (NameNotFoundException e) {
Log.i(TAG, "Ignoring unknown permission " + permission);
continue;
}
}
}
The app name and app's package name are normally different. You better use the package name as this is unique throughout the device.
Update:
Now I understand your problem. Thanks for clarifying. It is because of the variable row_count. Basically you're are using two different iterator variables. That's why your getting 2 different results. You don't need row_count because you already have interator for i.
Try the updated code below:
Basically l.get(row_count).processName was replaced by info.processName.
List<App> apps = new ArrayList<App>();
ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
PackageManager packageManager = getPackageManager();
List<RunningAppProcessInfo> l = am.getRunningAppProcesses();
Iterator<RunningAppProcessInfo> i = l.iterator();
PackageManager pm = this.getPackageManager();
// int row_count = 0 ; // no need for this. feel free to delete
while(i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try
{
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
App app = new App();
app.setTitle(c.toString());
app.setPackageName(info.processName);
PackageInfo packageInfo = packageManager.getPackageInfo(info.processName, PackageManager.GET_PERMISSIONS);
String[] reqPermission= packageInfo.requestedPermissions;
app.set_Permission_Info(reqPermission);
// app.setVersionName(p.versionName);
// app.setVersionCode(p.versionCode);
// CharSequence description = p.applicationInfo.loadDescription(packageManager);
// app.setDescription(description != null ? description.toString() : "");
//row_count++; // no need for this. feel free to delete
// app.setSize(p.s)
apps.add(app);
}
catch(Exception e){}
Process names are not bound to the application package name. They happen to be the same by default, as a convenience. However, each app is free to change its process name in its manifest using the android:process attribute, or to spawn more processes with different names for various components.
And in even more advanced scenarios, multiple applications can share the same process.
In particular, what this means is you can't use the process name to get the application(s) that are running currently. You should instead iterate over the list of packages that are loaded in that process using the RunningAppProcessInfo.pkgList field instead. Keep in mind that it is an array, and can contain more than one application package name. (See the note about the advanced scenarios above)
On a separate note, as the documentation for the getRunningAppProcesses() states:
Note: this method is only intended for debugging or building a user-facing process management UI.

Get Current Running Process in Android

It's about Currently Running Process Info.
I would like to capture the vivid name or the same name which appeared under icon of the current running process.
While using RunningTaskInfo or RunningProcessInfo , I can only get the complex information like com.android.browser.
Actually, I want to get the names like Brower, Clock, Contact etc.
How can I achieve this? Please direct to the full tutorial link or with fully explanation codes.
PS: I am just a beginner and I am sure that I've already explored the Android Developer site.
Get the current RunningAppProcessInfo from pid
public static String getCurrentProcessName(Context context) {
// Log.d(TAG, "getCurrentProcessName");
int pid = android.os.Process.myPid();
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses())
{
// Log.d(TAG, processInfo.processName);
if (processInfo.pid == pid)
return processInfo.processName;
}
return "";
}
Once you get the package name (such as com.android.browser), simply use that name with PackageManager:
PackageManager pm = context.getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo(name, 0);
CharSequence name = pm.getApplicationLabel(ai);
You'll need to handle some exceptions, but you will get the real app names from this.

Categories

Resources