getApplicationInfo(String,int) in PackageManager has been deprecated - android

#SuppressLint("HardwareIds")
public void initDbx(){
try {
ApplicationInfo app = activity.getPackageManager().getApplicationInfo(activity.getPackageName(), PackageManager.GET_META_DATA);
dbx.game_id = String.valueOf(app.metaData.getInt("main_id"));
dbx.game_name = activity.getResources().getString(R.string.app_name);
dbx.package_name = activity.getApplication().getPackageName();
dbx.user_id = Settings.Secure.getString(activity.getContentResolver(), Settings.Secure.ANDROID_ID);
dbx.check_net = isConnectionAvailable();
//dbx.getUserInfos(activity);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
This method was deprecated in API level 33.
Use getApplicationInfo(java.lang.String, android.content.pm.PackageManager.ApplicationInfoFlags) instead.
..
Can someone rewrite the code in the updated format please?

Related

PackageManager throws NameNotFoundException for all apps even for my own app

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]

Android - check if Facebook app exists returns wrong result

I'm opening links received in notifications in FB native app if it's installed on the app and in the browser otherwise, using this code:
PackageManager packageManager = context.getPackageManager();
try {
packageManager.getPackageInfo("com.facebook.katana", PackageManager.GET_ACTIVITIES);
return context.getString(R.string.fb_app_prefix) + fb_url;
} catch (PackageManager.NameNotFoundException e) {
return context.getString(R.string.fb_site_prefix) + fb_url; //normal web mUrl
}
It works on most devices (including the emulator), but in some of them it doesn't throw an error although the app isn't installed.
What's wrong with my code?
I can add the following code for every link I have but not sure it's "healthy":
Intent testIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(fb_app_url));
if (testIntent.resolveActivity(packageManager) != null) {
return fb_app_url;
}
public static boolean isPackageExisted(Context c, String targetPackage) {
PackageManager pm = c.getPackageManager();
try {
PackageInfo info = pm.getPackageInfo(targetPackage,
PackageManager.GET_META_DATA);
} catch (NameNotFoundException e) {
return false;
}
return true;
}

non native android app - check if push notification disabled in application manager

I am working on an android web app. I am using PhoneGap Push Plugin to receive push notifications on device. I need to track if push notifications have been disabled for my app in the application manager settings. How do I achieve it?
Thanks.
You have to check in .java class whether the notification is enabled or no
I am posting reference java code for check whether notification is enabled or not
public class NotificationsUtils {
private static final String CHECK_OP_NO_THROW = "checkOpNoThrow";
private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
public static boolean isNotificationEnabled(Context context) {
AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
ApplicationInfo appInfo = context.getApplicationInfo();
String pkg = context.getApplicationContext().getPackageName();
int uid = appInfo.uid;
Class appOpsClass = null; /* Context.APP_OPS_MANAGER */
try {
appOpsClass = Class.forName(AppOpsManager.class.getName());
Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class);
Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
int value = (int)opPostNotificationValue.get(Integer.class);
return ((int)checkOpNoThrowMethod.invoke(mAppOps,value, uid, pkg) == AppOpsManager.MODE_ALLOWED);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return false;
}
}

How to get Application name from package name in android?

I am developing an app in which if any app is installed in device I have to update to server with app name for that I have to get Application name from package name.
for example :- from package let suppose :- <com.example.Deals> from this I have to get only <Deals>. How can I do that.
here is my code from which I got package name using Broadcast receiver.
public class Receiver 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 get Application name from package using following code
final PackageManager pm = getApplicationContext().getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo( "your_package_name", 0);
} catch (final NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
PackageManager packageManagers= getApplicationContext().getPackageManager();
try {
String appName = (String) packageManagers.getApplicationLabel(packageManagers.getApplicationInfo("com.example.packagename", PackageManager.GET_META_DATA));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
Here dont miss to change your package name com.example.packagename
check following code
PackageManager packageManager = context.getPackageManager();
ApplicationInfo applicationInfo;
try {
applicationInfo = packageManager.getApplicationInfo(packageName, 0);
} catch (final NameNotFoundException e) {}
final String title = (String)((applicationInfo != null) ? packageManager.getApplicationLabel(applicationInfo) : "???");

Android: How can I get current opened application name on screen [duplicate]

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.

Categories

Resources