0

I want start my application after upgrade. I used both android.intent.action.MY_PACKAGE_REPLACED and android.intent.action.PACKAGE_REPLACED actions.But it's never calling to my onReceive method. Min SDK version of my app is 21. This app does not contain activity class.Only a service. I created this app for launching my another android application in this device.

Here is Manifest code

<application
        android:name=".DeviceAdminApplication"
        android:allowBackup="true"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <service
            android:name=".SchedulerJobService"
            android:permission="android.permission.BIND_JOB_SERVICE" />
        <receiver
            android:name=".AdminBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

        </receiver>
        <receiver android:name=".AppInstalledReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_INSTALL" />
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <data android:scheme="package"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
                <data android:scheme="package" android:path="com.pnct.agent"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_REPLACED" />
                <data android:scheme="package" android:path="com.pnct.agent"/>
            </intent-filter>
        </receiver>
    </application>

here is the onReceive method

public class AppInstalledReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(Constants.LOGTAG, "inside on receive");
        Log.d(Constants.LOGTAG, "action:"+intent.getAction());
        if(intent.getAction().equalsIgnoreCase("android.intent.action.MY_PACKAGE_REPLACED") ||
                (intent.getAction().equalsIgnoreCase("android.intent.action.PACKAGE_REPLACED"))){
            Log.d(Constants.LOGTAG, "package replaced");
        }}

If I install any other application,then onReceive method get called. Please help me to solve this problem thanks.

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
DevAra
  • 531
  • 3
  • 10
  • 35

3 Answers3

0

Documentation for ACTION_MY_PACKAGE_REPLACED says:

A new version of your application has been installed over an existing one. This is only sent to the application that was replaced. It does not contain any additional data; to receive it, just use an intent filter for this action.

This means old app should receive broadcast, not new one. So at least you cannot use this for starting app.

If you want to update your apk manually and by code, check here.

Afshin
  • 8,839
  • 1
  • 18
  • 53
0

download sdk if not exists in your pc The SDK Path \ platform tools folder, i mean "C:\AndroidStudio_SDK\platform-tools".

  • Run Command Prompt and execute cd C:\AndroidStudio_SDK\platform-tools

  • After that execute adb shell am broadcast -a android.intent.action.MY_PACKAGE_REPLACED

This method will run your UpdateReceiver or whatever you named that class. I suggest add data and control that. If data exists it was called


Command Prompt -> shell am broadcast -a android.intent.action.MY_PACKAGE_REPLACED -n com.example.myappname/.UpdateReceiver

This method run directly your receiver

-1

From Android Broadcast Documentation

Manifest-declared receivers

Note: If your app targets API level 26 or higher, you cannot use the manifest to declare a receiver for implicit broadcasts (broadcasts that do not target your app specifically), except for a few implicit broadcasts that are exempted from that restriction. In most cases, you can use scheduled jobs instead.

These changes are also mentioned in Android Oreo behaviour changes.

Apps cannot use their manifests to register for most implicit broadcasts (that is, broadcasts that are not targeted specifically at the app).

Solution

You can do one of below

  1. Instead of registering in manifest, you can register in your activity or service. See this answer.
  2. One guy did a trick to register broadcast receiver in manifest in oreo, nougat devices. See this repo.

Update

See documentation

ACTION_MY_PACKAGE_REPLACED

Broadcast Action: A new version of your application has been installed over an existing one. This is only sent to the application that was replaced. It does not contain any additional data; to receive it, just use an intent filter for this action.

Your old app will get this broadcast. So check if old app has broadcast receiver.

Community
  • 1
  • 1
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • ,Thanks for the reply. But my app's target sdk version also 21.and if I install any other application,then onReceive method get called,if I upgrade this app then only onReceive method not called – DevAra Sep 24 '18 at 09:20
  • If same code was working before, and not working now, then we can not help you, you have to find out the changes you made. – Khemraj Sharma Sep 24 '18 at 09:23
  • no no.I meant if I installed any other application then onReceive method still get called.problem is,if I upgrade my app,then only onReceive method not calling – DevAra Sep 24 '18 at 09:36
  • When you upgrade your app, then your app is killed during updation so how will that be called? – Khemraj Sharma Sep 24 '18 at 09:38
  • yeah,but in the android documentation they said this kind of thing: public static final String ACTION_MY_PACKAGE_REPLACED Broadcast Action: A new version of your application has been installed over an existing one. This is only sent to the application that was replaced. It does not contain any additional data; to receive it, just use an intent filter for this action. – DevAra Sep 24 '18 at 09:42
  • 1
    @rockKah you yourself say *This is only sent to the application that was replaced*. means the old app should receive it before replacing not new one. or at least this is what I got from document. – Afshin Sep 24 '18 at 09:45
  • @Afshin in my case first of all I download the latest version app from my server,and install it inside the current app(installing app older version).then after I want to call onReceive method. however my service is still running ,it's not killed when app upgrading. – DevAra Sep 24 '18 at 09:50
  • @rockKah it is strange that your service is not killed when application is replacing. I guess android should kill it before installing. You want to install upgrade manually yourself? – Afshin Sep 24 '18 at 09:54
  • I want to upgrade current app programmatically.yeah I set logs.I put a log inside onReceive method too – DevAra Sep 24 '18 at 10:16
  • Nah, "This is only sent to the application that was replaced." just means that other apps won't receive this broadcast when your app is replaced. It doesn't mean that the old version of your app is going to receive this. – Zsolt Safrany Feb 18 '20 at 23:49