I want to find from within my broadcast receiver what other activities are currently running. This is the code that i use from an activity to find the other running activites but when i try to use this code in my broadcast receiver i get errors on the following lines:
ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
shows the error in eclipse ACTIVITY_SERVICE cannot be resolved to a variable
PackageManager pm = this.getPackageManager();
and this shows the error in eclipse The method getPackageManager() is undefined for the type ScreenReceiver (my broadcast receiver)
Here is the full code:
public void getRunning(){
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));
Log.w("LABEL", c.toString());
runningApplications.add(c.toString());
}catch(Exception e) {
//Name Not Found Exception
}
}
}
Since BroadcastReceiver does not descent from a Context, you cannot use this, as you can do in Activity. You should use the instance of Context that is passed to your onReceive() method.
public void getRunning(Context context){
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = context.getPackageManager();
while(i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
Log.w("LABEL", c.toString());
runningApplications.add(c.toString());
}catch(Exception e) {
//Name Not Found Exception
}
}
}
try as:
public class ScreenReceiver extends BroadcastReceiver {
private Context ctext;
#Override
public void onReceive(Context context, Intent intent) {
ctext=context;
//OR you can also pass context as param to getRunning()
//your code here....
}
public void getRunning(){
ActivityManager am = (ActivityManager)ctext.getSystemService(Context.ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = ctext.getPackageManager();
while(i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
Log.w("LABEL", c.toString());
runningApplications.add(c.toString());
}catch(Exception e) {
//Name Not Found Exception
}
}
}
}
Related
I'm loading all apps from android and then I display them in my launcher (icon + name). Code looks like following:
public class PhoneAppItem
{
String mPackageName = null;
String mActivityName = null;
String mName = null;
ActivityInfo mActivityInfo = null;
public PhoneAppItem(String packageName, String activityName)
{
mPackageName = packageName;
mActivityName = activityName;
}
public void loadInfo()
{
PackageManager pm = MainApp.get().getPackageManager();
try
{
// following line throws the exception!
mActivityInfo = pm.getActivityInfo(new ComponentName(mPackageName, mActivityName), 0);
// some other code...
}
catch (PackageManager.NameNotFoundException e)
{
L.e(e);
mName = mPackageName;
}
catch (NullPointerException e)
{
L.e(e);
mName = mPackageName;
}
}
}
Package names and activity names for all my PhoneAppItem items are retrieved like following (for android < 5 which is relevant for the only yet known device having this issue):
List<PhoneAppItem> apps = new ArrayList<>();
ActivityManager activityManager = (ActivityManager) MainApp.get().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : processInfos)
{
try
{
ApplicationInfo ai = pm.getApplicationInfo(processInfo.processName, 0);
if (!excludedPackages.contains(ai.packageName))
apps.add(new PhoneAppItem(ai.packageName, ai.className));
}
catch (PackageManager.NameNotFoundException e) {}
}
I now have a user that get's following exception:
android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{com.package.myapp/com.package.myapp.app.MainApp}
at android.app.ApplicationPackageManager.getActivityInfo(ApplicationPackageManager.java:262)
at com.package.myap.classes.PhoneAppItem.h(PhoneAppItem.java:70)
...
Observations
the exception above is thrown for all apps, even for my own app
until now, this behaviour is unique to one user using following hardware: Lenovo Yoga Tablet HD+ (b8080), Android 4.4.2
Question
Does anyone have an idea why this could happen?
If processInfo.processName does not work, try another method of getting package:
processInfo.pkgList[0]
I have to develop an app locker for Android where the user can block apps and other users can not access these apps without an access key.
I have installed an app but I don't know how to lock this app.
Please suggest me something.
This is not how stack overflow works. You can not ask a complete solution without even trying anything.
For the most basic version of your app, you need to perform three functions.
Get a list of all the installed apps on device and show them in a ListView with check box. If the user checks any app, add the app to a different list say BlockedAppsList(which will be the list of apps which user wants to block).
You can get all the apps installed using the following code:
final PackageManager pm = getPackageManager();
//get a list of installed apps.
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
Log.d(TAG, "Installed package :" + packageInfo.packageName);
Log.d(TAG, "Source dir : " + packageInfo.sourceDir);
Log.d(TAG, "Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName));
}
Check the which is the current opened app. You can check by using this code:
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRecentTasks(1, ActivityManager.RECENT_WITH_EXCLUDED);
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));
Log.w("LABEL", c.toString());
} catch (Exception e) {
// Name Not FOund Exception
}
}
Now check if the current app is present in the BlockedAppsList, if it is there, you can show any screen with a block message.
good luck!
Assuming that you store the package names of locked apps in a table called:
locks
and you want to call an activity named
PasswordActivity
when ever the users launch a locked app,
you can implement a polling mechanism like below:
public class CheckAppLaunchThread extends Thread {
private Context context;
private Handler handler;
private ActivityManager actMan;
private int timer = 100;
public static final String TAG = "App Thread";
public static String lastUnlocked;
// private String lastUnlocked;
public CheckAppLaunchThread(Handler mainHandler, Context context) {
this.context = context;
this.handler = mainHandler;
actMan = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
this.setPriority(MAX_PRIORITY);
}
#Override
public void run() {
context.startService(new Intent(context, AppLockService.class));
Looper.prepare();
String prevTasks;
String recentTasks = "";
prevTasks = recentTasks;
Log.d("Thread", "Inside Thread");
while (true) {
try {
String topPackageName = "";
if(Build.VERSION.SDK_INT >= 21) {
UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService("usagestats");
long time = System.currentTimeMillis();
// We get usage stats for the last 10 seconds
List<UsageStats> stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000*5, time);
if(stats != null) {
SortedMap<Long,UsageStats> mySortedMap = new TreeMap<Long,UsageStats>();
for (UsageStats usageStats : stats) {
mySortedMap.put(usageStats.getLastTimeUsed(),usageStats);
}
if(mySortedMap != null && !mySortedMap.isEmpty()) {
topPackageName = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
}
}
}
else {
topPackageName = actMan.getRunningAppProcesses().get(0).processName;
}
recentTasks = topPackageName;
Thread.sleep(timer);
if (recentTasks.length()==0 || recentTasks.equals(
prevTasks)) {
} else {
if (isAppLocked(recentTasks)) {
Log.d(TAG, "Locked " + recentTasks);
handler.post(new RequestPassword(context, recentTasks));
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
prevTasks = recentTasks;
}
}
class ToastRunnable implements Runnable {
String message;
public ToastRunnable(String text) {
message = text;
}
#Override
public void run() {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
}
class RequestPassword implements Runnable {
private Context mContext;
private String pkgName;
public RequestPassword(Context mContext, String pkgName) {
this.mContext = mContext;
this.pkgName = pkgName;
}
#Override
public void run() {
Intent passwordAct = new Intent(context, PasswordActivity.class);
passwordAct.putExtra("PACKAGE_NAME", pkgName);
passwordAct.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
this.mContext.startActivity(passwordAct);
}
}
private boolean isAppLocked(String packageName) {
if (packageName.equals(PasswordActivity.lastUnlocked)) {
return false;
}
PasswordActivity.lastUnlocked = null;
DatabaseHelper dbHelper = new DatabaseHelper(context);
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM locks WHERE package_name=\'"
+ packageName + "\'", null);
boolean isLocked = false;
if (cursor.moveToNext()) {
isLocked = true;
}
cursor.close();
db.close();
dbHelper.close();
return isLocked;
}
}
Now you must call the above code from your service like this:
#Override
public void onCreate() {
handler = new Handler(getMainLooper());
context = getApplicationContext();
launchChecker = new CheckAppLaunchThread(handler, context);
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
while (true) {
if (!launchChecker.isAlive())
launchChecker.start();
return START_STICKY;
}
}
Warning: Since Oreo, google has restricted background services and you must figure out a way to always keep your service alive. (that is not the scope of this question) for a clue on this, consider scheduling a JobService and a broadcast receiver that would reschedule your service when ever the android kills it.
I'd like to load icon from a running task package name like this
com.android.smspush.WapPushManager
You can get top running application by following code : once you get applicationInfo, you can get icon from info.
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRecentTasks(1, ActivityManager.RECENT_WITH_EXCLUDED);
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));
Drawable ico = info.loadIcon(pm); // Icon of the application
Log.w("LABEL", c.toString());
} catch (Exception e) {
// Name Not FOund Exception
}
}
public class AppIconDrawable {
private HashMap<String, Drawable> drawables;
private static AppIconDrawable sharedInstance = null;
private AppIconDrawable(){
drawables = new HashMap<>();
}
public static AppIconDrawable getSharedInstance(){
if (sharedInstance == null)
sharedInstance = new AppIconDrawable();
return sharedInstance;
}
public void setDrawableForKey(String key, Drawable drawable){
drawables.put(key, drawable);
}
public Drawable getDrawableForKey(String key){
return drawables.get(key);
}
}
To save icon i used it, where rp.process = com.android.smspush.WapPushManager
PackageManager manager = getActivity().getPackageManager();
List<ActivityManager.RunningAppProcessInfo> listProcesses = manager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo info : listProcesses) {
try {
ApplicationInfo appinfo = manager.getApplicationInfo(info.processName, PackageManager.GET_META_DATA);
AppIconDrawable.getSharedInstance().setDrawableForKey(info.processName, manager.getApplicationIcon(appinfo));
} catch (PackageManager.NameNotFoundException e) {
AppIconDrawable.getSharedInstance().setDrawableForKey(info.processName, context.getResources().getDrawable(R.drawable.ic_android_default));
}
}
To get icon i used it, where rp.process = com.android.smspush.WapPushManager
holder.imgApp.setImageDrawable(AppIconDrawable.getSharedInstance().getDrawableForKey(rp.process));
This question already has answers here:
Get active Application name in Android
(3 answers)
Closed 6 months ago.
Please suggest me how can I get current open application name, even if there is home screen on device then I will find "Home screen is open".
Use an AccessibilityService
You can detect the currently active window by using an AccessibilityService.
In the onAccessibilityEvent callback, check for the TYPE_WINDOW_STATE_CHANGED event type to determine when the current window changes.
Check if the window is an activity by calling PackageManager.getActivityInfo().
I tested and working in Android 2.2 (API 8) through Android 7.1 (API 25).
public class MyAccessibilityService extends AccessibilityService {
#Override
protected void onServiceConnected() {
super.onServiceConnected();
//Configure these here for compatibility with API 13 and below.
AccessibilityServiceInfo config = new AccessibilityServiceInfo();
config.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
config.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
if (Build.VERSION.SDK_INT >= 16)
//Just in case this helps
config.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;
setServiceInfo(config);
}
#Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
Log.d("ABC-",accessibilityEvent.getPackageName()+" -- "+accessibilityEvent.getClassName());
if (accessibilityEvent.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
if (accessibilityEvent.getPackageName() != null && accessibilityEvent.getClassName() != null) {
ComponentName componentName = new ComponentName(
accessibilityEvent.getPackageName().toString(),
accessibilityEvent.getClassName().toString()
);
ActivityInfo activityInfo = tryGetActivity(componentName);
boolean isActivity = activityInfo != null;
if (isActivity)
Log.i("CurrentActivity", componentName.flattenToShortString());
}
}
}
private ActivityInfo tryGetActivity(ComponentName componentName) {
try {
return getPackageManager().getActivityInfo(componentName, 0);
} catch (PackageManager.NameNotFoundException e) {
return null;
}
}
#Override
public void onInterrupt() {
}
}
You can also list running tasks with the code below:
ActivityManager am = (ActivityManager) getApplicationContext().getSystemService(ACTIVITY_SERVICE);
List li = am.getRunningTasks(100);
Iterator i = li.iterator();
PackageManager pm = getApplicationContext().getPackageManager();
while (i.hasNext()) {
try {
ActivityManager.RunningTaskInfo info = (ActivityManager.RunningTaskInfo)(i.next());
String ac = info.baseActivity.getPackageName();
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(
ac, PackageManager.GET_META_DATA));
Log.v("asd", c.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
For me the above example did not work. So i ended up using this:
ActivityManager am = (ActivityManager) this
.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RecentTaskInfo> l = am.getRecentTasks(1,
ActivityManager.RECENT_WITH_EXCLUDED);
Iterator<ActivityManager.RecentTaskInfo> i = l.iterator();
PackageManager pm = this.getPackageManager();
while (i.hasNext()) {
try {
Intent intent = i.next().baseIntent;
List<ResolveInfo> list = pm.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(
list.get(0).activityInfo.packageName,
PackageManager.GET_META_DATA));
Toast.makeText(this, "Application name: " + c.toString(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this,
"Application name not found: " + e.toString(),
Toast.LENGTH_LONG).show();
}
}
With this you can get the current application name
Resources appR = getApplicationContext().getResources();
CharSequence txt = appR.getText(appR.getIdentifier("app_name","string", getApplicationContext().getPackageName()));
System.out.println(txt+" APp Name");
Simply using this code
getActivity().getApplicationInfo().packageName
It gives you application info, and then call the package name.
I want to get the application name from application package name. Somebody please show me how I can get this.
public class AppInstalledListener extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if(action.equals("android.intent.action.PACKAGE_ADDED")){
Logger.debug("DATA:"+intent.getData().toString());
}
if(action.equals("android.intent.action.PACKAGE_REMOVED")){
Logger.debug("DATA:"+intent.getData().toString());
}
if(action.equals("android.intent.action.PACKAGE_REPLACED")){
Logger.debug("DATA:"+intent.getData().toString());
}
}
}
You can use PackageManager class to obtain ApplicationInfo:
final PackageManager pm = getApplicationContext().getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo( this.getPackageName(), 0);
} catch (final NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
This would return the application name as defined in <application> tag of its manifest.
Try this
final String packageName = "my.application.package"
PackageManager packageManager= getApplicationContext().getPackageManager();
String appName = (String) packageManager.getApplicationLabel(packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA));
and replace packageName with your package full name.
you can get packageName using mContext.getPackageName() where mContext = yourActivityName.this for Activity and getActivity() for fragment.
public static String getAppNameFromPkgName(Context context, String Packagename) {
try {
PackageManager packageManager = context.getPackageManager();
ApplicationInfo info = packageManager.getApplicationInfo(Packagename, PackageManager.GET_META_DATA);
String appName = (String) packageManager.getApplicationLabel(info);
return appName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return "";
}
}
It seems that you are able to receive the event of new package added after that its a very simple concept to get the all relevant information about that package like one such information is the application name so here is the concept
-> your device package manager has all the information related to it so just make an object of that it will give you all the information related with the package name.
-> You should also remember that the intent gives you "package: real_package_name" so first you have to get real name first by spilling(I used) or by any other simple implementation of String
-> Here is the code hope you will get what you want I m also giving information about how you can get app name,app icon,app version,app version code etc.....
public class NewAppReciver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.intent.action.PACKAGE_ADDED")){
String[] a=intent.getData().toString().split(":");
String packageName=a[a.length-1];
List<PackageInfo> packageInfoList = context.getPackageManager().getInstalledPackages(0);
for (int i = 0; i < packageInfoList.size(); i++) {
PackageInfo packageInfo = packageInfoList.get(i);
if(packageInfo.packageName.equals(packageName)){
String appName = packageInfo.applicationInfo.loadLabel(context.getPackageManager()).toString();
String appVersion = packageInfo.versionName;
int appVerCode = packageInfo.versionCode;
Drawable app_icon = packageInfo.applicationInfo.loadIcon(context.getPackageManager());
}
}
}
}
}
But at the time of the application Uninstall you can only get the package name as on Un installation all other information gets removed by the system.
Try this below, it worked for me.
public static ArrayList<ApplicationInfo> getLaunchableInstalledApps(Context context){
final PackageManager pm = context.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ArrayList<ApplicationInfo> list = new ArrayList<>();
for (ResolveInfo resolveInfo:pm.queryIntentActivities(intent, PackageManager.GET_META_DATA)){
try {
ApplicationInfo app = context.getPackageManager().getApplicationInfo(resolveInfo.activityInfo.packageName, 0);
list.add(app);
} catch (Exception e) {
e.printStackTrace();
}
}
if (cacheAppsList.isEmpty()){
cacheAppsList.addAll(list);
}
return list;
}
PackageManager pm = getPackageManager();
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.MAIN");
filter.addCategory("android.intent.category.HOME");
filter.addCategory("android.intent.category.DEFAULT");
Context context = getApplicationContext();
ComponentName component = new ComponentName(context.getPackageName(), TestReplaceHomeAppActivity.class.getName());
ComponentName[] components = new ComponentName[] {new ComponentName("com.android.launcher", "com.android.launcher.Launcher"), component};
pm.clearPackagePreferredActivities("com.android.launcher");
pm.addPreferredActivity(filter, IntentFilter.MATCH_CATEGORY_EMPTY, components, component);