Device Owner on Android 5.0 (and others) whitout rooted devices, device provisioning by NFC

人走茶凉 提交于 2019-11-27 11:37:01
Spynet

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!