简体   繁体   中英

Android to set/change/remove Lock Screen PIN, Password or Unlock Pattern programatically

I'm working in an android project which is used to secure android device with password. For that i have to set password programatically. used reference user device administration api and this example But it throws error(below) :

08-18 02:12:26.888: W/dalvikvm(2600): threadid=1: thread exiting with uncaught       exception (group=0x40a401f8)
08-18 02:12:26.888: E/AndroidRuntime(2600): FATAL EXCEPTION: main
08-18 02:12:26.888: E/AndroidRuntime(2600): java.lang.RuntimeException: Unable to start activity ComponentInfo{dilli.passwordtest/dilli.passwordtest.MainActivity}: java.lang.SecurityException: No active admin ComponentInfo{dilli.passwordtest/dilli.passwordtest.MainActivity}
08-18 02:12:26.888: E/AndroidRuntime(2600):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
08-18 02:12:26.888: E/AndroidRuntime(2600):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-18 02:12:26.888: E/AndroidRuntime(2600):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-18 02:12:26.888: E/AndroidRuntime(2600):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-18 02:12:26.888: E/AndroidRuntime(2600):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-18 02:12:26.888: E/AndroidRuntime(2600):     at android.os.Looper.loop(Looper.java:137)
08-18 02:12:26.888: E/AndroidRuntime(2600):     at android.app.ActivityThread.main(ActivityThread.java:4429)
08-18 02:12:26.888: E/AndroidRuntime(2600):     at java.lang.reflect.Method.invokeNative(Native Method)
08-18 02:12:26.888: E/AndroidRuntime(2600):     at java.lang.reflect.Method.invoke(Method.java:511)
08-18 02:12:26.888: E/AndroidRuntime(2600):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
08-18 02:12:26.888: E/AndroidRuntime(2600):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
08-18 02:12:26.888: E/AndroidRuntime(2600):     at dalvik.system.NativeStart.main(Native Method)
08-18 02:12:26.888: E/AndroidRuntime(2600): Caused by: java.lang.SecurityException: No active admin ComponentInfo{dilli.passwordtest/dilli.passwordtest.MainActivity}
08-18 02:12:26.888: E/AndroidRuntime(2600):     at android.os.Parcel.readException(Parcel.java:1327)
08-18 02:12:26.888: E/AndroidRuntime(2600):     at android.os.Parcel.readException(Parcel.java:1281)
08-18 02:12:26.888: E/AndroidRuntime(2600):     at android.app.admin.IDevicePolicyManager$Stub$Proxy.setPasswordQuality(IDevicePolicyManager.java:761)
08-18 02:12:26.888: E/AndroidRuntime(2600):     at android.app.admin.DevicePolicyManager.setPasswordQuality(DevicePolicyManager.java:292)
08-18 02:12:26.888: E/AndroidRuntime(2600):     at dilli.passwordtest.MainActivity.onCreate(MainActivity.java:24)
08-18 02:12:26.888: E/AndroidRuntime(2600):     at android.app.Activity.performCreate(Activity.java:4465)
08-18 02:12:26.888: E/AndroidRuntime(2600):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-18 02:12:26.888: E/AndroidRuntime(2600):     at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
08-18 02:12:26.888: E/AndroidRuntime(2600):     ... 11 more

I don't have clear clarification in that. So, Could some buddy please help me. Thanks in advance.

Caused by: java.lang.SecurityException: No active admin ComponentInfo{dilli.passwordtest/dilli.passwordtest.MainActivity}

In order to modify device administrator settings, your app has to be approved by the user as a device administrator. You need to have implemented a DeviceAdminReceiver with the appropriate policies, and the user has to have gone into the security screen in Settings and activated your application as a device administration app.

The SDK provides a complete example on how to use Device Administrator on android:

<SDK_PATH>/samples/android-21/legacy/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.java

The example is also available in other API's.

PS: Dont forget to review the manifest :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM