Device owner on a nonrooted device (Android L), without NFC, using adb shell, dpm set-device-owner

Rin malavi picture Rin malavi · Apr 23, 2015 · Viewed 11.4k times · Source

Final intention here is to have a device in 'kiosk mod'.

They say you don't need NFC nor rooting to achieve application becoming device owner. I've yet to see a full example of this method but lets try:

adb shell dpm set-device-owner <package>/.<ReceiverImplementation>

should do... So I do so, and get:

java.lang.SecurityException: 
Neither user 2000 nor current process has android.permission.BIND_DEVICE_ADMIN.

Following code, therefore, returns false.

((DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE))
   .isDeviceOwnerApp(getApplicationContext().getPackageName())

This STO question poses a similar question but doesn't specify an actual failure..

Manifest file and the rest of the source is mostly inspired from this google sample

<manifest
    package="com.example.android.deviceowner"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionCode="1"
    android:versionName="1.0">

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

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

        <receiver
            android:name=".DeviceOwnerReceiver"
            android:description="@string/app_name"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_DEVICE_ADMIN">
            <meta-data
                android:name="android.app.device_admin"
                android:resource="@xml/device_owner_receiver"/>
            <intent-filter>
                <action android:name="android.app.action.ACTION_DEVICE_ADMIN_ENABLED"/>
            </intent-filter>
        </receiver>

    </application>

</manifest>

Device I am trying to do this currently is LG G Pad.

Answer

Florent Dupont picture Florent Dupont · Apr 28, 2015

Your manifest file seems correct. You should be aware that it may come from the state of your system when you are executing this command. Many points should be checked before running the dpm command successfully :

  • Make sure your app is already installed, like any other casual app
  • Make sure that no account is already set for the current user (make sure no account is set in Settings > Accounts) before its use.
  • there shouldn't be an existing device owner already registered

The best thing to do (That's what I did when experimenting indeed) is to completely factory-reboot your phone and avoid most configuration steps (except the mandatories steps "configure Wi-Fi", and "Name"), and do not associate any Google account.
Once provisioned, you are sure to be in a clean state. Then,

  1. Activate Debugging
  2. install your app with your IDE (or with pm install...)
  3. run the command adb shell dpm set-device-owner ...

I've written an article explaining most of these steps on my blog, take a look at it, it may be useful in your case.