Android Phone Lock

I try to lock the device programmatically when the user clicks a button. I know that I will need to use deviceAdminReciever , and I did it, but my application crashes whenever I launch it

Below is my manifest:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.MyApp" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".MainActivity" android:permission="android.permission.BIND_DEVICE_ADMIN" > <meta-data android:name="android.app.device_admin" android:resource="@xml/device_admin_sample" /> <intent-filter> <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /> </intent-filter> </receiver> </application> <uses-feature android:name="android.hardware.camera" /> </manifest> 

Below is the java code:

 public class MainActivity extends DeviceAdminReceiver { public static class MyActivity extends Activity { protected static final int ACTIVATION_REQUEST = 1; private ImageButton btn; private DevicePolicyManager mDPM; private ComponentName mDeviceAdminSample; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); mDeviceAdminSample = new ComponentName(Potter.this, MainActivity.class); setContentView(R.layout.activity_main); btn = (ImageButton) findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent( DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); mDeviceAdminSample = new ComponentName(this, MainActivity.class); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdminSample); startActivityForResult(intent, ACTIVATION_REQUEST); mDPM.lockNow(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case ACTIVATION_REQUEST: if (resultCode == Activity.RESULT_OK) { Log.i("DeviceAdminSample", "Administration enabled!"); } else { Log.i("DeviceAdminSample", "Administration enable FAILED!"); } return; } } } } 

However, when I run the application, it crashes. How can i fix this?

+4
source share
2 answers

OK, DeviceAdminReceiver is a BroadcastReceiver, not an Activity. Right now, your manifest is declaring MainActivity for both components, so one of these declarations is incorrect. MainActivity is a bad name for this class, because it is not an activity, it should probably be MainReceiver or something (just for consistency).

Your application crashes because Android is trying to launch MainActivity , which is not an Activity, as the main activity of your application, which it cannot do.

Also, according to your code, MyActivity is the inner class of this receiver. This is not a paradigm that I would recommend adhering to, and can lead to some confusion. I would define both of these entities as completely separate classes. If one MUST be the inner class of the other, BroadcastReceiver will make more sense as an inner class of Activity .

In BARE MINIMUM, if you do not reorganize any of your Java code, you need to update the manifest to reference the relevant elements based on what you wrote, which means a reference to the actual activity as an inner class.

 <activity android:name=".MainActivity$MyActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".MainActivity" android:permission="android.permission.BIND_DEVICE_ADMIN" > <meta-data android:name="android.app.device_admin" android:resource="@xml/device_admin_sample" /> <intent-filter> <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /> </intent-filter> </receiver> 

It may take some time to re-examine the sample device administration API in the SDK, which is located in

 <SDK location>/samples/<platform-version>/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.java 

on your computer.

+5
source

Here is my code that works:

manifest:

 <application android:icon="@drawable/ic_power_settings_new_black_48dp" android:label="Screen Off" android:theme="@android:style/Theme.NoDisplay"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="android.app.admin.DeviceAdminReceiver"> <meta-data android:name="android.app.device_admin" android:resource="@xml/device_admin" /> <intent-filter> <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /> </intent-filter> </receiver> </application> 

MainActivity.java:

 package com.example.anhkhoachu.screenoff; import android.app.Activity; import android.app.Application; import android.app.admin.DeviceAdminReceiver; import android.app.admin.DevicePolicyManager; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends Activity { DevicePolicyManager dpm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); try { dpm.lockNow(); finish(); } catch (SecurityException e) { Intent it = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); it.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, new ComponentName(this, DeviceAdminReceiver.class)); startActivityForResult(it, 0); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { dpm.lockNow(); } else { Toast.makeText(this, "Device administrator must be activated to lock the screen", Toast.LENGTH_LONG).show(); } finish(); }} 

device_admin.xml:

<device-admin> <uses-policies> <force-lock /> </uses-policies> </device-admin>

+3
source

All Articles