gpt4 book ai didi

android - 锁定问题

转载 作者:行者123 更新时间:2023-11-30 03:49:49 24 4
gpt4 key购买 nike

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

我把代码写成

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com