26

Play ストアにない私のアプリケーションは Web で確認します。新しいバージョンがある場合は、ダウンロードして起動します。インストール後、アプリケーションを再起動したいので with を使用BroadcastRecevierACTION_PACKAGE_REPLACEDます。これはコードです:

ブロードキャスト:

public void onReceive(Context context, Intent intent) {
  if(intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)){
    ApplicationInfo app = new ApplicationInfo();
    if(app.packageName.equals("it.android.downloadapk")){
      Intent LaunchIntent = context.getPackageManager().getLaunchIntentForPackage(app.packageName);
      context.startActivity(LaunchIntent);                    
    }
  }
}

マニフェスト:

<receiver android:name="it.android.downloadapk.Broadcast">
  <intent-filter>
    <action android:name="android.intent.action.ACTION_PACKAGE_REPLACED"></action>
    <data android:scheme="package" android:path="it.android.downloadapk" /> 
  </intent-filter>
</receiver>

問題は、新しい apk をインストールすると、ブロードキャストが開始されないように見えることです。なぜですか?

4

5 に答える 5

30

これを参照してください:

アラームをリセットするために Android アプリケーションがアップグレードされたことを確認するにはどうすればよいですか?

正しい修正は、マニフェストで間違った文字列を使用することです: http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_REPLACED

代わりに「android.intent.action.PACKAGE_REPLACED」にする必要があります。


わかりました、私が書いたものはまだ試してみるのに十分ではないことがわかりました. テストする前に実行することを忘れないでください。そうしないと、一部の Android バージョン (API 11+ だと思います) では動作しません。

マニフェスト:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.broadcast_receiver_test" android:versionCode="1"
  android:versionName="1.0">
  <uses-sdk android:minSdkVersion="3" />

  <application android:icon="@drawable/ic_launcher"
    android:label="@string/app_name">

    <activity android:name=".BroadcastReceiverTestActivity"
      android:label="@string/app_name">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>

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

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

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

  </application>
</manifest>

MyBroadcastReceiver.java:

public class MyBroadcastReceiver extends BroadcastReceiver
  {
  @Override
  public void onReceive(final Context context,final Intent intent)
    {
    final String msg="intent:"+intent+" action:"+intent.getAction();
    Log.d("DEBUG",msg);
    Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
    }
  }

実行して、完全に動作することを確認してください。


編集: アプリが API12 以降用であり、アプリの更新のみを処理したい場合は、このインテントを単独で使用できます。

http://developer.android.com/reference/android/content/Intent.html#ACTION_MY_PACKAGE_REPLACED

于 2012-05-23T22:23:57.023 に答える
8

次のレシーバーを AndroidManifest.xml に入れました

<receiver android:name=".StartupReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
    </intent-filter>
</receiver>

したがって、私のアプリは、更新時とデバイスの再起動時に起動できます。もちろん、MY_PACKAGE_REPLACED には API 12+ が必要だと誰もが言っているように。

于 2016-03-01T00:31:31.267 に答える