Mogician Ha
Mogician Ha

Reputation: 85

I cannot set device owner for my app on device

I have been working on setting my apps to device owner through adb shell dpm command but the error came out

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

autolock.AdminReceiver}

Here is my activity and manifests

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");

    }

}
}

the manifest:

<?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>

I am not sure why the error is still out there, is that because one device could only have single app that be set device owner? Another question is that the adb command could be implemented on a device without account and connect to pc in order to input command in terminal. Can I do it by code in app under the circumstances the device is not rooted because I have several devices and it is hard to set them one by one, Thanks

Upvotes: 0

Views: 8036

Answers (2)

user3114161
user3114161

Reputation:

You must first set active admin after that set device owner and be careful your package name must be correct.

Upvotes: 0

Cranescanner
Cranescanner

Reputation: 101

I solved the bad admin error when I followed this tutorial: http://www.sureshjoshi.com/mobile/android-kiosk-mode-without-root/

I was missing the src/main/res/xml/device_admin.xml file, which only contained:

<device-admin> </device-admin>

Upvotes: 1

Related Questions