欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 国际 > 控制卸载/安装应用

控制卸载/安装应用

2024/11/2 18:27:59 来源:https://blog.csdn.net/wzx311/article/details/143279746  浏览:    关键词:控制卸载/安装应用

这个是解决:开机默认状态不能卸载/安装应用,不能恢复出厂设置,
如果需要卸载/安装应用,和恢复出厂设置,则需要在设置-系统-关于平板-自定义版本点击5次,打开
输入密码28117three(弹出提示窗,请输入解锁密码),
同样自定义版本点击5次也要能关闭,也是同上步骤(弹出提示窗,请输入退出密码)
这个就是在安装或者卸载的时候去跳过那个流程

1、Settings.java

frameworks/base/core/java/android/provider/Settings.java
需要在这里加上一个属性,方便控制安装、卸载apk

         @Readablepublic static final String SCREEN_BRIGHTNESS = "screen_brightness";
+               
+               @Readable
+               @SuppressLint("NoSettingsProvider")
+        public static final String LOCK_INSTALL_APP = "lock_install_app";
+/*** The screen backlight brightness between 0.0f and 1.0f.

2、PackageInstallerActivity.java

frameworks/base/packages/PackageInstaller/src/com/android/packageinstaller/PackageInstallerActivity.java
这个是判断有没有权限去安装

 import java.io.File;import java.util.ArrayList;import java.util.List;
-
+import android.widget.Toast;/*** This activity is launched when a new application is installed via side loading* The package is first parsed and the user is notified of parse errors via a dialog.
@@ -517,6 +517,19 @@ public class PackageInstallerActivity extends AlertActivity {* Check if it is allowed to install the package and initiate install if allowed.*/private void checkIfAllowedAndInitiateInstall() {
+               
+                if (Settings.System.getInt(getContentResolver(),
+                Settings.System.LOCK_INSTALL_APP, 0) == 0){
+                                        Log.w(TAG, "caninstall  false ="+mPkgInfo.applicationInfo.packageName);
+        if(mPkgInfo.applicationInfo.packageName != null ){
+            setPmResult(PackageManager.INSTALL_FAILED_INVALID_APK);
+            Toast.makeText(this, "install_failed", Toast.LENGTH_LONG).show();
+            finish();
+            return;
+        }
+                               }
+       
+               if (mAllowUnknownSources || !isInstallRequestFromUnknownSource(getIntent())) {if (mLocalLOGV) Log.i(TAG, "install allowed");initiateInstall();

// 这里就是把那个安装的流程给跳过了

3、UninstallFinish.java

frameworks/base/packages/PackageInstaller/src/com/android/packageinstaller/UninstallFinish.java

 public class UninstallFinish extends BroadcastReceiver {private static final String LOG_TAG = UninstallFinish.class.getSimpleName();-    private static final String UNINSTALL_FAILURE_CHANNEL = "uninstall failure";
+     static final String UNINSTALL_FAILURE_CHANNEL = "uninstall failure";static final String EXTRA_UNINSTALL_ID = "com.android.packageinstaller.extra.UNINSTALL_ID";static final String EXTRA_APP_LABEL = "com.android.packageinstaller.extra.APP_LABEL";

4、UninstallerActivity.java

frameworks/base/packages/PackageInstaller/src/com/android/packageinstaller/UninstallerActivity.java
这个是卸载的时候也是采用跳过

 import com.android.packageinstaller.television.UninstallAppProgress;import java.util.List;
-
+import android.provider.Settings;/** This activity presents UI to uninstall an application. Usually launched with intent* Intent.ACTION_UNINSTALL_PKG_COMMAND and attribute
@@ -344,6 +344,27 @@ public class UninstallerActivity extends Activity {PendingIntent.getBroadcast(this, uninstallId, broadcastIntent,PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);+ 
+                               if (Settings.System.getInt(getContentResolver(),
+                Settings.System.LOCK_INSTALL_APP, 0) == 0){
+                                       NotificationManager notificationManager =getSystemService(NotificationManager.class);
+
+        NotificationChannel uninstallFailureChannel = new NotificationChannel(
+               UninstallFinish.UNINSTALL_FAILURE_CHANNEL,
+                       getString(R.string.uninstall_failure_notification_channel),
+                NotificationManager.IMPORTANCE_DEFAULT);
+        notificationManager.createNotificationChannel(uninstallFailureChannel);
+
+        Notification.Builder uninstallFailedNotification = new Notification.Builder(this,
+                UninstallFinish.UNINSTALL_FAILURE_CHANNEL);
+                               
+                               uninstallFailedNotification.setContentTitle(getString(R.string.uninstall_failed_app, label));
+        uninstallFailedNotification.setOngoing(false);
+        uninstallFailedNotification.setSmallIcon(R.drawable.ic_error);
+        notificationManager.notify(uninstallId, uninstallFailedNotification.build());
+               finish();
+               return;
+                               }NotificationManager notificationManager = getSystemService(NotificationManager.class);NotificationChannel uninstallingChannel = new NotificationChannel(UNINSTALLING_CHANNEL,getString(R.string.uninstalling_notification_channel),

5、defaults.xml

frameworks/base/packages/SettingsProvider/res/values/defaults.xml
这里是定义一个初始值
<integer name="def_lock_install_app">0</integer>
表示在最开始的时候也是不能安装apk和卸载apk的

6、DatabaseHelper.java

frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java

初始值的设置

             loadIntegerSetting(stmt, Settings.System.SCREEN_BRIGHTNESS,R.integer.def_screen_brightness);+                       loadIntegerSetting(stmt, Settings.System.LOCK_INSTALL_APP,
+                    R.integer.def_lock_install_app);loadBooleanSetting(stmt, Settings.System.SCREEN_BRIGHTNESS_MODE,R.bool.def_screen_brightness_automatic_mode);

7、BuildNumberPreferenceController.java

packages/apps/Settings/src/com/android/settings/deviceinfo/BuildNumberPreferenceController.java

这里是开始去点击设置的版本号

 import com.android.settingslib.utils.StringUtil;import com.google.android.setupcompat.util.WizardManagerHelper;
-
+import android.content.DialogInterface;
+import androidx.appcompat.app.AlertDialog;
+import android.view.View;
+import android.view.WindowManager;
+import android.widget.EditText;
+import android.util.Log;
+import android.provider.Settings;public class BuildNumberPreferenceController extends BasePreferenceController implementsLifecycleObserver, OnStart {@@ -67,6 +73,7 @@ public class BuildNumberPreferenceController extends BasePreferenceController imprivate int mDevHitCountdown;private boolean mProcessingLastDevHit;+       private EditText editText;public BuildNumberPreferenceController(Context context, String key) {super(context, key);mUm = (UserManager) context.getSystemService(Context.USER_SERVICE);
@@ -89,8 +96,7 @@ public class BuildNumberPreferenceController extends BasePreferenceController immContext, UserManager.DISALLOW_DEBUGGING_FEATURES, UserHandle.myUserId());mDebuggingFeaturesDisallowedBySystem = RestrictedLockUtilsInternal.hasBaseUserRestriction(mContext, UserManager.DISALLOW_DEBUGGING_FEATURES, UserHandle.myUserId());
-        mDevHitCountdown = DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(mContext)
-                ? -1 : TAPS_TO_BE_A_DEVELOPER;
+        mDevHitCountdown =  TAPS_TO_BE_A_DEVELOPER;mDevHitToast = null;}@@ -168,7 +174,8 @@ public class BuildNumberPreferenceController extends BasePreferenceController im.show();if (!mProcessingLastDevHit) {
-                    enableDevelopmentSettings();
+                                       unluckAlterDialog();
+                    //enableDevelopmentSettings();}mMetricsFeatureProvider.action(mMetricsFeatureProvider.getAttribution(mActivity),
@@ -221,7 +228,8 @@ public class BuildNumberPreferenceController extends BasePreferenceController imreturn false;}if (resultCode == Activity.RESULT_OK) {
-            enableDevelopmentSettings();
+                       unluckAlterDialog();
+            //enableDevelopmentSettings();}mProcessingLastDevHit = false;return true;
@@ -231,7 +239,7 @@ public class BuildNumberPreferenceController extends BasePreferenceController im* Enables development settings. Only call this after confirming password.*/private void enableDevelopmentSettings() {
-        mDevHitCountdown = 0;
+        mDevHitCountdown = TAPS_TO_BE_A_DEVELOPER;mProcessingLastDevHit = false;DevelopmentSettingsEnabler.setDevelopmentSettingsEnabled(mContext, true);if (mDevHitToast != null) {
@@ -248,4 +256,70 @@ public class BuildNumberPreferenceController extends BasePreferenceController improtected boolean isUserAMonkey() {return ActivityManager.isUserAMonkey();}
+       
+       private void unluckAlterDialog() {
+
+        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
+        builder.setTitle("Input lock Password");
+        builder.setNegativeButton("ok", new DialogInterface.OnClickListener() {
+            @Override
+            public void onClick(DialogInterface dialog, int which) {
+                String mPassword = editText.getText().toString();
+                               if(!TextUtils.isEmpty(mPassword)) {
+                Log.d("zmz-----", "text=" + mPassword);
+                if (mPassword.equals("28117three")) {
+                                       if (Settings.System.getInt(mContext.getContentResolver(),
+                Settings.System.LOCK_INSTALL_APP, 0) == 0){
+                                       Settings.System.putInt(mContext.getContentResolver(), Settings.System.LOCK_INSTALL_APP, 1);
+                                       enableDevelopmentSettings();
+                                       if (mDevHitToast != null) {
+                               mDevHitToast.cancel();
+                               }
+                               mDevHitToast = Toast.makeText(mContext, "unLock Password",
+                Toast.LENGTH_LONG);
+                               mDevHitToast.show();
+                               
+                               }else{
+                                       Settings.System.putInt(mContext.getContentResolver(), Settings.System.LOCK_INSTALL_APP, 0);
+                                       mDevHitCountdown = 7;
+                                       mProcessingLastDevHit = false;
+                                       DevelopmentSettingsEnabler.setDevelopmentSettingsEnabled(mContext, false);
+                                       if (mDevHitToast != null) {
+                               mDevHitToast.cancel();
+                               }
+                               mDevHitToast = Toast.makeText(mContext, "Lock Password",
+                Toast.LENGTH_LONG);
+                               mDevHitToast.show();
+                               
+                               }
+                
+                               
+                }else{
+                       if (mDevHitToast != null) {
+            mDevHitToast.cancel();
+        }
+                               mDevHitToast = Toast.makeText(mContext, "Password Error",
+                Toast.LENGTH_LONG);
+                               mDevHitToast.show();
+                               }
+                               }else{
+                                       if (mDevHitToast != null) {
+            mDevHitToast.cancel();
+        }
+                               mDevHitToast = Toast.makeText(mContext, "Password Error",
+                Toast.LENGTH_LONG);
+                               mDevHitToast.show();
+                               }
+            }
+        });
+        final AlertDialog dialog = builder.create();
+
+        View dialogView = View.inflate(mContext, R.layout.lock_input_password_dialog, null);
+        dialog.setView(dialogView);
+        dialog.show();
+        WindowManager.LayoutParams attrs = dialog.getWindow().getAttributes();
+        attrs.height = 310;
+        dialog.getWindow().setAttributes(attrs);
+        editText = dialog.findViewById(R.id.et_name);
+    }}

8、ResetPreferenceController.java

packages/apps/Settings/src/com/android/settings/system/ResetPreferenceController.java

 import com.android.settings.R;import com.android.settings.core.BasePreferenceController;import com.android.settings.network.NetworkResetPreferenceController;
-
+import android.provider.Settings;public class ResetPreferenceController extends BasePreferenceController {private final UserManager mUm;
@@ -37,7 +37,16 @@ public class ResetPreferenceController extends BasePreferenceController {@Overridepublic int getAvailabilityStatus() {
-        return mContext.getResources().getBoolean(R.bool.config_show_reset_dashboard)
-                ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
+       // return mContext.getResources().getBoolean(R.bool.config_show_reset_dashboard)
+        //        ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
+               /*if (Settings.System.getInt(mContext.getContentResolver(),
+                Settings.System.LOCK_INSTALL_APP, 0) == 0){
+                               return UNSUPPORTED_ON_DEVICE;   
+                               }else{
+                                       return AVAILABLE;
+                               }*/
+                               
+                               return Settings.System.getInt(mContext.getContentResolver(),
+                Settings.System.LOCK_INSTALL_APP, 0) == 1 ?  AVAILABLE : UNSUPPORTED_ON_DEVICE;}}

9、lock_input_password_dialog.xml

packages/apps/Settings/res/layout/lock_input_password_dialog.xml
弹窗的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><EditTextandroid:id="@+id/et_name"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:layout_marginRight="20dp"android:layout_marginTop="20dp"android:hint="Password:"android:singleLine="true"android:textSize="18sp" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="5dp"android:orientation="horizontal"android:paddingLeft="5dp"android:paddingRight="5dp"></LinearLayout>
</LinearLayout>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com