防止应用程序在 Android 中卸载
Posted
技术标签:
【中文标题】防止应用程序在 Android 中卸载【英文标题】:Prevent app from Uninstall in Android 【发布时间】:2015-05-01 22:12:47 【问题描述】:我想要什么
我想在我的设置菜单中有一个简单的复选框,如果选中该复选框,将为我的应用启用设备管理,并防止我的应用被卸载。
未选中该复选框将禁用设备管理。
我的应用程序与安全有关,需要防止被卸载。我能得到一个简单的解决方案吗?
PS - 我已阅读有关此的文档,但似乎无法使其正常工作。
【问题讨论】:
How to prevent an application from being uninstalled? 可能重复。 【参考方案1】:import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
/**
* This is the component that is responsible for actual device administration.
* It becomes the receiver when a policy is applied. It is important that we
* subclass DeviceAdminReceiver class here and to implement its only required
* method onEnabled().
*/
public class DemoDeviceAdmin extends DeviceAdminReceiver
static final String TAG = "DemoDeviceAdmin";
/** Called when this application is approved to be a device administrator. */
@Override
public void onEnabled(Context context, Intent intent)
super.onEnabled(context, intent);
Toast.makeText(context, R.string.device_admin_enabled,
Toast.LENGTH_LONG).show();
Log.d(TAG, "onEnabled");
/** Called when this application is no longer the device administrator. */
@Override
public void onDisabled(Context context, Intent intent)
super.onDisabled(context, intent);
Toast.makeText(context, R.string.device_admin_disabled,
Toast.LENGTH_LONG).show();
Log.d(TAG, "onDisabled");
@Override
public void onPasswordChanged(Context context, Intent intent)
super.onPasswordChanged(context, intent);
Log.d(TAG, "onPasswordChanged");
@Override
public void onPasswordFailed(Context context, Intent intent)
super.onPasswordFailed(context, intent);
Log.d(TAG, "onPasswordFailed");
@Override
public void onPasswordSucceeded(Context context, Intent intent)
super.onPasswordSucceeded(context, intent);
Log.d(TAG, "onPasswordSucceeded");
MainActivity 是这样的
devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
demoDeviceAdmin = new ComponentName(this, DemoDeviceAdmin.class);
Log.e("DeviceAdminActive==", "" + demoDeviceAdmin);
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);// adds new device administrator
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, demoDeviceAdmin);//ComponentName of the administrator component.
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"Disable app");//dditional explanation
startActivityForResult(intent, ACTIVATION_REQUEST);
Manifest 是这样的:
<!-- This is where we register our receiver -->
<receiver
android:name=".DemoDeviceAdmin"
android:permission="android.permission.BIND_DEVICE_ADMIN" >
<intent-filter>
<!-- This action is required -->
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
<!-- This is required this receiver to become device admin component. -->
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_admin_sample" />
</receiver>
【讨论】:
一个非常复杂的解决方案。非常感谢。 什么在 android:resource="@xml/device_admin_sample" ?? android.googlesource.com/platform/packages/apps/Email/+/9829145/… 谢谢,它对我有用。但是现在又如何恢复回来?我的意思是禁用启用的权限。【参考方案2】:这是不可能的。您无法自行决定让您的应用成为设备管理员。欢迎您将用户引导到“设置”应用中的适当位置,让用户选择让您的应用成为设备管理员,不过 via ACTION_ADD_DEVICE_ADMIN
。
例如,此活动将查看它是否已经是设备管理员(通过isActiveAdmin()
),然后在需要时启动ACTION_ADD_DEVICE_ADMIN
活动:
/***
Copyright (c) 2012 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
From _The Busy Coder's Guide to Android Development_
http://commonsware.com/Android
*/
package com.commonsware.android.lockme;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class LockMeNowActivity extends Activity
private DevicePolicyManager mgr=null;
private ComponentName cn=null;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cn=new ComponentName(this, AdminReceiver.class);
mgr=(DevicePolicyManager)getSystemService(DEVICE_POLICY_SERVICE);
public void lockMeNow(View v)
if (mgr.isAdminActive(cn))
mgr.lockNow();
else
Intent intent=
new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, cn);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
getString(R.string.device_admin_explanation));
startActivity(intent);
(来自this sample project)
这两个附加功能(EXTRA_DEVICE_ADMIN
和 EXTRA_ADD_EXPLANATION
)是可选的,尽管它们是个好主意。第一个应该是 ComponentName
标识您的 DeviceAdminReceiver
子类;第二个应该是一个字符串(来自字符串资源),它解释了为什么用户应该让您的应用成为设备管理员。
我的应用与安全有关,需要保护以防被卸载。
由于任何人都可以进入并决定不让您的应用成为设备管理员(再次通过设置),然后将其卸载,因此这并不是什么防御措施。
【讨论】:
这就是我真正指的。我想指导用户选择是否要成为应用设备管理员。 但不幸的是,我似乎无法让它工作。你能帮忙完成它的步骤吗? @Aritra:嗯,这只是一个Intent
操作,所以没什么大不了的。我已将示例粘贴到我的答案中。
我似乎犯了一些非常小的错误。我会检查你的代码并尝试一下。万分感谢。 :-)
@CommnsWare 我不应该在清单中声明一个接收者吗?以上是关于防止应用程序在 Android 中卸载的主要内容,如果未能解决你的问题,请参考以下文章