问题描述
我尝试实施kiosk模式的应用程序。我能够锁定大部分的可能性,关闭应用程序或访问系统功能。现在,我想知道,是否有可能在一个锁屏多种活动。如果我在我的应用程序的多个活动之间切换时,会显示的默认锁屏了片刻,然后应用程序再次出现。如果我只是取代片段,应用程序的工作原理就像一个魅力。
I try to implement a kiosk mode application. I was able to lock down most of the possibilities to close the app or access system functions. Now, I was wondering, if it is possible have multiple activities in a lock screen. If I switch between multiple activities of my app, the default lock screen is shown for a short moment and then the app re-appears. If I just replace fragments, the app works like a charm.
我有以下的code:
@Override
public void onAttachedToWindow() {
getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG|WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onAttachedToWindow();
}
和
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
}
有没有人有一些提示如何提高下来锁定无闪烁?
Does anyone has some hints how to improve the lock down without flickering?
推荐答案
好吧,这是我如何解决了Kiosk模式的问题,在键盘锁的面前。
Ok, this is how I solved the problem with kiosk mode in front of the keyguard.
首先,我不得不接受该标志 FLAG_SHOW_WHEN_LOCKED
不能很好地与多种活动工作。因此,我们必须将应用程序减少到一个单一的活动。但这意味着另一个缺点: startActivity
仍然将开始新的活动,并导致应用程序闪烁
First of all, I had to accept that the flag FLAG_SHOW_WHEN_LOCKED
does not work well with multiple activities. So, we have to reduce the app to one single activity. But this implies another drawback: startActivity
will still start new activities and causes the app to flicker.
要避免这种情况,我改写了所有的活动,使他们的碎片。在 MainActivity
现在一直控制在需要时更换片段。它宣布在表现为 SingleTop
:
To avoid that, I rewrote all Activities and made them Fragments. The MainActivity
now keeps control over replacing the fragments when needed. It is declared in the Manifest as SingleTop
:
<activity android:name="com.example.DashboardActivity"
android:screenOrientation="landscape"
android:launchMode="singleTop"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.HOME"/>
</intent-filter>
...
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="video" />
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
...
</activity>
的意图与处理:
@Override
public void onNewIntent(Intent intent){
super.onResume();
dispatchIntent(intent);
}
public void dispatchIntent(Intent intent) {
Log.d(TAG, "Intent:" + intent);
Bundle extras = intent.getExtras();
String action = intent.getAction();
if(extras == null) {
extras = new Bundle();
}
Fragment fragment = null;
if (Intent.ACTION_VIEW.equals(action)) {
extras.putParcelable("uri", intent.getData());
fragment = new VideoplayerFragment();
} else {
fragment = new DashboardFragment();
}
addOrReplaceFragment(fragment, extras);
}
private void addOrReplaceFragment(Fragment fragment, Bundle arguments) {
if (fragment != null && findViewById(CONTENT_CONTAINERVIEW_ID) != null) {
if(arguments != null) {
fragment.setArguments(arguments);
}
FragmentTransaction ft = getFragmentManager().beginTransaction();
String tag = fragment.getClass().getSimpleName();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.addToBackStack(tag);
if(getFragmentManager().findFragmentByTag(fragment.getClass().getSimpleName()) != null) {
ft.replace(CONTENT_CONTAINERVIEW_ID, fragment, tag);
} else {
ft.add(CONTENT_CONTAINERVIEW_ID, fragment, tag);
}
ft.commit();
}
}
此外,你应该为 SCREEN_ON注册
和 SCREEN_OFF
意图开始练习,如果是由于任何停止原因。
Additionally, you should register for SCREEN_ON
and SCREEN_OFF
intents to launch the activity, if it was stopped due any reason.
希望它可以帮助别人。
这篇关于Android的锁屏多项活动(a.k.a基本预设模式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!