theDazzler
theDazzler

Reputation: 1059

How to use BroadCastReceiver programmatically

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.

Upvotes: 1

Views: 1296

Answers (2)

jtt
jtt

Reputation: 13541

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'.

Upvotes: 2

Iman Marashi
Iman Marashi

Reputation: 5753

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.

Upvotes: -1

Related Questions