在手機上,當然可以通過系統的設定來將其開啟,但是如果某個裝置上沒有該設定呢?
以下的程式碼需要平臺的簽名或者作為系統應用來執行才行(adb push <your apk files> /system/app/<your apk files>) ---當然
需要先執行 adb root
1.需要新增的許可權
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/> <uses-permission android:name="android.permission.WRITE_SETTINGS"/> <uses-permission android:name="android.permission.READ_SECURE_SETTINGS"/> <uses-permission android:name="android.permission.READ_SETTINGS"/>
2.
import android.provider.Settings;
import android.content.ContentValues;
ContentValues values = new ContentValues(); values.put("value", 1);
Cursor cursor = null; try{ int value = 0; cursor = getContentResolver().query(Settings.Secure.CONTENT_URI,
new String[] { "value",}, "name=?", new String[] {Settings.Secure.INSTALL_NON_MARKET_APPS}, null); if(cursor != null && cursor.moveToNext()){ value = cursor.getInt(cursor.getColumnIndex("value")); }
if(cursor != null){
cursor.close();
cursor = null;
}
if(0 == value){ int i = getContentResolver().update(Settings.Secure.CONTENT_URI, values,"name=?", new String[] {Settings.Secure.INSTALL_NON_MARKET_APPS} ); if(i > 0){ Log.e("", "success"); }else{ Log.e("", "fail"); } } }catch (Exception e) { e.printStackTrace(); }finally{
if(cursor != null){
cursor.close();
cursor = null;
}
}