2011-10-21 2 views
1

저는 Android에서 새롭기 때문에 휴대 전화를 잠그고 싶습니다. 그래서 나는 Device Admin 문서를 읽는다. 내 코드를 이렇게 보입니다. 내가 그것을 내가Android 기기 관리자가 작동하지 않습니다.

내 manifest.xml

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
<activity android:name=".MainLauncher"android:label="@string/app_name"> 
<intent-filter> 
<action android:name="android.intent.action.MAIN" /> 
<category android:name="android.intent.category.LAUNCHER" /> 
</intent-filter> 
</activity> 
<activity android:name=".Controller"android:label="@string/app_name"> 
</activity> 

<activity android:name=".DeviceAdminSample$Controller"android:label="@string/activity_sample_device_admin"> 
<intent-filter> 
<action android:name="android.intent.action.MAIN" /> 
<category android:name="android.intent.category.SAMPLE_CODE" /> 
</intent-filter> 

</activity> 

<receiver android:name=".DeviceAdminSample"android:label="@string/sample_device_admin"android:description="@string/sample_device_admin_description"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> 
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 
</manifest> 

내 코드

public class DeviceAdminSample extends DeviceAdminReceiver { 

public static class Controller extends Activity { 
      static final int RESULT_ENABLE = 1; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE); 
     mAM = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); 
     mDeviceAdminSample = new ComponentName(Controller.this, DeviceAdminSample.class); 

     mEnableButton = (Button)this.findViewById(R.id.button1); 
     mEnableButton.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       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); 
      } 
     }); 

     mDisableButton = (Button)this.findViewById(R.id.button2); 
     mDisableButton.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       mDPM.removeActiveAdmin(mDeviceAdminSample); 
       updateButtonStates(); 
      } 
     }); 

     mForceLockButton = (Button)this.findViewById(R.id.button3); 
     mForceLockButton.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       Log.i("                           In","In the forc lock button"); 
       if (mAM.isUserAMonkey()) { 
        AlertDialog.Builder builder = new AlertDialog.Builder(Controller.this); 
        builder.setMessage("You can't lock my screen because you are a monkey!"); 
        builder.setPositiveButton("I admit defeat", null); 
        builder.show(); 
        return; 
       } 
       boolean active = mDPM.isAdminActive(mDeviceAdminSample); 
       if (active) { 
       v mDPM.lockNow(); 
       } 
      } 
     }); 
    } 
    void updateButtonStates() { 
     boolean active = mDPM.isAdminActive(mDeviceAdminSample); 
     if (active) { 
      mForceLockButton.setEnabled(true); 
     } else { 
      mForceLockButton.setEnabled(false); 
     } 
    } 
} 
} 

내 다른 작업을 수행하는 것이 정의가있는 경우 그러나 내 매니페스트에 통지하는 것 나던 제안 활동

package com.examples; 


public class MainLauncher extends Activity { 


@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.mainlauncher); 

Button btnLock = (Button)this.findViewById(R.id.button22); 
btnLock.setOnClickListener(new OnClickListener() { 

    public void onClick(View v) { 
     KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE); 
     KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE); 

     lock.reenableKeyguard(); 

    } 
}); 

Button btnDevice = (Button)this.findViewById(R.id.button11); 
btnDevice.setOnClickListener(new OnClickListener() { 

    public void onClick(View arg0) { 
    try{      
     Intent openScreen = new Intent(); 
     openScreen.setClass(MainLauncher.this, DeviceAdminSample.class); 
     startActivity(openScreen); 
    } 
     catch(Exception e) 
     {Log.i("DeviceAdmin button",String.valueOf(e));} 
    } 
}); 
} 


} 
+0

여기에 주석 처리 된 코드를 붙여 넣지 말고 사용자를 '원숭이'라고 지칭하지 마십시오 (때로는 조금 두꺼울 수도 있음). 약간의 존경심을 표시하십시오 :-) – fredley

+0

따르기 [링크] [1] 장치 관리자 앱을 개발하기 위해 는 [1] : http://stackoverflow.com/questions/13450986/device-administrative-android-app-implementation –

+1

@TomMedley 원숭이 모드는 도구입니다 앱 테스트 용. OP가 사용자를위한 것은 아닙니다. http://developer.android.com/tools/help/monkey.html을 참조하십시오. – Sundeep

답변

1

ave "device_admin_sample.xml"이라는 "xml"폴더 아래에 리소스를 추가 했습니까? 장치 관리자가 작동하도록 "강제 잠금"을 수행하는 데 필요한 권한을가집니다.

관련 문제