Android BroadcastReceiver is not working when application is closed-ThrowExceptions

Exception or error:

I want to write a service that will be triggered when a new app is installed. First of all, I think I need a broadcast receiver. The broadcast receiver does not work when the application is closed. I’m trying on Android 7.0 and 8.1

What am I doing wrong?

I tried this solution:
Android Broadcast Receiver is not working in background

And this solution:
Android BroadcastReceiver application doesn't die when application is closed

compileSdkVersion 28
minSdkVersion 16
targetSdkVersion 28

My BroadcastReceiver

public class IsPackageAdded extends BroadcastReceiver {
    public static boolean isAdded = false;
    public static boolean isRemoved = false;
    Context context;
    private static final String TAG = "IsPackageAdded";

    @Override
    public void onReceive(Context paramContext, Intent paramIntent) {
        this.context = paramContext;
        if (paramIntent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {
            isRemoved = true;
            String packageName = paramIntent.getData().getSchemeSpecificPart();
            Log.d(TAG,"Successfully uninstalled: " + packageName);
            Toast.makeText(context, "Successfully uninstalled: "+packageName, Toast.LENGTH_LONG).show();

        }
        while (!paramIntent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {
            return;
        }
        String packageName = paramIntent.getData().getSchemeSpecificPart();
        Log.d(TAG,"Successfully installed: " + packageName);
        Toast.makeText(context, "Successfully installed: "+packageName, Toast.LENGTH_LONG).show();
        isAdded = true;
    }
}

My Manifest

<application>
...
    <receiver
        android:name="com.sdatam.catcher.classes.IsPackageAdded"
        android:enabled="true"
        android:exported="true"
        >
        <intent-filter android:priority="1000">
            <action android:name="android.intent.action.PACKAGE_ADDED" />
            <action android:name="android.intent.action.PACKAGE_REMOVED" />
            <data android:scheme="package" />
        </intent-filter>
    </receiver>
...
</application>

Another article says I should add this line in Manifest:

android:permission="android.permission.BROADCAST_PACKAGE_ADDED"
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *