2013-05-05 81 views
1

我想製作一個應用程序來禁用相機,我試圖從SDK的設備管理員示例,它在模擬器上正常工作,但在設備上,app force關閉項目點擊並顯示NoSuchmethod錯誤。設備管理API示例:在模擬器上正常工作設備上的NoSuchMethod錯誤

代碼段

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); 
    } 
} 

logcat的

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) 

請幫助。 !我不是從很長一段時間我的問題:(提前

+0

您確定有問題的設備支持嗎?它運行的是什麼版本的Android? – 2013-05-05 19:09:00

+0

@ChrisStratton我一直保持目標爲Jellybean,它在Jellybean的模擬器上工作,我的設備是ICS,ICS支持DeviceAdmin API,不能理解 – Prax 2013-05-05 19:12:34

回答

2

雖然一些設備管理員API的較早加入,具體方法getKeyguardDisabledFeatures()僅在API 17中添加,即使用Android變得令人滿意的答案

謝謝Android 4.2或JELLY_BEAN_MR1以及最近發佈的最新版本

因此,您將無法在您的ICS測試設備上使用該方法,或者在2013年5月之前用戶擁有的許多設備

想必您的成功測試是在模擬器運行4.2或4.2.2

+0

因此我不能在ICS上禁用攝像頭?我從哪裏可以獲得API 14樣本(ICS)呢? – Prax 2013-05-05 19:22:49

+0

我是Android開發新手,請指導,謝謝 – Prax 2013-05-05 19:23:16

+1

Android工具可讓您安裝API 14的示例,但我不知道這些示例是否包含禁用相機的方法。 – 2013-05-05 19:27:06

相關問題