2016-04-15 7 views
1

最後にインストールされたアプリから共通名、パッケージ名、アイコンを取得できるコードセクションを作成しようとしています。電話。現在私が持っているのは、このソース(get application name from package name)からパッケージ名と共通名を取得する方法ですが、これは私のためには機能しません。BroadcastReceiverを使用してLAST INSTALLEDアプリからパッケージ名、共通名、アイコンを取得する

最初のエラーは「メソッドgetApplicationContextおよびgetPackageNameを解決できません」です。これは意味があります。なぜなら、これらのメソッドは "Activity"に固有であり、 "BroadcastReceiver"ではなく(他の人がどのように動作するのかわかりません)。

私はプライベートコンテキストを作成して、getApplicationContextとgetPackageNameを使用できるようにしました。私はエラーで自分の携帯電話に別のアプリをインストールするときに私のコードは、今、私は以下の投稿したもののように見えるのGradleビルドが、私のアプリがクラッシュ:

can't instantiate class com.example.natalievold.applistener.NewInstallReceiver; no empty constructor 

私は "を削除することによって、このエラーを解決できることを読みます私が追加した「私的なコンテキスト」セクションですが、getApplicationContextとgetPackageNameを使用する必要があります。誰かが私がこれを行うことができる別の方法を知っていますか?私は最後にインストールされたアプリのアイコンをつかむ方法も不明です。

public class NewInstallReceiver extends BroadcastReceiver 
{ 

private Context mContext; 

public NewInstallReceiver(Context context) { 
    mContext = context; 
} 


@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.getLogger("DATA:" + intent.getData().toString()); 
    } 
    if(action.equals("android.intent.action.PACKAGE_REMOVED")){ 
     Logger.getLogger("DATA:" + intent.getData().toString()); 
    } 
    if(action.equals("android.intent.action.PACKAGE_REPLACED")){ 
     Logger.getLogger("DATA:" + intent.getData().toString()); 
    } 



    final PackageManager pm = mContext.getApplicationContext().getPackageManager(); 
    ApplicationInfo ai; 
    try { 
     ai = pm.getApplicationInfo(this.mContext.getPackageName(), 0); 
    } catch (final PackageManager.NameNotFoundException e) { 
     ai = null; 
    } 
    final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)"); 
} 
} 

私のマニフェスト:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.natalievold.applistener"> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 



    <receiver android:name="com.example.natalievold.applistener.NewInstallReceiver"> 
     <intent-filter android:priority="100"> 
      <action 
       android:name="android.intent.action.PACKAGE_INSTALL"/> 
      <action 
       android:name="android.intent.action.PACKAGE_ADDED"/> 
      <action 
       android:name="android.intent.action.PACKAGE_DATA_CLEARED"/> 
      <action 
       android:name="android.intent.action.PACKAGE_REMOVED"/> 
      <data android:scheme="package"/> 
     </intent-filter> 
    </receiver> 


    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 

ここで私が動作するようになった最終的なコードです! (これは、アンインストールではなく、アプリケーションをインストールするための部分です)。そして私はマニフェストで何も変えなかった。

public class NewInstallReceiver extends BroadcastReceiver 
{ 

@Override 
public void onReceive(Context context, Intent intent) { 

    Log.d("NewInstallReceiver", "Intent: " + intent.getAction()); 


    final PackageManager pm = context.getPackageManager(); 
    ApplicationInfo ai; 
    try { 
     ai = pm.getApplicationInfo(intent.getData().getSchemeSpecificPart(), 0); 
     Log.d("PACKAGE NAME","Intent" + ai); 
    } catch (final PackageManager.NameNotFoundException e) { 
     ai = null; 
    } 
    final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)"); 
    Log.d("Application NAME", "Intent: " + applicationName); 

    Drawable icon = context.getPackageManager().getApplicationIcon(ai); 
    Log.d("Application ICON", "Intent: " + icon); 

} 

} 

答えて

1

I read that I can solve this error by removing the "private context" section that I added, but I need that to use getApplicationContext and getPackageName.

いいえ、あなたはしないでください。

Does anyone know another way I can do this?

まず、あなたのonReceive()メソッドの最初のパラメータとして渡されContextを使用しています。これで

final PackageManager pm = mContext.getApplicationContext().getPackageManager(); 

:それはあなたがこれを交換することができます

final PackageManager pm = context.getPackageManager(); 

独自のContextに呼ばれる第二に、getPackageName()は、あなたにあなたパッケージ名を与えます。自分のパッケージ名が必要ではないようです。代わりに、インストールされているアプリのパッケージ名が必要です。そのためにはintent.getData().getSchemeSpecificPart()と呼ばれ、放送されたIntentUripackage:スキームの後のものを入手してください。

次に、そのパッケージに関する他の情報を入手するには、PackageManagerで調べることができます。 PackageManagergetApplicationInfo()に電話し、パッケージ名を渡します。これにより、ApplicationInfoオブジェクトが得られます。それをgetApplicationLabel()getApplicationIcon()PackageManagerにそれぞれ渡すと、ラベルとアイコンが表示されます。

+0

ありがとうございました! getApplicationIcon()のために、アイコンpngファイルまたは他のイメージファイルを取得しようとしていました。 getApplicationIconメソッドを使用すると、 "[email protected]"というメッセージが表示されます。イメージファイルを取得するためにこれを使用する方法はありますか? – Natalie

+0

@Natalie:Um、 'Bitmap'に裏打ちされた' Canvas'に描画してください。 – CommonsWare

関連する問題