How to install a new administrator

I use Device Policy Manager to immediately block my Android phone, and here is my activity code:

package com.husam.admin; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.app.admin.DeviceAdminReceiver; import android.app.admin.DevicePolicyManager; import android.content.ComponentName; import android.content.Context; import android.content.Intent; public class AdminActivity extends Activity { /** Called when the activity is first created. */ protected static final int REQUEST_CODE_ENABLE_ADMIN=1; DevicePolicyManager dpm; ComponentName mAdminName; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); dpm=(DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE); mAdminName=new ComponentName(this,MyAdmin.class); Button button=(Button)findViewById(R.id.admin); button.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub if(dpm.isAdminActive(mAdminName)) { Log.w("Yes admin","Locking Now"); dpm.lockNow(); } else {Intent intent1 =new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); intent1.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName); intent1.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Need new Admin"); Log.w("no Admin","Set admin"); startActivityForResult(intent1,REQUEST_CODE_ENABLE_ADMIN); } } }); } class MyAdmin extends DeviceAdminReceiver{ void OnEnabled(){ } void onDisable(){ } } } 

and My Mainfest.xml is populated as follows:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.husam.admin" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".AdminActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".MyAdmin" android:label="@string/device_admin" android:permission="android.permission.BIND_DEVICE_ADMIN"> <meta-data android:name="android.app.device_admin" android:resource="@xml/my_admin"/> <intent-filter> <action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/> </intent-filter> </receiver> </application> </manifest> 

and my_admin.xml, which is inside the xml folder under the res folder, looks like this:

 <device-admin xmlns:android="http://schemas.android.com/apk/res/android"> <uses-policies> <force-lock /> </uses-policies> </device-admin> 

and when I started the application, I encountered an error in my state of the log file, which:

 W/DeviceAdminAdd(433): Unable to retrieve device policy ComponentInfo{com.husam.admin/com.husam.admin.AdminActivity$MyAdmin} 

I searched this site for similar releases, but all I found was that there was an error in the mainfest file when closing the receiver, before including the meat and intent filter in it, which is not my case.

Therefore, any help or redirection to solve this problem will be fully appreciated.

considers

Khusy

+4
source share
1 answer

I think I understood the answer to the question that I wrote above. What I need to do is that when I register my receiver in my mainfest file, I have to do the following:

  <receiver android:name=".AdminActivity$MyAdmin" android:permission="android.permission.BIND_DEVICE_ADMIN"> <meta-data android:name="android.app.device_admin" android:resource="@xml/my_admin"/> <intent-filter> <action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/> </intent-filter> </receiver> 

Note:

in android:name I need to write .AdminActivity$Myadmin instead of .Myadmin , since my class for the administration broadcast receiver is an inner class in my activity, not a separate one.

+4
source

Source: https://habr.com/ru/post/1414413/


All Articles