android - 锁定问题

标签 android

我想在点击按钮时锁定应用程序的屏幕

我把代码写成

public class MainActivity extends DeviceAdminReceiver{


public void makeTest()
{
    System.out.println("Entered Main Class");
}
public class Controller extends Activity {

static final int RESULT_ENABLE = 1;
Button powerOff;
int amountOfTime =20*1000;
Context context = this;
DevicePolicyManager mDPM;
ActivityManager mAM;
ComponentName mDeviceAdminSample;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    System.out.println("Enterd inner Class");
    mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
    mAM = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
    mDeviceAdminSample = new ComponentName( Controller.this,MainActivity.class);


    powerOff = (Button)findViewById(R.id.button1);
    powerOff.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
              System.out.println("Entered Button Click");
              mDPM.lockNow();


                Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
                        mDeviceAdminSample);
                intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                        "Additional text explaining why this needs to be added.");
                startActivityForResult(intent, RESULT_ENABLE);


        }
    });


}

它显示 logcat 输出的某些错误

 01-15 15:51:12.043: E/AndroidRuntime(8148): FATAL EXCEPTION: main
 01-15 15:51:12.043: E/AndroidRuntime(8148): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.screenlock/com.example.screenlock.MainActivity}: java.lang.ClassCastException: com.example.screenlock.MainActivity cannot be cast to android.app.Activity
 01-15 15:51:12.043: E/AndroidRuntime(8148):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
 01-15 15:51:12.043: E/AndroidRuntime(8148):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
 01-15 15:51:12.043: E/AndroidRuntime(8148):    at android.app.ActivityThread.access$600(ActivityThread.java:139)
 01-15 15:51:12.043: E/AndroidRuntime(8148):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
 01-15 15:51:12.043: E/AndroidRuntime(8148):    at android.os.Handler.dispatchMessage(Handler.java:99)
 01-15 15:51:12.043: E/AndroidRuntime(8148):    at android.os.Looper.loop(Looper.java:156)
 01-15 15:51:12.043: E/AndroidRuntime(8148):    at android.app.ActivityThread.main(ActivityThread.java:4987)
 01-15 15:51:12.043: E/AndroidRuntime(8148):    at java.lang.reflect.Method.invokeNative(Native Method)
 01-15 15:51:12.043: E/AndroidRuntime(8148):    at java.lang.reflect.Method.invoke(Method.java:511)
 01-15 15:51:12.043: E/AndroidRuntime(8148):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
 01-15 15:51:12.043: E/AndroidRuntime(8148):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
 01-15 15:51:12.043: E/AndroidRuntime(8148):    at dalvik.system.NativeStart.main(Native Method)
 01-15 15:51:12.043: E/AndroidRuntime(8148): Caused by: java.lang.ClassCastException: com.example.screenlock.MainActivity cannot be cast to android.app.Activity
 01-15 15:51:12.043: E/AndroidRuntime(8148):    at android.app.Instrumentation.newActivity(Instrumentation.java:1039)
 01-15 15:51:12.043: E/AndroidRuntime(8148):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
 01-15 15:51:12.043: E/AndroidRuntime(8148):    ... 11 more

最佳答案

DeviceAdminReceiver 是否扩展了 Activity ?如果不是,则日志表明这是您的问题。

编辑:可以看出here DeviceAdminReceiver 是一个 BroadcastReciever,它不是一个 Activity。您的 MainActivity 类必须扩展 ActivityActivity 的子类,这样系统才能运行它。

关于android - 锁定问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14335646/

相关文章:

java - Google Drive API - 在 ListView 中填充文件列表

Java android AsyncHttpClient 如何设置 header 'Accept' 'application/xml' 或 'application/json'

java - OpenGL ES 2.0 不绘制任何东西

java - Android 应用程序中 setText 的空对象引用

android - Firebase onTokenRefresh() 未被调用

android - 从 AsyncTask 调用 AsyncTask

android - 从 Pipeline 项目发布 Android Lint 结果

Android BLE onCharacteristicRead 和 onCharacteristicChanged 从未调用过

java - 将上下文传递给 Async 类

android - Paypal sdk- INSTRUMENT_DECLINE 错误