在手機上,當然可以通過系統的設置來將其打開,但是如果某個設備上沒有該設置呢?
以下的代碼需要平台的簽名或者作為系統應用來運行才行(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;
}
}