Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
86 views
in Technique[技术] by (71.8m points)

lockscreen - Lock Screen for android is not working properly using Android

for Lock Screen programmatically using Android. I got some good ideas from Stackoverflow previous questions regarding this, and I did some thing well, but when I run that code, there is No Exception and Error. but also , the screen didn't lock. kindly some one point my fault in this code My manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.top"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity 
        android:name=".LockActivity">
        </activity>
    <activity
        android:name=".LockActivity$Myclass"
        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=".LockActivity"
    android:label="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>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
</manifest>

my Class file

package net.top;


 import android.app.Activity;
import android.app.KeyguardManager;

import android.app.KeyguardManager.KeyguardLock;
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.os.PowerManager;
import android.widget.Toast;

public class LockActivity extends DeviceAdminReceiver{

public void onEnabled(Context context, Intent intent) {
    showToast(context, "Sample Device Admin: enabled");
    }

    @Override
    public CharSequence onDisableRequested(Context context, Intent intent) {
    return "This is an optional message to warn the user about disabling.";
    }

    @Override
    public void onDisabled(Context context, Intent intent) {
    showToast(context, "Sample Device Admin: disabled");
    }
    void showToast(Context context, CharSequence msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();

    }

public static class Myclass extends Activity {

protected PowerManager.WakeLock mWakeLock;
DevicePolicyManager mDPM;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  //  setContentView(R.layout.main);

    mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
    //  mDPM.lockNow();

    ComponentName mAdminName = new ComponentName(Myclass.this,LockActivity.class);

        if(!mDPM.isAdminActive(mAdminName))
        {
            Intent intent = new       
Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,    
mAdminName);
            //mDPM.lockNow();
        }
        else
        {
            mDPM.lockNow();
        }   



         }
}

}

I also added the My_admin.xml file

<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
    <limit-password />
    <watch-login />
    <reset-password />
    <force-lock />
    <wipe-data />
    <expire-password />
    <encrypted-storage />
    <disable-camera />
</uses-policies>
</device-admin>

I can't find what is goes wrong here. kindly help me to point out my mistake here. thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you can add this code into your Activity class, it's works fine for me

    if(!mDPM.isAdminActive(mAdminName))
        {
             intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
              intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"is locked");
              intent.putExtra("force-locked", DeviceAdminInfo.USES_POLICY_FORCE_LOCK);

            startActivityForResult(intent, 1);
            System.out.println("The Device Could not lock because device admin not enabled");
            //mDPM.lockNow();
        }
        else
        {
            System.out.println("The Device  device admin enabled");
             intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
              intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"onEnabled");
            mDPM.lockNow();
            mDPM.setMaximumTimeToLock(mAdminName, 0);
              intent.putExtra("force-locked", DeviceAdminInfo.USES_POLICY_FORCE_LOCK);

                startActivityForResult(intent, 1);

        }   

Hope this would be helpful for you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...