0

Im trying to do a BootReceiver call when my own package installs/updates, but I cant seem to find how to check if the called reason is because of the package the code is in, or another app that has updated. Does such a thing exist already, and if so, how to use it?

Here is the code:

[BroadcastReceiver(Enabled = true, Exported = true, DirectBootAware = true)]
[IntentFilter(new string[] { Intent.ActionBootCompleted, Intent.ActionLockedBootCompleted, Intent.ActionPackageReplaced, "android.intent.action.QUICKBOOT_POWERON", "com.htc.intent.action.QUICKBOOT_POWERON" })]
public class BootReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        Intent timedIntent = new Intent(context, typeof(ITM.NotificationService));
        //add missed traffic before program started with boot
        if(intent.Action.Equals(Intent.ActionBootCompleted) || intent.Action.Equals(Intent.ActionLockedBootCompleted))
        {
            timedIntent.PutExtra("aDownBootup", TrafficStats.TotalRxBytes);
            timedIntent.PutExtra("aUpBootup", TrafficStats.TotalTxBytes);
        }
        if (ITM.NotificationService.isStarted == false)
        {
            Application.Context.StartService(timedIntent);
        }
    }
}
Nevaran
  • 137
  • 9
  • By `the called reason is because of the package the code is in`, what do you mean? Do you want to use `BootReceiver` to check package installs/updates? Or do you want to use `BootReceiver` to receive package has been installed/updated? – Robbit Jun 18 '18 at 01:17
  • Right now the BroadcastReceiver gets called every time an app(any app) has been replaced/updated - I want to somehow only make it when *my* app has done that - aka the app that has this code. – Nevaran Jun 18 '18 at 05:52
  • Hello, try this [action](https://developer.android.com/reference/android/content/Intent#ACTION_MY_PACKAGE_REPLACED), not `ActionPackageReplaced`. – Robbit Jun 18 '18 at 06:10
  • Hi, have you tested my answer? – Robbit Jun 18 '18 at 14:54
  • Hello- sorry, Im having a small hiccup in the phone im testing it on. The notification for the app got blocked and now I cannot see the notification(service probably isnt even started). Going to try it out as soon as I find a way of fixing it without needing to reset the phone. Also is there a way to test it out without needing to actually do a replacement? Rather new in the android development business and I still have some distance before figuring out all the testing tools. – Nevaran Jun 18 '18 at 16:22

1 Answers1

0

Thanks @android developer's answer.

You can use ACTION_MY_PACKAGE_REPLACED 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.

Robbit
  • 4,300
  • 1
  • 13
  • 29