我想在按钮单击时锁定应用程序的屏幕
我已将代码编写为
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