2011-12-30 13 views
5

作成されたBroadcastReceiverは、アプリケーションのインストール/アンインストール時にアプリケーション名とバージョン番号を表示します。しかし、私はintent.getData()によってパッケージ名を取得しています。しかし、私がpackagemanagerを使ってそのアプリケーションの名前を見つけようとすると、インストール/アンインストール/置き換えのすべてのケースで例外がスローされます。考えられる問題は何か、またこれをどのように修正できるのでしょうか?アプリケーションのインストール/アンインストール時にアプリケーション名とバージョン番号を表示するBroadcastReceiverを作成しましたか?

CODE:私は次のようにBroadcastReceiverが導入さthis例に従っ

import android.content.BroadcastReceiver; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.content.pm.ApplicationInfo; 
    import android.content.pm.PackageManager; 
    import android.widget.Toast; 

public class ApplicationStatusNotification extends BroadcastReceiver { 

    /** 
    * This method receives message for any application status(Install/ Uninstall) and display details. 
    */ 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     // Get application status(Install/ Uninstall) 
     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_INSTALL")){ 
      // Application Install 
      toastMessage = "PACKAGE_INSTALL: "+ intent.getData().toString() + getApplicationName(context, intent.getData().toString(), PackageManager.GET_UNINSTALLED_PACKAGES); 
     }else if(intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")){ 
      // Application Uninstall 
      toastMessage = "PACKAGE_REMOVED: "+ intent.getData().toString() + getApplicationName(context, intent.getData().toString(), PackageManager.GET_UNINSTALLED_PACKAGES); 
     }else if(intent.getAction().equals("android.intent.action.PACKAGE_REPLACED")){ 
      // Application Replaced 
      toastMessage = "PACKAGE_REPLACED: "+ intent.getData().toString() + getApplicationName(context, intent.getData().toString(), PackageManager.GET_UNINSTALLED_PACKAGES); 
     } 

     //Display Toast Message 
     if(toastMessage != null){ 
      Toast.makeText(context, toastMessage, Toast.LENGTH_LONG).show(); 
     } 
    } 

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

     final PackageManager pckManager = context.getPackageManager(); 
     ApplicationInfo applicationInformation; 
     try { 
      applicationInformation = pckManager.getApplicationInfo(data, flag); 
     } catch (PackageManager.NameNotFoundException e) { 
      applicationInformation = null; 
     } 
     final String applicationName = (String) (applicationInformation != null ? pckManager.getApplicationLabel(applicationInformation) : "(unknown)"); 

     return applicationName; 
    } 
} 
+0

Intent.ACTION_PACKAGE_INSTALLをリッスンするのではなく、おそらく変更したいことが1つあります。Intent.ACTION_PACKAGE_ADDEDを探している可能性があります。最初に文書化されたものを使用してはいけません。あなたがそのような行動を取った場合、それは非常に驚くべきことです。 – harism

+0

はい、でもpckManager.getApplicationInfo(packageName、flag)がNameNotFoundException例外をスローしています。 –

+0

onReceiveメソッドLog.d( "MyApp"、 "Receive intent:" + intent)に追加できますか? logcatメッセージを追加しますか? – Yury

答えて

11

。 PackageChangeReceiver.onReceive(..)が呼び出された後

<receiver android:name="PackageChangeReceiver"> 
    <intent-filter> 
     <action android:name="android.intent.action.PACKAGE_ADDED"/> 
     <action android:name="android.intent.action.PACKAGE_REPLACED"/> 
     <action android:name="android.intent.action.PACKAGE_REMOVED"/> 
     <data android:scheme="package"/> 
    </intent-filter> 
</receiver> 

は今、Intent.getData()ウリは、周りの何かが含まれています。 Uri.toString()によって返されるpackage:my.test.package。 PackageManagerを使用してこのApplicationInfoを検索するには、Uri.getSchemeSpecificPart()で取得できるパッケージ名のみを抽出してください。my.test.packageが必要です。

また、クイックテストに基づいて、パッケージを削除した後もApplicationInfoは使用できなくなる可能性が高いようです。

+0

おかげで...アプリケーションがインストールされているが、アプリケーションがアンインストールされても同じ問題に直面しているときに機能します。 –

関連する問題