3

我的應用程序運行一項服務,當設備重新啓動或應用程序重新安裝(更新)時終止。我添加了兩個廣播接收器來捕獲這些事件 - BOOT_COMPLETED和ACTION_MY_PACKAGE_REPLACED。Android:廣播ACTION_MY_PACKAGE_REPLACED從未收到

ACTION_MY_PACKAGE_REPLACED接收器似乎不工作。下面是我有:

的AndroidManifest.xml:

<receiver android:name=".RebootReceiver"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED"/> 
     </intent-filter> 
    </receiver> 
    <receiver android:name=".ReInstallReceiver"> 
     <intent-filter> 
      <action android:name="android.intent.action.ACTION_MY_PACKAGE_REPLACED"/> 
     </intent-filter> 
    </receiver> 

RebootReceiver:

public class RebootReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Logg.d("Reboot completed. Restarting service"); 
     context.startService(new Intent(context, MyService.class)); 
    } 
} 

ReInstallReceiver:

public class ReInstallReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Logg.d("App Upgraded or Reinstalled. Restarting service"); 
     context.startService(new Intent(context, MyService.class)); 
    } 
} 

運行minSdk = 16;在運行KitKat的Galaxy S3上進行測試。通過檢查我的服務是否在設置/應用程序中運行來測試成功,它在重新啓動時執行,但不能重新安裝。

我已經考慮到了以下的註釋,它說在Android Studio 1.0+中,清單合併意味着我無法將兩個接收器合併到一個類中。見ACTION_MY_PACKAGE_REPLACED not receivedAndroid manifest merger fails for receivers with same name but different content

回答

12

你可能想通了這一點了,但在清單的動作名稱是錯誤的,而不是:

android.intent.action.ACTION_MY_PACKAGE_REPLACED 

應該

android.intent.action.MY_PACKAGE_REPLACED 

您也可以手動觸發接收器使用adb shell進行測試:

adb shell am broadcast -a android.intent.action.MY_PACKAGE_REPLACED -n com.example.myapp/.ReInstallReceiver 
+2

無法在Android 7 Nougat上使用adb命令模擬此意圖,似乎現在它在敏感意圖列表中:「java.lang.SecurityException:Permission Denial:not allowed to send broa dcast android.intent.action.MY_PACKAGE_REPLACED「。儘管在Kit-Kat 4.4上完美運行,但即使沒有adb命令,每當我在Android Studio中啓動應用程序時,它都會在接收器中調用我的斷點 – 2017-04-17 21:41:21

1

ACTION EXPLAIN IMAGE

要考慮到:

  1. 您的應用程序的新版本
  2. 您應該運行ADB安裝-r新版本的APK,如果你只是在Android Studio中它贏得運行 」 t收到此廣播