【问题标题】:Android lock screen multiple activities (a.k.a basic kiosk mode)Android 锁屏多项活动(又称基本信息亭模式)
【发布时间】:2014-04-12 21:17:10
【问题描述】:

我尝试实现一个自助服务终端模式应用程序。我能够锁定关闭应用程序或访问系统功能的大部分可能性。现在,我想知道,是否有可能在锁定屏幕中有多个活动。如果我在我的应用程序的多个活动之间切换,默认锁定屏幕会显示片刻,然后应用程序重新出现。如果我只是替换 Fragments,应用程序就像一个魅力。

我有以下代码:

@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);
}

有没有人有一些提示如何在不闪烁的情况下改善锁定?

【问题讨论】:

  • 你已经回答了你自己的问题 - 使用片段:)
  • SingleTop 与片段和意图过滤器相关的活动完成了这项工作。谢谢。

标签: android lockscreen kiosk-mode


【解决方案1】:

好的,我就是这样解决了键盘锁前的信息亭模式问题。

首先,我不得不接受标志FLAG_SHOW_WHEN_LOCKED 不适用于多项活动。因此,我们必须将应用程序简化为一个活动。但这意味着另一个缺点:startActivity 仍会启动新活动并导致应用闪烁。

为了避免这种情况,我重写了所有活动并将它们制作为片段。 MainActivity 现在可以在需要时控制替换片段。它在 Manifest 中声明为 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_ONSCREEN_OFF 意图以启动活动。

希望对某人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 2010-10-18
    • 2015-11-09
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    • 2011-01-24
    相关资源
    最近更新 更多