http://florent-dupont.blogspot.ro/2015/02/android-5-screen-pinning.html
从固定的应用程序中,您无法启动辅助应用程序,除非该应用程序具有相同的共享用户 ID(这意味着在 AndroidManifest.xml 中设置的 sharedUserIdis 并且该第二个应用程序使用相同的证书打包)。不允许启动其他应用程序的活动,这样做(通过使用 Context.startActivity())将被忽略。
我已经完成了上述两件事,但startActivity()
仍然被忽略。
来自https://developer.android.com/reference/android/R.attr.html#lockTaskMode:
如果在启动基于此活动的新任务时系统已处于 lockTask 模式,则该任务将启动或不启动,具体取决于此活动的包是否已列入白名单。
看起来我已经采取了必要的步骤来实现这一点。
有谁得到这个工作?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
ComponentName deviceAdmin = new ComponentName(this, AdminReceiver.class);
mDpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
if (!mDpm.isAdminActive(deviceAdmin)) {
Toast.makeText(this, getString(R.string.not_device_admin), Toast.LENGTH_SHORT).show();
}
if (mDpm.isDeviceOwnerApp(getPackageName()))
{
mDpm.setLockTaskPackages(deviceAdmin, new String[]{getPackageName(), "com.that.other.package"});
try
{
enableKioskMode(true);
PackageManager pm = this.getPackageManager();
Intent it = pm.getLaunchIntentForPackage("com.that.other.package");
if (null != it) {
this.startActivity(it);
Log.d(_TAG, "Started activity for com.that.other.package");
}
}
catch (Exception e)
{
Log.e(_TAG, e.getMessage());
finish();
}
} else {
Toast.makeText(this, getString(R.string.not_device_owner), Toast.LENGTH_SHORT).show();
}
}
private void enableKioskMode(boolean enabled) throws Exception
{
if (enabled)
{
if (mDpm.isLockTaskPermitted(this.getPackageName()))
{
startLockTask();
mIsKioskEnabled = true;
mButton.setText(getString(R.string.exit_kiosk_mode));
} else {
Toast.makeText(this, getString(R.string.kiosk_not_permitted), Toast.LENGTH_SHORT).show();
}
} else {
stopLockTask();
mIsKioskEnabled = false;
mButton.setText(getString(R.string.enter_kiosk_mode));
}
}