get application name from package name - android

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);

Related

ApplicationInfo for Settings pages in android

I'm trying to get the ApplicationInfo for Settings.ACTION_ACCESSIBILITY_SETTINGS or Settings.ACTION_BATTERY_SAVER_SETTINGS, so that I can launch them directly.
I tried following steps but it's return setting's ApplicationInfo.
private static final String[] SETTINGS_ACTION_MENUS = new String[]{
Settings.ACTION_ACCESSIBILITY_SETTINGS,Settings.ACTION_ADD_ACCOUNT};
public void getSettingPages(){
for(String menu : SETTINGS_ACTION_MENUS){
final Intent intent = new Intent(menu);
ComponentName componentName = intent.resolveActivity(packageManager);
if(componentName != null){
try {
ApplicationInfo info = packageManager.getApplicationInfo(componentName.getPackageName(),0);
if(packageManager.getLaunchIntentForPackage(info.packageName) != null){
launchableApps.add(info);
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
}
}
I tried following steps but it's return setting's ApplicationInfo
That is because those Intent actions are tied to activities in the Settings app. Individual activities do not have their own ApplicationInfo.

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) : "???");

How to get topActivity by running task package name?

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));

Find Running activities from a Broadcast Receiver in Android

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
}
}
}
}

How to get all apps installed on android phone

This is the code that i have at the moment but i still can't get a list of all the apps on the phone. Does anyone see what i am doing wrong?
public class GetAppList extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
List<PackageInfo> appListInfo1 = this.getPackageManager()
.getInstalledPackages(0);
JSONArray ja = new JSONArray();
try {
HttpClient httpclient = new DefaultHttpClient();
Object sendDataUrl = null;
HttpPost request = new HttpPost(sendDataUrl.toString());
List<NameValuePair> params = new ArrayList<NameValuePair>();
ContextWrapper context = null;
PackageManager pm = context.getPackageManager();
List<PackageInfo> appListInfo = pm.getInstalledPackages(0);
for (PackageInfo p : appListInfo) {
if (p.applicationInfo.uid > 10000) {
JSONObject jo = new JSONObject();
jo.put("label", p.applicationInfo.loadLabel(pm).toString());
jo.put("packageName", p.applicationInfo.packageName);
ja.put(jo);
}
System.out.print(ja);
}
} catch (Exception e) {
// TODO: handle exception
}
finally {
// ... cleanup that will execute whether or not an error occurred ...
}
}catch (Exception e) {
// TODO: handle exception
}
finally {
// ... cleanup that will execute whether or not an error occurred ...
}
}}
Sorry cant get this to format the code properly.
This code logs name and package name of all the installed applications. You can easily check the log using LogCat (if you are using Eclipse).
Package name - name of the app's package.
Name - text label associated with the application. You can't use just appInfo.name as it will return null. Using appInfo.loadLabel(packageManager) you get the actual app's name like Speech Recorder instead of package name com.android.speechrecorder.
final PackageManager packageManager = getPackageManager();
List<ApplicationInfo> installedApplications =
packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo appInfo : installedApplications)
{
Log.d("OUTPUT", "Package name : " + appInfo.packageName);
Log.d("OUTPUT", "Name: " + appInfo.loadLabel(packageManager));
}
This is the code I use to list applications:
PackageManager pm = this.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
String activityName = rInfo.activityInfo.name;
List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
for (ResolveInfo rInfo : list) {
pkg = rInfo.activityInfo.applicationInfo.packageName;
if (pm.getLaunchIntentForPackage(pkg) == null) {
continue;
}
String label = rInfo.activityInfo.applicationInfo.loadLabel(pm).toString();
arrayList.add(new AppEntry(label, activityName, pkg, null));
}
If you later on want to run application only knowing pkg and activityName you can do:
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName(pkg, activityname);
ctx.startActivity(intent);

Categories

Resources