1

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

}

2

3 Answers 3

6

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)");
Sign up to request clarification or add additional context in comments.

Comments

1
 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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.