1

How can I register a BroadcastReceiver listening for the "PACKAGE_ADDED" action programmatically? I am trying to do something after a package is installed. I can get it working by registering the receiver in the AndroidManifest.xml, but I need to get it working the other way by programmatically registering it so that it only gets called on .apks installed through my app. I've tried it several different ways, the code below is from this answer https://stackoverflow.com/a/4805733/1024722 but it doesn't work. Any ideas?

private BroadcastReceiver receiver;

protected void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);
        ButterKnife.inject(this);


        intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);

        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Log.v("test", "received");
            }
        };

        registerReceiver(receiver, intentFilter);   

}

EDIT: In my app's activity, I click a button to download the app from the server and then to install it I click the downloaded app in my notification bar. It then presents this screen:

enter image description here

I click "Install" and it installs but doesn't call my onReceive method(unless I register it in the xml). Then it shows this screen:

enter image description here

then I click "done" and it returns to my activity with the "install" button. I am wondering if it's not working because it launches the activities shown in the screenshots, and is therefore not able to call the onReceive method in my receiver since my activity's onPause method has been called and isn't "active" anymore until I click done, which is after the "PACKAGE_ADDED" action gets called.

Community
  • 1
  • 1
theDazzler
  • 1,039
  • 2
  • 11
  • 27
  • The code is correct. I am using such this and works fine. How did you test it? – Misagh Emamverdi Oct 19 '14 at 19:24
  • I ran my app and then installed a package with it, and my onReceive method never got called. It gets called however when I register the receiver in my AndroidManifest instead – theDazzler Oct 19 '14 at 19:26

2 Answers2

2

You need to add the http://developer.android.com/reference/android/content/IntentFilter.html#addDataScheme(java.lang.String):

|intent filter object|.addDataScheme("package");

The PackageManager will only send it to receivers that have that have that intent action AND the data scheme as 'package'.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
  • You are correct. For some reason I had that when I registered it in the xml, but I didn't do it when I tried to register it programmatically. Thanks – theDazzler Oct 29 '14 at 23:17
-1

It sounds like you want to control whether components published in your manifest are active, not dynamically register a receiver (via Context.registerReceiver()) while running.

If so, you can use PackageManager.setComponentEnabledSetting() to control whether these components are active:

http://developer.android.com/reference/android/content/pm/PackageManager.html#setComponentEnabledSetting(android.content.ComponentName, int, int)

Note if you are only interested in receiving a broadcast while you are running, it is better to use registerReceiver(). A receiver component is primarily useful for when you need to make sure your app is launched every time the broadcast is sent.

Iman Marashi
  • 5,593
  • 38
  • 51