我一直在嘗試使用鎖屏概念作爲幹運行的應用程序我創建了一個應用程序,該應用程序在點擊給定按鈕後鎖定屏幕,以及我使用的基本概念沒有什麼新的,這裏是我的Java代碼方法OnCreate(bundle)永遠不會被調用和應用程序崩潰
package com.example.gaurav.locknowtest2;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.admin.DevicePolicyManager;
import android.app.admin.DeviceAdminReceiver;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends DeviceAdminReceiver{
public class controller extends Activity{
DevicePolicyManager dpm;
ComponentName comname;
Button b1;
TextView display;
public void OnCreate(Bundle xyz){
super.onCreate(xyz);
dpm=(DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
comname=new ComponentName(this,MainActivity.class);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.start);
display=(TextView)findViewById(R.id.xyz);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, comname);
startActivityForResult(intent,1);
display.setText("just to test the method");
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
if (resultCode == Activity.RESULT_OK) {
dpm.lockNow();
} else {
Log.i("DeviceAdminSample", "Administration enable FAILED!");
}
return;
}
}
}
}
下了起來,這裏是我的清單,我認爲是創造一切問題
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gaurav.locknowtest2" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity$controller"
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=".MainActivity"
android:permission="android.permission.BIND_DEVICE_ADMIN" >
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_admin_sample" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
</application>
</manifest>
我需要得到這個這個做一週,所以請看看..
請發佈崩潰日誌。 – Antrromet
'controller'是一個擴展了'Activity'的內部類。我不確定你是否可以這樣做。 –
@PaulBoddington不,我相當肯定你可以做到這一點,只要它是一個靜態的內部類,即使這樣你就必須在清單中用$符號指定它 – EpicPandaForce