gpt4 book ai didi

android - 设备管理 API 示例 : works fine on emulator NoSuchMethod error on Device

转载 作者:行者123 更新时间:2023-11-29 21:43:45 26 4
gpt4 key购买 nike

我正在尝试制作一个禁用相机的应用程序,我正在尝试 SDK 中的 Device Admin 示例,它在模拟器上运行良好但在设备上,应用程序强制关闭项目点击并显示 NoSuchmethod 错误。

代码 fragment

 public static class AdminSampleFragment extends PreferenceFragment
implements OnPreferenceChangeListener, OnPreferenceClickListener{

// Useful instance variables
protected DeviceAdmin mActivity;
protected DevicePolicyManager mDPM;
protected ComponentName mDeviceAdminSample;
protected boolean mAdminActive;

// Optional shared UI
private PreferenceScreen mSetPassword;
private EditTextPreference mResetPassword;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

// Retrieve the useful instance variables
mActivity = (DeviceAdmin) getActivity();
mDPM = mActivity.mDPM;
mDeviceAdminSample = mActivity.mDeviceAdminSample;
mAdminActive = mActivity.isActiveAdmin();

// Configure the shared UI elements (if they exist)
mResetPassword = (EditTextPreference) findPreference(KEY_RESET_PASSWORD);
mSetPassword = (PreferenceScreen) findPreference(KEY_SET_PASSWORD);

if (mResetPassword != null) {
mResetPassword.setOnPreferenceChangeListener(this);
}
if (mSetPassword != null) {
mSetPassword.setOnPreferenceClickListener(this);
}
}

@Override
public void onResume() {
super.onResume();
mAdminActive = mActivity.isActiveAdmin();
reloadSummaries();
// Resetting the password via API is available only to active admins
if (mResetPassword != null) {
mResetPassword.setEnabled(mAdminActive);
}
}

/**
* Called automatically at every onResume. Should also call explicitly any time a
* policy changes that may affect other policy values.
*/
protected void reloadSummaries() {
if (mSetPassword != null) {
if (mAdminActive) {
// Show password-sufficient status under Set Password button
boolean sufficient = mDPM.isActivePasswordSufficient();
mSetPassword.setSummary(sufficient ?
R.string.password_sufficient : R.string.password_insufficient);
} else {
mSetPassword.setSummary(null);
}
}
}

@Override
public boolean onPreferenceClick(Preference preference) {
if (mSetPassword != null && preference == mSetPassword) {
Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
startActivity(intent);
return true;
}
return false;
}

@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (mResetPassword != null && preference == mResetPassword) {
doResetPassword((String)newValue);
return true;
}
return false;
}

/**
* This is dangerous, so we prevent automated tests from doing it, and we
* remind the user after we do it.
*/
private void doResetPassword(String newPassword) {
if (alertIfMonkey(mActivity, R.string.monkey_reset_password)) {
return;
}
mDPM.resetPassword(newPassword, DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
String message = mActivity.getString(R.string.reset_password_warning, newPassword);
builder.setMessage(message);
builder.setPositiveButton(R.string.reset_password_ok, null);
builder.show();
}

/**
* Simple helper for summaries showing local & global (aggregate) policy settings
*/
protected String localGlobalSummary(Object local, Object global) {
return getString(R.string.status_local_global, local, global);
}
}

/**
* PreferenceFragment for "general" preferences.
*/
public static class GeneralFragment extends AdminSampleFragment
implements OnPreferenceChangeListener {
// UI elements
private CheckBoxPreference mEnableCheckbox;
private CheckBoxPreference mDisableCameraCheckbox;
private CheckBoxPreference mDisableKeyguardWidgetsCheckbox;
private CheckBoxPreference mDisableKeyguardSecureCameraCheckbox;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.device_admin_general);
mEnableCheckbox = (CheckBoxPreference) findPreference(KEY_ENABLE_ADMIN);
mEnableCheckbox.setOnPreferenceChangeListener(this);
mDisableCameraCheckbox = (CheckBoxPreference) findPreference(KEY_DISABLE_CAMERA);
mDisableCameraCheckbox.setOnPreferenceChangeListener(this);
mDisableKeyguardWidgetsCheckbox =
(CheckBoxPreference) findPreference(KEY_DISABLE_KEYGUARD_WIDGETS);
mDisableKeyguardWidgetsCheckbox.setOnPreferenceChangeListener(this);
mDisableKeyguardSecureCameraCheckbox =
(CheckBoxPreference) findPreference(KEY_DISABLE_KEYGUARD_SECURE_CAMERA);
mDisableKeyguardSecureCameraCheckbox.setOnPreferenceChangeListener(this);
}

// At onResume time, reload UI with current values as required
@Override
public void onResume() {
super.onResume();
mEnableCheckbox.setChecked(mAdminActive);
enableDeviceCapabilitiesArea(mAdminActive);

if (mAdminActive) {
mDPM.setCameraDisabled(mDeviceAdminSample, mDisableCameraCheckbox.isChecked());
mDPM.setKeyguardDisabledFeatures(mDeviceAdminSample, createKeyguardDisabledFlag());
reloadSummaries();
}
}

int createKeyguardDisabledFlag() {
int flags = DevicePolicyManager.KEYGUARD_DISABLE_FEATURES_NONE;
flags |= mDisableKeyguardWidgetsCheckbox.isChecked() ?
DevicePolicyManager.KEYGUARD_DISABLE_WIDGETS_ALL : 0;
flags |= mDisableKeyguardSecureCameraCheckbox.isChecked() ?
DevicePolicyManager.KEYGUARD_DISABLE_SECURE_CAMERA : 0;
return flags;
}

@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (super.onPreferenceChange(preference, newValue)) {
return true;
}
boolean value = (Boolean) newValue;
if (preference == mEnableCheckbox) {
if (value != mAdminActive) {
if (value) {
// Launch the activity to have the user enable our admin.
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdminSample);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
mActivity.getString(R.string.add_admin_extra_app_text));
startActivityForResult(intent, REQUEST_CODE_ENABLE_ADMIN);
// return false - don't update checkbox until we're really active
return false;
} else {
mDPM.removeActiveAdmin(mDeviceAdminSample);
enableDeviceCapabilitiesArea(false);
mAdminActive = false;
}
}
} else if (preference == mDisableCameraCheckbox) {
mDPM.setCameraDisabled(mDeviceAdminSample, value);
reloadSummaries();
} else if (preference == mDisableKeyguardWidgetsCheckbox
|| preference == mDisableKeyguardSecureCameraCheckbox) {
mDPM.setKeyguardDisabledFeatures(mDeviceAdminSample, createKeyguardDisabledFlag());
reloadSummaries();
}
return true;
}

@Override
protected void reloadSummaries() {
super.reloadSummaries();
String cameraSummary = getString(mDPM.getCameraDisabled(mDeviceAdminSample)
? R.string.camera_disabled : R.string.camera_enabled);
mDisableCameraCheckbox.setSummary(cameraSummary);

int disabled = mDPM.getKeyguardDisabledFeatures(mDeviceAdminSample);

String keyguardWidgetSummary = getString(
(disabled & DevicePolicyManager.KEYGUARD_DISABLE_WIDGETS_ALL) != 0 ?
R.string.keyguard_widgets_disabled : R.string.keyguard_widgets_enabled);
mDisableKeyguardWidgetsCheckbox.setSummary(keyguardWidgetSummary);

String keyguardSecureCameraSummary = getString(
(disabled & DevicePolicyManager.KEYGUARD_DISABLE_SECURE_CAMERA) != 0 ?
R.string.keyguard_secure_camera_disabled : R.string.keyguard_secure_camera_enabled);
mDisableKeyguardSecureCameraCheckbox.setSummary(keyguardSecureCameraSummary);
}

/** Updates the device capabilities area (dis/enabling) as the admin is (de)activated */
private void enableDeviceCapabilitiesArea(boolean enabled) {
mDisableCameraCheckbox.setEnabled(enabled);
mDisableKeyguardWidgetsCheckbox.setEnabled(enabled);
mDisableKeyguardSecureCameraCheckbox.setEnabled(enabled);
}
}

日志

05-06 00:19:00.797: E/AndroidRuntime(9614): FATAL EXCEPTION: main


05-06 00:19:00.797: E/AndroidRuntime(9614): java.lang.NoSuchMethodError: android.app.admin.DevicePolicyManager.getKeyguardDisabledFeatures
05-06 00:19:00.797: E/AndroidRuntime(9614): at com.example.deviceadmin.DeviceAdmin$GeneralFragment.reloadSummaries(DeviceAdmin.java:317)
05-06 00:19:00.797: E/AndroidRuntime(9614): at com.example.deviceadmin.DeviceAdmin$AdminSampleFragment.onResume(DeviceAdmin.java:159)
05-06 00:19:00.797: E/AndroidRuntime(9614): at com.example.deviceadmin.DeviceAdmin$GeneralFragment.onResume(DeviceAdmin.java:256)
05-06 00:19:00.797: E/AndroidRuntime(9614): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:877)
05-06 00:19:00.797: E/AndroidRuntime(9614): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)
05-06 00:19:00.797: E/AndroidRuntime(9614): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1018)
05-06 00:19:00.797: E/AndroidRuntime(9614): at android.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:1810)
05-06 00:19:00.797: E/AndroidRuntime(9614): at android.app.Activity.performResume(Activity.java:4549)
05-06 00:19:00.797: E/AndroidRuntime(9614): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2461)
05-06 00:19:00.797: E/AndroidRuntime(9614): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2499)
05-06 00:19:00.797: E/AndroidRuntime(9614): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1997)
05-06 00:19:00.797: E/AndroidRuntime(9614): at android.app.ActivityThread.access$600(ActivityThread.java:127)
05-06 00:19:00.797: E/AndroidRuntime(9614): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
05-06 00:19:00.797: E/AndroidRuntime(9614): at android.os.Handler.dispatchMessage(Handler.java:99)
05-06 00:19:00.797: E/AndroidRuntime(9614): at android.os.Looper.loop(Looper.java:137)
05-06 00:19:00.797: E/AndroidRuntime(9614): at android.app.ActivityThread.main(ActivityThread.java:4448)
05-06 00:19:00.797: E/AndroidRuntime(9614): at java.lang.reflect.Method.invokeNative(Native Method)
05-06 00:19:00.797: E/AndroidRuntime(9614): at java.lang.reflect.Method.invoke(Method.java:511)
05-06 00:19:00.797: E/AndroidRuntime(9614): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
05-06 00:19:00.797: E/AndroidRuntime(9614): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
05-06 00:19:00.797: E/AndroidRuntime(9614): at dalvik.system.NativeStart.main(Native Method)

请帮忙。 !我的问题很长一段时间都没有得到满意的答案:(

提前致谢

最佳答案

虽然前面添加了一些Device Admin API,但是具体方法getKeyguardDisabledFeatures()仅在 API 17 中添加,即 android Android 4.2 或 JELLY_BEAN_MR1 以及撰写本文时发布的最新版本。

因此,您将无法在您的 ICS 测试设备上使用该方法,或在 2013 年 5 月用户手中的许多设备上使用该方法。

大概您的成功测试是在运行 4.2 或 4.2.2 的模拟器上进行的

关于android - 设备管理 API 示例 : works fine on emulator NoSuchMethod error on Device,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16388074/

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