13

I need to know how to set my application as Device owner in Android 5.0, 4.4 and 4.3(?). I've yet tried the method for rooted devices (described in there), successfully. I saw that works great in android 5.0 and 4.4.2 emulator and in CyanoGen AOSP 4.4.4 (all rooted devices). But I must need to try this on other non rooted devices, in Android 5.0 Developer API you can read this

"To deploy and activate a device owner, you must perform an NFC data transfer from a programming app to the device while the device is in its unprovisioned state."

but I don't understand what it means, or better, what I've to do. Can someone help me, or explain me the step to do?

PS. I know what NFC is and how it works but I can't understand how to use for this issue.

frogatto
  • 28,539
  • 11
  • 83
  • 129
alex_au
  • 240
  • 1
  • 2
  • 11

2 Answers2

25

Create a NFC trigger application and install that on a device (other than the one on which you want to make your app as device owner) having NFC.

Following is the code for NFC trigger

public class MainActivity extends Activity implements CreateNdefMessageCallback {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
        nfcAdapter.setNdefPushMessageCallback(this, this);
    }

    @Override
    public NdefMessage createNdefMessage(NfcEvent event) {
        try {
            Properties p = new Properties();

            p.setProperty(
                    DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME,
                    "apk package name");
            p.setProperty(
                    DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION,
                    "app download url");
            p.setProperty(
                    DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM,
                    "apk checksum");
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            OutputStream out = new ObjectOutputStream(bos);
            p.store(out, "");
            final byte[] bytes = bos.toByteArray();

            NdefMessage msg = new NdefMessage(NdefRecord.createMime(
                    DevicePolicyManager.MIME_TYPE_PROVISIONING_NFC, bytes));
            return msg;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

For checksum run following command

cat your_device_owner_app_name.apk | openssl dgst -binary -sha1 | openssl base64 | tr '+/' '-_' | tr -d '='​

  • Paste the generated checksum in NFC trigger code.
  • Compile and run NFC trigger app on device.

Now upload your application apk which you want to make as device owner on google drive or dropbox.

Take a fresh device or factory reset the device on which you want to set your application as device owner.

Reboot the device and on first screen bring your device containing NFC trigger application and touch for beam transfer.

Your application will be downloaded and will get installed as device owner.

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
Akhil
  • 829
  • 1
  • 11
  • 26
  • 1
    Yes, this work, I tried it and is well. this was what I need and in this days I tried other method with NFC and without. I Just found a method to provision without root and without using NFC tag and then I go for testing that – alex_au Nov 19 '14 at 17:13
  • @alex_au can you please share the new way you found, thank you – Vishal Santharam Nov 20 '14 at 05:49
  • yes sure, you have to replicate the TAG_DISCOVERED action in new intent, and in this put the extras: `NfcAdapter.EXTRA_TAG`, `NfcAdapter.EXTRA_NDEF_MESSAGES` and `NfcAdapter.EXTRA_ID`, with the correct value, I have created this intent referring the Intent that android send when new TAG is Discovered, and I populate them with correct data. – alex_au Nov 20 '14 at 12:03
  • Another solution for development is to use [adb shell dpm](http://stackoverflow.com/a/27909315/1489493). No root, NFC or even two devices required. – Hartok Jan 15 '15 at 21:53
  • Hi @Spynet, i have used same code for installing device owner app using NFC triggering application, but it gives me the couldn't setup your admin app due to CheckSum error Please let me know where i am doing wrong. I have posted code in second Comments: – sunil jain Jan 22 '15 at 08:37
  • Properties p = new Properties(); p.setProperty(DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME,"com.marakana.android.devicepolicydemo");p.setProperty(DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION,"https://www.dropbox.com/s/0mpolgzm0nufnd6/DevicePolicyDemo_2.1.apk?dl=0"); p.setProperty(DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM,"4tEyGrxvIf9XBgGHxneg62a6hAQ"); – sunil jain Jan 22 '15 at 08:38
  • 2
    Hi @Sunil Jain, your checksum is correct. In your dropbox url replace dl=0 with dl=1 and then try again. :) – Akhil Jan 23 '15 at 04:54
  • Thank you very much @Spynet it's worked and app is install successfully.....could you let me know what should i do to enable app like camera etc. because when device is setup only few apps are installed showing in Running app list. Thanks Once Again – sunil jain Jan 23 '15 at 08:32
  • @Sunil Jain, I am figuring that out will let you know why they are not present after provisioning :) Plz upvote the answer :) – Akhil Jan 23 '15 at 09:59
  • Hi @alex_au i have installed device owner app with NFC, now i want to install it without NFC could you let me know how you have installed device owner app without NFC, i have tried options that you have mentioned in comment but did not get success..... – sunil jain Jan 23 '15 at 10:00
  • @sunil jain you can use "adb shell dpm" command :) Link: http://florent-dupont.blogspot.fr/2015/01/android-shell-command-dpm-device-policy.html – Akhil Jan 23 '15 at 10:05
  • Another solution needs rooting of device: http://stackoverflow.com/questions/21183328/how-to-make-my-app-a-device-owner/26839548#26839548 – Akhil Jan 23 '15 at 10:07
  • @Spynet, it's showing only a few apps installed but when i tried to install any system app from google play market, it got installed... – sunil jain Jan 23 '15 at 10:21
  • Hi @alex_au, did you get success to make device owner without using NFC. – sunil jain Jan 25 '15 at 07:16
  • Hello, did any of you figure out how to enable apps such as camera? – Gruntcakes May 15 '15 at 15:50
  • @Spynet Can we update it OTA after making device owner, I have posted a seperate question here http://stackoverflow.com/questions/31041832/ota-updates-for-device-owner-applicationkiosk-mode – Shubham Jun 25 '15 at 13:08
1

If it's needed, it's also possible to set a device-owner with adb as mentioned here: http://sdgsystems.com/blog/implementing-kiosk-mode-android-part-3-android-lollipop

B_Nut
  • 68
  • 1
  • 5
  • It's better to describe the solution here and give attribution to the original source. – hoss Aug 27 '15 at 18:41