2015-01-13 134 views
3

我試圖啓用我的應用程序作爲設備管理員。試圖調用這給我的應用程序的許可活動是設備管理員這是在我的應用程序的第一個活動的代碼如下:Android設備管理員權限活動未啓動

import android.app.Activity; 
import android.app.admin.DevicePolicyManager; 
import android.content.ComponentName; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

import com.google.android.gms.common.GooglePlayServicesUtil; 

public class RegisterActivity extends Activity { 
    // alert dialog manager 
    AlertDialogManager alert = new AlertDialogManager(); 

    // Internet detector 
    ConnectionDetector cd; 

    // UI elements 
    EditText txtName; 
    EditText txtEmail; 

    // Register button 
    Button btnRegister; 

    static final int ACTIVATION_REQUEST = 47; //Request ID for Device Administrator 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_register); 
     //Unrelated 
     int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
     if (status != 0) { 
      Toast.makeText(this, "This device is not supported - please download Google Play Services.", 
         Toast.LENGTH_LONG).show(); 
     } 

     //Begin enabling device administrator 
     Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); 
     ComponentName deviceAdminComponentName = new ComponentName(this, DeviceAdmin.class); 
     intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceAdminComponentName); 
     intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "You must enable device administration for certain features" 
       + " of the app to function."); 

     //I thought that this would start the activity that lets the user 
     //choose whether to enable the app as a device admin 
     startActivityForResult(intent, ACTIVATION_REQUEST); 



     //Also contains code pertaining to unrelated Google Cloud messaging features 
     //and an onClick() method, all inside of onCreate() 


     } 


@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch (requestCode) { 
     case ACTIVATION_REQUEST: 
      if (resultCode == Activity.RESULT_OK) { 
       Log.i("DeviceAdminSample", "Administration enabled!"); 
      } 
      else { 
       Log.i("DeviceAdminSample", "Administration enable FAILED!"); 
      } 
      return; 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
} 

使用此代碼,當我運行我的應用程序的活動來了,然後使動畫看起來像是在打開一個新的活動,但隨後該動畫停止並且保持在當前活動狀態,而不提示用戶啓用設備管理員。我的任何關於設備管理員的日誌消息都沒有被擊中。但是,在「設置」 - >「安全」 - >「設備管理員」頁面上查看當前設備管理員時,我的應用會顯示,但未作爲設備管理員進行檢查。

這裏是我的DeviceAdmin子類的代碼,雖然我不能看到那裏的問題可以從這些代碼來:

import android.app.admin.DeviceAdminReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 

public class DeviceAdmin extends DeviceAdminReceiver { 

    @Override 
    public void onEnabled(Context context, Intent intent) { 
     super.onEnabled(context, intent); 
     Log.i("Device Admin: ", "Enabled"); 
    } 

    @Override 
    public String onDisableRequested(Context context, Intent intent) { 
     return "Admin disable requested"; 
    } 

    @Override 
    public void onDisabled(Context context, Intent intent) { 
     super.onDisabled(context, intent); 
     Log.i("Device Admin: ", "Disabled"); 
    } 

    @Override 
    public void onPasswordChanged(Context context, Intent intent) { 
     super.onPasswordChanged(context, intent); 
     Log.i("Device Admin: ", "Password changed"); 
    } 

} 

任何幫助將不勝感激!

更新:在我的Android清單中的設備管理接收者如下:

<!-- Device Administration Receiver --> 
    <receiver 
     android:name="com.myPackage.DeviceAdminReceiver" 
     android:permission="android.permission.BIND_DEVICE_ADMIN"> 
     <meta-data android:name="android.app.device_admin" 
        android:resource="@xml/device_admin" /> 
     <intent-filter> 
      <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /> 
      <action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLE_REQUESTED" /> 
      <action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLED" /> 
     </intent-filter>   
    </receiver>   

回答

1

我在Jagadesh的幫助下發現了問題。

在我的Android清單文件,我設置了接收器的名稱是DeviceAdminReceiver,因爲在這裏看到

<receiver 
    android:name="com.myPackage.DeviceAdminReceiver" 
    android:permission="android.permission.BIND_DEVICE_ADMIN"> 

我是這樣做是錯誤的接收器應攜帶類的名稱DeviceAdminReceiver代替我寫了一個擴展DeviceAdminReceiverDeviceAdmin

更新,工作R中的子類的名字清單文件中的收件人是:

<!-- Device Administration Receiver --> 
    <receiver 
     android:name="com.myPackage.DeviceAdmin" 
     android:permission="android.permission.BIND_DEVICE_ADMIN"> 
     <meta-data android:name="android.app.device_admin" 
        android:resource="@xml/device_admin" /> 
     <intent-filter> 
      <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /> 
      <action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLE_REQUESTED" /> 
      <action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLED" /> 
     </intent-filter>   
    </receiver>   
5

我想你忘了加上接收器的清單與正確的意圖過濾

<receiver 
     android:name="com.yourpackage.DeviceAdmin" 
     android:permission="android.permission.BIND_DEVICE_ADMIN" > 
     <intent-filter> 
      This action is required 

      <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /> 
     </intent-filter> 
     This is required this receiver to become device admin component. 

     <meta-data 
      android:name="android.app.device_admin" 
      android:resource="@xml/device_admin" /> 
    </receiver> 

而XMl

<device-admin xmlns:android="http://schemas.android.com/apk/res/android" > 

<uses-policies> 
    <limit-password /> 

    <watch-login /> 

    <reset-password /> 

    <force-lock /> 

    <wipe-data /> 

    <expire-password /> 

    <encrypted-storage /> 

    <disable-camera /> 
</uses-policies> 

</device-admin> 

有關Device Adminstration的更多信息。 Check this

相關問題