-2

when an app is being install in device and after success. I have to get that app package name and from package name i have to identify "App name". How can I do that

code:-

private String TAG = CAppReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (action.equals("android.intent.action.PACKAGE_ADDED")){
        Log.d(TAG,"App package::" + intent.getData().toString());
    }
}
Vishal
  • 67
  • 2
  • 8

4 Answers4

1
 String pakageName =getPackageName();
 String[] separated = pakageName.split(".");
 separated[0]; 
 separated[1];  

hear code for get application name ,first get package name and than split it to get application name.

Payal Sorathiya
  • 756
  • 1
  • 8
  • 23
0

In your manifest, add a receiver with the relevant intent service (inside application tag):

<receiver android:name=".apps.AppListener">
    <intent-filter android:priority="100">
         <action android:name="android.intent.action.PACKAGE_INSTALL"/>
         <data android:scheme="package"/> 
    </intent-filter>
</receiver>

According to google doc's:

PACKAGE_INSTALL Broadcast Action: A new application package has been installed on the device. The data contains the name of the package. Note that the newly installed package does not receive this broadcast.

Then use your onReceive code method.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class AppListener extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent arg1) {
    // TODO Auto-generated method stub
    Log.v(TAG, "there is a broadcast");
    }
}
Avi Levin
  • 1,868
  • 23
  • 32
0

Use the below Method to get the Application package name.

intent.getData().getSchemeSpecificPart()

and use below code to get other application specific information.

 try {
    ApplicationInfo app = this.getPackageManager().getApplicationInfo("com.example.name", 0);        

    Drawable icon = packageManager.getApplicationIcon(app);
    String name = packageManager.getApplicationLabel(app);
    return icon;
} catch (NameNotFoundException e) {
    Toast toast = Toast.makeText(this, "error in getting icon", Toast.LENGTH_SHORT);
    toast.show();
    e.printStackTrace();
}
User10001
  • 1,295
  • 2
  • 18
  • 30
0

You can get your your app name or application label if you have the package name. Here is how

PackageManager packageManager= getApplicationContext().getPackageManager();
String appName = (String) packageManager.getApplicationLabel(packageManager.getApplicationInfo("YourPackageName", PackageManager.GET_META_DATA));
Sandeep R
  • 2,284
  • 3
  • 25
  • 51