阻止安装某个特定的Android设备

时间:2018-10-12 21:11:09

标签: android android-studio broadcastreceiver android-broadcastreceiver

我正在尝试创建一个应用,以根据软件包名称阻止某个应用的安装。我尝试了BroadcastReceiver方法,但该应用似乎崩溃了。并感谢您对此进行审核和提供帮助。

这是我的BroadcastReceiver类:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {

    Context context;
    String log = MyReceiver.class.getName();

    @Override
    public void onReceive(Context context, Intent intent) {

        this.context = context;
        if(intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")){
            Toast.makeText(context,"Package removed", Toast.LENGTH_LONG).show();
            Log.d(log,"Package removed");
        }
        if(intent.getAction().equals("android.intent.action.PACKAGE_ADDED")){
            Toast.makeText(context, "Package installed", Toast.LENGTH_SHORT).show();
            Log.d(log,"Package installed");
        }
    }
}

清单:

<receiver
        android:name="MyReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter android:priority="100">
            <action android:name="android.intent.action.PACKAGE_ADDED"/>
            <action android:name="android.intent.action.PACKAGE_REMOVED"/>
            <data android:scheme="package"/>
        </intent-filter>

    </receiver>

崩溃日志:

10-12 22:18:22.836 15429-15429/com.example.mohammed.broadcastreceiver E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mohammed.broadcastreceiver, PID: 15429
java.lang.RuntimeException: Unable to instantiate receiver com.example.mohammed.broadcastreceiver.MyReceiver: java.lang.ClassNotFoundException: Didn't find class "com.example.mohammed.broadcastreceiver.MyReceiver" on path: DexPathList[[zip file "/data/app/com.example.mohammed.broadcastreceiver-1/base.apk", zip file "/data/app/com.example.mohammed.broadcastreceiver-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.mohammed.broadcastreceiver-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.mohammed.broadcastreceiver-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.mohammed.broadcastreceiver-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.mohammed.broadcastreceiver-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.mohammed.broadcastreceiver-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.mohammed.broadcastreceiver-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.mohammed.broadcastreceiver-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.mohammed.broadcastreceiver-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.mohammed.broadcastreceiver-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.mohammed.broadcastreceiver-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.mohammed.broadcastreceiver-1/lib/arm64, /system/lib64, /vendor/lib64]]
    at android.app.ActivityThread.handleReceiver(ActivityThread.java:3287)
    at android.app.ActivityThread.-wrap20(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1719)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6692)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.mohammed.broadcastreceiver.MyReceiver" on path: DexPathList[[zip file "/data/app/com.example.mohammed.broadcastreceiver-1/base.apk", zip file "/data/app/com.example.mohammed.broadcastreceiver-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.mohammed.broadcastreceiver-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.mohammed.broadcastreceiver-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.mohammed.broadcastreceiver-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.mohammed.broadcastreceiver-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.mohammed.broadcastreceiver-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.mohammed.broadcastreceiver-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.mohammed.broadcastreceiver-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.mohammed.broadcastreceiver-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.mohammed.broadcastreceiver-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.mohammed.broadcastreceiver-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.mohammed.broadcastreceiver-1/lib/arm64, /system/lib64, /vendor/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at android.app.ActivityThread.handleReceiver(ActivityThread.java:3282)
    at android.app.ActivityThread.-wrap20(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1719) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6692) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 

1 个答案:

答案 0 :(得分:0)

您的接收器注册不正确。

崩溃日志显示它无法在项目的根目录中找到MyReceiver(这就是您所说的清单中所存在的位置),但看起来好像没有。

确保name属性指向实际位置。