Capturing installed applications on an Android device

I got a list of the package name and label of the installed application in the database. Now I want to intercept the installed application and database. Suppose any application is removed from the device, then I want the changes in the database. I use a broadcast receiver for this, but my code does not work.

PackageManager pm = this.getPackageManager();
List < ApplicationInfo > list = getPackageManager().getInstalledApplications(PackageManager.PERMISSION_GRANTED);
for (int n = 0; n < list.size(); n++) {
    Apps.add(list.get(n).loadLabel(pm).toString());
    AppsP.add(list.get(n).packageName.toString());
    Log.w("Installed Applications", list.get(n).loadLabel(pm).toString());
    Log.w("Installed Applications Package", list.get(n).packageName.toString());


    ...
    In other class

    public class PackageReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            if (intent.getAction().equalsIgnoreCase(Intent.ACTION_PACKAGE_ADDED)) {
                String added_package = intent.getData().toString();
                Log.i("PackageReceiver", "Package Added = " + added_package);

            } else if (intent.getAction().equalsIgnoreCase(Intent.ACTION_PACKAGE_REMOVED)) {
                String removed_package = intent.getData().toString();
                MyDBHelper DB = new MyDBHelper(context);
                Log.i("PackageReceiver", "Package removed = " + removed_package);
                DB.open();
                DB.deleteStmt = DB.db.compileStatement(QueryManager.ApplicationDelete);
                DB.deleteStmt.bindString(1, removed_package);
                DB.close();

            }

any help?

+3
source share
2 answers
    intentAPP.addAction(Intent.ACTION_PACKAGE_ADDED);
            intentAPP.addAction(Intent.ACTION_PACKAGE_CHANGED);
            intentAPP.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED);
            intentAPP.addAction(Intent.ACTION_PACKAGE_INSTALL);
            intentAPP.addAction(Intent.ACTION_PACKAGE_REMOVED);
            intentAPP.addAction(Intent.ACTION_PACKAGE_REPLACED);
            intentAPP.addAction(Intent.ACTION_PACKAGE_RESTARTED);

//for storing list in db

    for (int i = 0; i < AppsP.size() && i < Apps.size(); i++) {

                    DB.insertStmt.bindString(1, URLDecoder.decode(AppsP.get(i)));
                    DB.insertStmt.bindString(2, URLDecoder.decode(Apps.get(i)));
                    DB.insertStmt.bindString(3, 1 + "");
                    DB.insertStmt.bindString(4, "false");
                    DB.insertStmt.executeInsert();

                }

//adding package notification
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_PACKAGE_ADDED)) {
            String added_package = intent.getData().toString();
            Log.i("PackageReceiver", "Package Added = " + added_package);
            Intent Intent_add = new Intent();
            Intent_add.setClass(context, ServiceAppsAdd.class);
            Intent_add.putExtra("Package_Name", added_package.substring(8));
            Intent_add.putExtra("Status", status_new);
            context.startService(Intent_add);
+1
source

What exactly are you trying to achieve?

If you are trying to prevent the removal or installation of applications, I'm afraid you will not be able to do this. All you can do is request a notification.


EDIT:

Have you also specified an intent filter in your manifest? For instance:

<manifest>
    ....
    <application>
        ....
        <receiver>
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_ADDED">
                <action android:name="android.intent.action.PACKAGE_REMOVED">
            </intent-filter>
        </receiver>
    </application>
</manifest>
+3
source

Source: https://habr.com/ru/post/1795486/


All Articles