2016-12-16 82 views
0

我一直在努力通過亞行外殼DPM命令設置我的應用,設備所有者我的應用程序設置設備所有者但錯誤出來我無法在設備上

Error: Bad admin: ComponentInfo{com.example.oshao.autolock/com.example.oshao. 

autolock.AdminReceiver}

這裏是我的活動和表現

public class MainActivity extends AppCompatActivity { 

private DevicePolicyManager mDpm; 
private boolean isKioskModeEnabled = false; 
private Button btnEnabled; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    btnEnabled = (Button) findViewById(R.id.btnEnable); 
    btnEnabled.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      enableKioskMode(!isKioskModeEnabled); 
     } 
    }); 

    ComponentName deviceAdmin = new ComponentName(this, AdminReceiver.class); 
    mDpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); 
    if (!mDpm.isAdminActive(deviceAdmin)) { 
     Toast.makeText(this, "This ap is not a device admin", Toast.LENGTH_SHORT).show(); 
    } 

    if (mDpm.isDeviceOwnerApp(getPackageName())) { 
     mDpm.setLockTaskPackages(deviceAdmin, new String[]{getPackageName()}); 
    } else { 
     Toast.makeText(this, "This app is not the device owner", Toast.LENGTH_SHORT).show(); 
    } 
} 

private void enableKioskMode(boolean enabled) { 

    if (enabled) { 

     if (mDpm.isLockTaskPermitted(this.getPackageName())) { 

      startLockTask(); 
      isKioskModeEnabled = true; 
      btnEnabled.setText("Exit Kiosk Mode"); 

     } else { 
      Toast.makeText(this, "Kiosk Mode is not permitted", Toast.LENGTH_SHORT).show(); 
     } 
    } else { 

     stopLockTask(); 
     isKioskModeEnabled = false; 
     btnEnabled.setText("Enter Kiosk Mode"); 

    } 

} 

}

清單:

<?xml version="1.0" encoding="utf-8"?> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 


    <activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <receiver 
     android:name="com.example.oshao.autolock.AdminReceiver" 
     android:permission="android.permission.BIND_DEVICE_ADMIN"> 

     <meta-data 
      android:name="android.app.admin" 
      android:resource="@xml/device_admin"/> 
     <intent-filter> 
      <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /> 
     </intent-filter> 

    </receiver> 

</application> 

我不知道爲什麼誤差仍然在那裏,是因爲一個設備只能有一個設置設備所有者單一的應用程序? 另一個問題是,adb命令可以在沒有帳戶的設備上實現,並連接到PC以便在終端中輸入命令。我可以通過應用程序中的代碼在設備沒有紮根的情況下做到這一點,因爲我有幾個設備,它很難一一設置它們。謝謝

+0

可以有一個以上的應用爲DeviceAdmin。檢查此鏈接:https://developer.android.com/guide/topics/admin/device-admin.html –

+0

嘗試在清單中爲AdminReceiver添加說明,如下所示:'' –

+0

@AkhilSoman \t 我不認爲增加在描述XML文件將解決adb錯誤,我已經試過了,謝謝。 –

回答