My original question is here: Android - How to programmatically switch users by user name? and it still unanswered, but I got some progress and now I'm completely stuck.
Now I've followed these steps:
Added a
DeviceAdmin
class as shown in the code samples, I don't know if it is good or am I supposed to implement something there:import android.app.admin.DeviceAdminReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class DeviceAdmin extends DeviceAdminReceiver { void showToast(Context context, String msg) { Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); } @Override public void onEnabled(Context context, Intent intent) { showToast(context, "admin_receiver_status_enabled"); } @Override public CharSequence onDisableRequested(Context context, Intent intent) { return ""; //context.getString(R.string.admin_receiver_status_disable_warning); } @Override public void onDisabled(Context context, Intent intent) { showToast(context, "admin_receiver_status_disabled"); } @Override public void onPasswordChanged(Context context, Intent intent) { showToast(context, "admin_receiver_status_pw_changed"); } }
Added the
DeviceAdmin
in the manifest:<receiver android:name=".DeviceAdmin" android:label="@string/device_admin" android:description="@string/device_admin_description" android:permission="android.permission.BIND_DEVICE_ADMIN"> <meta-data android:name="android.app.device_admin" android:resource="@xml/security_policies" /> <intent-filter> <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /> </intent-filter> </receiver>
In my
MainActivity
class I've added this code to get admin permissions:DevicePolicyManager mDPM; ComponentName mDeviceAdmin; final int REQUEST_CODE_ENABLE_ADMIN = 1; mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE); mDeviceAdmin = new ComponentName(this, DeviceAdmin.class); if (!mDPM.isAdminActive(mDeviceAdmin)) { // try to become active – must happen here in this activity, to get result Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdmin); intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, mContext.getString(R.string.device_admin_description)); startActivityForResult(intent, REQUEST_CODE_ENABLE_ADMIN); } else { UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE); // always return 1 List<UserHandle> h = um.getUserProfiles(); // crashes !!! saying I need MANAGE_USERS persmission mDPM.switchUser(mDeviceAdmin, h.get(0)); }
I can see my app in the settings menu as device administrator.
So I went to the manifest and added these 2 permissions:
<permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" android:permissionGroup="android.permission-group.SYSTEM_TOOLS" android:protectionLevel="signature" android:label="@string/manage_users_permission" android:description="@string/manage_users_permission_description" /> <permission android:name="android.permission.MANAGE_USERS" android:permissionGroup="android.permission-group.SYSTEM_TOOLS" android:protectionLevel="signature" android:label="@string/manage_users_permission" android:description="@string/manage_users_permission_description" />
This also didn't work, so I found out that I need to sign my app and I have no idea how to even start doing that. I found posts saying I need these 2 files: platform.x509.pem, platform.pk8 so I went and got them from our source code team who worked on the ROM we have installed. I asked a Linux guy to run
keytool-importkeypair
as shown here: Apk with system privileges, this got me a file: debug.keystore which I placed in the app folder of my application.Then I've added this to the
build.gradle
file in the project:signingConfigs { release { storeFile file("debug.keystore") storePassword "test123" keyAlias "platform" keyPassword "test123" } }
None of these solved my problem and I still can't get
MANAGE_USERS
permission and I still can't switch users and then I saw a post saying add this:android:sharedUserId="android.uid.system"
to the<manifest>
tag and this got me this errorINSTALL_FAILD_SHARED_USER_INCOMPATIBLE
I'm very new to android, and this is very advanced for me to understand but I have to do this at work and there are no other Android developers here...
java -jar signapk.jar platform.x509.pem platform.pk8 app-platform-unsigned.apk Platform.apk adb install Platform.apk TIMEOUT /T 5
or for update
java -jar signapk.jar platform.x509.pem platform.pk8 app-platform-unsigned.apk Platform.apk adb install -r Platform.apk TIMEOUT /T 5 – Anuj J Pandey Mar 03 '17 at 10:03