问题描述:

In my project , I want to prevent user to uninstall my application. It means that when user select my application to uninstall , must need administrator permission.

here is my code:

 public class AdminReceiver extends DeviceAdminReceiver

{

public static final String TAG = "DeviceAdminBugDemo";

@Override

public void onEnabled(Context context, Intent intent)

{

Log.v(TAG, "AdminReceiver.onEnabled()");

}

@Override

public void onDisabled(Context context, Intent intent)

{

Log.v(TAG, "AdminReceiver.onDisabled()");

}

@Override

public void onPasswordChanged(Context context, Intent intent)

{

Log.v(TAG, "AdminReceiver.onPasswordChanged()");

}

}

and also i add these to my manifest:

 <receiver android:name=".AdminReceiver" android:permission="android.permission.BIND_DEVICE_ADMIN">

<meta-data android:name="android.app.device_admin" android:resource="@xml/device_admin" />

<intent-filter>

<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />

<action android:name="android.app.action.DEVICE_ADMIN_DISABLED" />

<action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLE_REQUESTED" />

<action android:name="android.app.action.ACTION_PASSWORD_CHANGED" />

</intent-filter>

</receiver>

and here in my activity:

 btntoggle= (Button) findViewById(R.id.btntoggle);

btntoggle.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

toggleDeviceAdmin();

}

});

private void toggleDeviceAdmin()

{

if (devicePolicyManager.isAdminActive(adminReceiver))

{

int count = 0;

List<ComponentName> compNames = devicePolicyManager.getActiveAdmins();

if (compNames != null)

{

for (ComponentName compName : compNames)

{

if (compName.equals(adminReceiver))

{

devicePolicyManager.removeActiveAdmin(compName);

count++;

}

}

}

Toast.makeText(getApplicationContext(),"Unistall Allow", Toast.LENGTH_SHORT).show();

btntoggle.setText("Activate Device Admin");

}

else

{

Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);

intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminReceiver);

startActivityForResult(intent, ACTIVITY_RESULT_DEVICE_ADMIN);

}

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data)

{

super.onActivityResult(requestCode, resultCode, data);

switch (requestCode)

{

case ACTIVITY_RESULT_DEVICE_ADMIN:

if (resultCode == Activity.RESULT_OK)

{

btntoggle.setText("Deactivate Device Admin");

Toast.makeText(getApplicationContext(), "Uninstall Not Allow", Toast.LENGTH_SHORT).show();

}

}

}

this code works , but the question is that when I don't allow user to uninstall the application , In my device setting, screen time out disabled and my screen turn off after each one second.

any suggestion?

相关阅读:
Top