我必須編寫一個可用作「設備硬件管理」的應用程序。用戶應該能夠根據自己的喜好啓用或禁用任何硬件功能。我加入了以下應用程序UI截圖:以編程方式阻止Android設備的硬件訪問
這些設置存儲在應用程序的本地數據庫。我設法做到如下:
步驟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>
我收到廣播的WiFi和藍牙正常。我沒有通過相機操作或USB連接和斷開進行廣播。
問題1:我用於相機和USB檢測的意圖過濾器有什麼問題嗎?
第2步:我想阻止用戶使用這些硬件。在用戶通過檢測接收器類的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;
}
}
因此,它會檢測到藍牙或WiFi已打開或連接,並將其關閉或以編程方式斷開連接。
但要求是用戶應該無法啓動藍牙或WiFi。這意味着禁用WiFi和藍牙按鈕。
問題2:這可能嗎?
任何幫助或建議,將不勝感激。
謝謝。 DPM幫助阻止相機 –