2016-04-15 6 views
0

Ich entwickle eine App, in der, wenn irgendeine neue APP in Gerät installieren wird, ich App-Namen vom Paketnamen dieser APP bekommen muss, wenn App für dieses richtig installiert ist, tue ich das unter Code. aber es zeigt immer (unbekannte) Nachricht an mich nicht App-Name.Bitte helfen Sie mir.Wie bekomme ich den App-Namen aus dem Paket Name in Android?

hier mein Code: -

public class Receiver extends BroadcastReceiver { 
    public Context context; 
    public String title; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // Get application status(Install/ Uninstall) 
     @SuppressWarnings("UnusedAssignment") boolean applicationStatus = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); 
     String toastMessage = null; 

     // Check if the application is install or uninstall and display the message accordingly 
     if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) { 
      // Application Install 
      toastMessage = "PACKAGE_INSTALL: " + intent.getData().toString() + getApplicationName(context, intent.getData().toString()); 
     } else if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) { 

      // Application Uninstall 
      toastMessage = "PACKAGE_REMOVED: " + intent.getData().toString() + getApplicationName(context, intent.getData().toString()); 
     } else if (intent.getAction().equals("android.intent.action.PACKAGE_REPLACED")) { 
      // Application Replaced 
      toastMessage = "PACKAGE_REPLACED: " + intent.getData().toString() + getApplicationName(context, intent.getData().toString()); 
     } 

     //Display Toast Message 
     if (toastMessage != null) { 
      Toast.makeText(context, toastMessage, Toast.LENGTH_LONG).show(); 
     } 
     System.out.println("App Name:" + title); 
    } 

    /** 
    * This method get application name name from application package name 
    */ 
    private String getApplicationName(Context context, String data) { 

     final PackageManager pckManager = context.getPackageManager(); 
     ApplicationInfo applicationInformation; 
     try { 
      applicationInformation = pckManager.getApplicationInfo(data, 0); 
     } catch (PackageManager.NameNotFoundException e) { 
      applicationInformation = null; 
     } 

     title = (String) (applicationInformation != null ? pckManager.getApplicationLabel(applicationInformation) : "(unknown)"); 
     return title; 

    } 
} 

Antwort

0

Need PackageManager

import android.content.pm.ApplicationInfo; 
import android.content.pm.PackageManager; 
import android.content.pm.PackageManager.NameNotFoundException; 

PackageManager pkManager = getPackageManager(); 
     ApplicationInfo appInfo; 
     String packageName=getApplicationContext().getApplicationInfo().packageName; 
     try { 
      appInfo = pkManager.getApplicationInfo(packageName, 0); 
      final String appname = (String)((appInfo != null) ? pkManager.getApplicationLabel(appInfo) : "???"); 
        Log.e("appname", appname); 
     } catch (final NameNotFoundException e) {} 
verwenden
Verwandte Themen