简体   繁体   English

以编程方式阻止Android设备的硬件访问

[英]Block Android device's Hardware Access Programatically

I have to write an application that would work as 'Device Hardware Admin'. 我必须编写一个可以用作“设备硬件管理员”的应用程序。 The user should be able to enable or disable any feature of hardware according to his preferences. 用户应能够根据自己的喜好启用或禁用硬件的任何功能。 I am adding the screenshot of application UI below: 我在下面添加了应用程序UI的屏幕截图:

应用程序用户界面

These settings are stored in a local database of application. 这些设置存储在应用程序的本地数据库中。 What I have managed to do is as follows: 我设法做的如下:

Step 1: I have registered broadcast receiver for detecting all these actions. 步骤1:我已经注册了广播接收器,用于检测所有这些动作。

<receiver android:name=".HardwareActionListener" >
    <intent-filter>
        <action android:name="android.intent.action.CAMERA_BUTTON" >
        </action>

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.media.action.STILL_IMAGE_CAMERA" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.hardware.action.NEW_PICTURE" >
        </action>
    </intent-filter>
    <intent-filter>
        <action android:name="android.media.action.IMAGE_CAPTURE" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.UMS_CONNECTED" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.UMS_DISCONNECTED" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" >
        </action>
    </intent-filter>
    <intent-filter>
        <action android:name="android.net.wifi.WIFI_STATE_CHANGED" >
        </action>
    </intent-filter>
    <intent-filter>
        <action android:name="android.net.wifi.WIFI_STATE_CHANGED_ACTION" >
        </action>
    </intent-filter>
    <intent-filter>
        <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />

            <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
    </intent-filter>
</receiver>

I am getting broadcasts for Wifi and bluetooth properly. 我正在正确接收Wifi和蓝牙广播。 I am not getting broadcasts on Camera action or USB connection and disconnection. 我没有收到有关相机操作或USB连接和断开连接的广播。

Question 1 : Is anything wrong in intent filters I have used for Camera and USB detection? 问题1 :我用于相机和USB检测的意图过滤器有什么问题吗?

Step 2: I want to block the user from using these hardwares. 步骤2:我想阻止用户使用这些硬件。 I managed to disable bluetooth or wifi just after user started it by detecting the action in my receiver class's onReceive() method. 在用户启动蓝牙或wifi之后,我通过检测接收器类的onReceive()方法中的操作设法禁用了蓝牙或wifi。

if (intent.getAction().equalsIgnoreCase(
                "android.bluetooth.adapter.action.STATE_CHANGED")) {

    int extraBTState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                    BluetoothAdapter.ERROR);
    switch (extraBTState) {
        case BluetoothAdapter.STATE_CONNECTING:
            BluetoothAdapter mBluetoothAdapter = BluetoothAdapter
                        .getDefaultAdapter();
            mBluetoothAdapter.disable();
        break;
        case BluetoothAdapter.STATE_ON:

            BluetoothAdapter mBluetoothAdapter1 = BluetoothAdapter
                        .getDefaultAdapter();
            mBluetoothAdapter1.disable();
        break;
        case BluetoothAdapter.STATE_CONNECTED:

            BluetoothAdapter mBluetoothAdapter2 = BluetoothAdapter
                        .getDefaultAdapter();
            mBluetoothAdapter2.disable();
        break;
        case BluetoothAdapter.STATE_OFF:
            Toast.makeText(context, "Bluetooth access not allowed",
                        Toast.LENGTH_SHORT).show();
        break;
    }

} else if (intent.getAction().equalsIgnoreCase(
                "android.net.wifi.WIFI_STATE_CHANGED")) {
    int extraWifiState = intent.getIntExtra(
                    WifiManager.EXTRA_WIFI_STATE,
                    WifiManager.WIFI_STATE_UNKNOWN);
    switch (extraWifiState) {
        case WifiManager.WIFI_STATE_DISABLED:
            Toast.makeText(context, "Wifi enabling not allowed",
                        Toast.LENGTH_SHORT).show();
        break;

        case WifiManager.WIFI_STATE_ENABLED:

            WifiManager wifiManager1 = (WifiManager) context
                        .getSystemService(Context.WIFI_SERVICE);
            wifiManager1.setWifiEnabled(false);
        break;
        case WifiManager.WIFI_STATE_ENABLING:

            WifiManager wifiManager2 = (WifiManager) context
                        .getSystemService(Context.WIFI_SERVICE);
            wifiManager2.setWifiEnabled(false);
        break;

    }

} 

So it detects that Bluetooth or WiFi is turned on or connected and turns it off or disconnects it programmatically. 因此,它会检测到蓝牙或WiFi已打开或已连接,并以编程方式将其关闭或断开连接。

But requirement is that user shouldn't be able to start Bluetooth or WiFi at all. 但要求是用户完全不能启动蓝牙或WiFi。 That means disable the button of WiFi and Bluetooth. 这意味着禁用WiFi和蓝牙按钮。

Question 2 : Is that possible? 问题2 :有可能吗?

Any help or suggestions would be appreciated. 任何帮助或建议,将不胜感激。

Is anything wrong in intent filters I have used for Camera and USB detection? 我用于相机和USB检测的意图过滤器有什么问题吗?

Yes. 是。 For example: 例如:

  • <category> is not usually used on broadcasts, and definitely not the LAUNCHER category <category>通常不在广播中使用,并且绝对不是LAUNCHER类别
  • android.media.action.STILL_IMAGE_CAMERA and others in your filters are activity actions, not broadcast actions android.media.action.STILL_IMAGE_CAMERA和其他过滤器是活动操作,不是广播操作

Question 2: Is that possible? 问题2:有可能吗?

From an SDK app, no. 在SDK应用中,否。 You are welcome to download the source code to Android, modify it to suit your needs, compile and build a ROM mod from that, then install that ROM mod on whatever devices you can. 欢迎您将源代码下载到Android,对其进行修改以适应您的需要,然后从中编译并构建ROM mod,然后将ROM mod安装在您可以使用的任何设备上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM