1

I am working on Android app.Firstly, I am not familiar with BroadcastReceiver. I need to create an app in which, if install app have specific package like "com.whatsapp" a broadcast receiver will show the Toast

public class PackageAddedReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Package Installed: ", Toast.LENGTH_LONG).show();
    }
}

Manifest

<receiver android:name=".receiver.PackageAddedReceiver" android:label="Package added Receiver">
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_ADDED" />
        <action android:name="android.intent.action.PACKAGE_REMOVED"/>
        <data android:scheme="package"/>
    </intent-filter>
</receiver>

How can I add BroadcastReceiver for the specific package?

Denysole
  • 3,903
  • 1
  • 20
  • 28
mr.klood4
  • 93
  • 1
  • 1
  • 5

3 Answers3

3

Try this

public class PackageAddedReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {



        Uri data = intent.getData();
        String mypkg="package:com.pck.name";

        Log.e("DATA",data+"");
        Log.e( "Action: " ,intent.getAction());

        if(mypkg.equals(data.toString())){
            Toast.makeText(context, "Package Installed: ", Toast.LENGTH_LONG).show();
        }else {
            Toast.makeText(context, "not match ", Toast.LENGTH_LONG).show();
        }

    }

Manifest code

 <receiver android:name=".PackageAddedReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_INSTALL" />
            <action android:name="android.intent.action.PACKAGE_ADDED" />
            <data android:scheme="package"/>
        </intent-filter>
    </receiver>
Goku
  • 9,102
  • 8
  • 50
  • 81
  • I've 2 questions: 1- Why do you need the Activity? 2- Can you filter directly the package name from the Manifest like with deep linking? – Eselfar Jan 05 '18 at 10:42
  • where add specific package ? – mr.klood4 Jan 05 '18 at 10:42
  • @Eselfar i have 3 activity and all activity have apk for install i want after installed any apk file inside this activity get toast – mr.klood4 Jan 05 '18 at 10:45
  • Sorry was asking @Prem as I'm trying to understand his answer. – Eselfar Jan 05 '18 at 10:53
  • @Eselfar no worry i was just checking in my pc no need to use activity – Goku Jan 05 '18 at 10:55
  • @Prem Alright. But is it not possible to filter the package name in the Manifest? I've never used the `PACKAGE_` filter yet, but I was wondering if it can work like deep linking where you can specify a regex for the url you're targeting directly in the filter itself. – Eselfar Jan 05 '18 at 11:01
  • 1
    @Prem According to [this answer](https://stackoverflow.com/a/40966228/1827254), you can. But need to be tested as it doesn't seem to work will all the versions of the API (API 19 seems to be required). – Eselfar Jan 05 '18 at 11:17
1

A. Create BroadcastReceiver class (implement onReceive): you can extract the data related to package

import android.content.*;
import android.net.Uri;
import android.util.Log;

public class PackageChangeReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context ctx, Intent intent) {
     Uri data = intent.getData();
     Log.d(TAG, "Action: " + intent.getAction());
     Log.d(TAG, "The DATA: " + data);
   }
}

B. Declare receiver with intent-filter in AndroidManifest.xml:

<receiver android:name="PackageChangeReceiver">
    <intent-filter>
     <action android:name="android.intent.action.PACKAGE_ADDED"/>
     <action android:name="android.intent.action.PACKAGE_REPLACED"/>
     <action android:name="android.intent.action.PACKAGE_REMOVED"/>
     <data android:scheme="package"/>
  </intent-filter>
</receiver> 
Sunil Soni
  • 443
  • 3
  • 18
1

The receiver should be registered programmatically as below :

val packageAddedReceiver = PackageAddedReceiver()
    val intentFilter = IntentFilter()
    intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED)
    intentFilter.addAction(Intent.ACTION_PACKAGE_FIRST_LAUNCH)
    intentFilter.addAction(Intent.ACTION_PACKAGE_INSTALL)
    intentFilter.addDataScheme("package")
    registerReceiver(packageAddedReceiver, intentFilter)

for further explanation check CommonsWare answer in the link Can't receive broadcasts for PACKAGE intents

M.Khouli
  • 3,992
  • 1
  • 23
  • 26
  • its weird when you add this action "android.intent.action.PACKAGE_FULLY_REMOVED" you will receive the broadcast even if you register your receiver in the AndroidManifest only. – M.Khouli Jan 05 '18 at 11:13