我有设备跟踪器应用程序,用户需要登录才能分配设备。
但是,现在我希望用户在用户进行身份验证之前不应该使用该设备,并且应该可以使用,直到用户从该设备中退出?
我想要检索用户在解锁屏幕时输入的密码,并希望将其用于我的应用程序身份验证,这是自己的,如果密码不匹配,我将再次解锁屏幕。
我正在尝试关注代码,但仍然无法使用密码锁定和解锁屏幕。
public class AdminSettingsActivity extends Activity {
private EditText baseURLEditText;
private Button saveBaseURL;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
saveBaseURL = (Button) findViewById(R.id.saveBaseURLBtn);
baseURLEditText = (EditText) findViewById(R.id.base_url_edit);
tv = (TextView) findViewById(R.id.text_admin_message);
tv.append(" " + DeviceTrackerApp.DEFAULT_BASE_URL);
saveBaseURL.setOnClickListener(new OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
String url = baseURLEditText.getText().toString().trim();
if (!url.isEmpty()) {
try {
if (URLUtil.isValidUrl(url)) {
new ValidateServerURL(AdminSettingsActivity.this).execute(url);
} else {
showInvalidURLToast();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
protected void showInvalidURLToast() {
// get your custom_toast.xml ayout
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
// set a dummy image
ImageView image = (ImageView) layout.findViewById(R.id.image_invalid_pwd);
image.setImageResource(R.drawable.invalid_pwd);
// set a message
TextView text = (TextView) layout.findViewById(R.id.text_invalid_password);
text.setText("Invalid URL! Can't Save.");
// Toast...
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
public void setURLStatus(int statusCode) {
if (statusCode == 200) {
DeviceTrackerApp.setBaseURL(baseURLEditText.getText().toString());
Toast.makeText(getApplicationContext(), "Valid URL Saved Successful",
Toast.LENGTH_LONG).show();
tv.setText("\nYour Setting saved successfuly. Press back button to go home screen\n");
} else {
showInvalidURLToast();
baseURLEditText.setText("");
}
}
}
和我的DeviceTrackerAdmin类是
public class DeviceTrackerAdmin extends DeviceAdminReceiver {
static SharedPreferences getSamplePreferences(Context context) {
return context.getSharedPreferences(DeviceAdminReceiver.class.getName(), 0);
}
static String PREF_PASSWORD_QUALITY = "password_quality";
static String PREF_PASSWORD_LENGTH = "password_length";
static String PREF_MAX_FAILED_PW = "max_failed_pw";
void showToast(Context context, CharSequence msg) {
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
@Override
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");
}
@Override
public void onPasswordChanged(Context context, Intent intent) {
showToast(context, "Sample Device Admin: pw changed");
}
@Override
public void onPasswordFailed(Context context, Intent intent) {
showToast(context, "Sample Device Admin: pw failed");
}
@Override
public void onPasswordSucceeded(Context context, Intent intent) {
showToast(context, "Sample Device Admin: pw succeeded");
}
}
我还在AndroidMenifest.xml中定义了接收器
<receiver
android:name=".DeviceTrackerAdmin"
android:permission="android.permission.BIND_DEVICE_ADMIN" >
<meta-data
android:name="com.abc.devicetracker.DeviceTrackerApp"
android:resource="@layout/policies" >
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" >
</action>
</intent-filter>
</meta-data>
</receiver>
但是,每次onActivityResult()
`if (resultCode == RESULT_OK)` **OR** `if (resultCode == Activity.RESULT_OK)` condition fails as `resultCode is 0 only` So for Device locking I need to root my mobile.
答案 0 :(得分:0)
当您输入密码正确或密码不正确时,请使用以下代码
//Get the window from the context
WindowManager wm = Context.getSystemService(Context.WINDOW_SERVICE);
//Unlock if password is incorrect
http://developer.android.com/reference/android/app/Activity.html#getWindow()
Window window = getWindow();
window.addFlags(wm.LayoutParams.FLAG_DISMISS_KEYGUARD);
//Lock device if password is correct
DevicePolicyManager mDPM;
mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
答案 1 :(得分:0)
我找到了一个github项目,我正在按照我的要求对其进行修改。 感谢All的努力。