用代码的方式打开“允许安装来自未知来源的应用”

在手机上,当然可以通过系统的设置来将其打开,但是如果某个设备上没有该设置呢?

以下的代码需要平台的签名或者作为系统应用来运行才行(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;
       }
     }
原文地址:https://www.cnblogs.com/mengshu-lbq/p/3214737.html