本文介绍了如何从锁屏开始新的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建简单的小工具联系人管理,它允许用户拨打和发送短信到所需的联系人。

I am creating simple widget for contact management, which allows user to dial and send sms to desired contact.

它工作正常,正常的部件,但是当我将其添加为在Android 4.2插件锁屏,手机短信的应用程序或拨号程序无法启动。那么其实他们出演,但后面锁屏,让用户仍必须手动解锁屏幕可以拨打/发送短信。

It works fine as "normal widget", but when I add it as lockscreen widget on Android 4.2, sms app or dial app does not start.Well in fact they star, but "behind" lockscreen, so user still must manually unlock screen to be able to dial/send sms.

我搜索网络的一些解决方案,但没有派上用场。我也知道FLAG_DISABLE_KEYGUARD或FLAG_SHOW_WHEN_LOCKED的,但由于短信/拨号应用程序是不是我的,所以我不知道他们是否建立了正确的标志。作为一种变通方法我试图创建我的活动而设置的标志,然后简单地启动所需的一个(拨号或短信),但是这并没有帮助。

I searched web for some solution, but nothing come in handy.I' am aware of FLAG_DISABLE_KEYGUARD or FLAG_SHOW_WHEN_LOCKED, but since sms/dial apps are not "mine" so i dont know if they set up proper flag.As a workaround i tried to create my activity which set those flag and then simply starts desired one (dial or sms), but this does not help.

有一种方法来解锁屏幕,但是这涉及到使用KeyguardManager和KeyguardLock(其中正常工作),但在使用KeyguardLock.newKeyguardLock()我结束了电话未能自动打开锁,肯定是因为结果我不释放这个锁(它导致锁再次出现,这不是我想要的)。

There is a way to unlock screen, but this involves using KeyguardManager and KeyguardLock (which work fine), but in a result of using KeyguardLock.newKeyguardLock() I end up with phone not being able to turn lock automatically, surely because I do not release this lock (it causes lock to appear again, which is not what i want).

其实,这个构件应该simmilarly默认短信控件或插件邮寄锁屏?

In fact, this widget should work simmilarly to default sms widget or mail widget on lock screen?

所以,我的问题是,如何实现这一点,从锁屏开始新的活动?

So, my question is, how to achieve that and start new activity from lockscreen?

推荐答案

嗯,我发现自己的解决方案。事实证明,我很接近:)

Well, i found solution myself. it turned out i was close :)

要启动第三方应用程序/活动中,最简单的解决方案是创建一些代理活动,这将设置窗口中正确的标志,然后启动所需的活动和完成。

To launch 3rd party app/activity, simplest solution is to create some kind of proxy activity, which will set proper flags on window and then launches desired activity and FINISHES.

样品code如下所示:

sample code is shown below:

在插件调用的意图(呼叫代理):

calling intent in widget (calling proxy):

    @Override
public void onReceive(Context context, Intent intent) {
    Utilities.printLog(TAG, "onReceive");
    Utilities.printLog(TAG, "intent: " + intent);
    if (intent.getAction().equals(ACTION)) {

        final String number = intent.getStringExtra(EXTRAS);
        Toast.makeText(context, "Selected number: " + number,
                Toast.LENGTH_SHORT)
                .show();


        /** REMOVING KEYGUARD RECEIVER **/
        // not really an option - lock is still holded by widget and screen
        // cannot be locked again ;(
        // KeyguardManager keyguardManager = (KeyguardManager) context
        // .getSystemService(Context.KEYGUARD_SERVICE);
        // KeyguardLock lock = keyguardManager
        // .newKeyguardLock(Context.KEYGUARD_SERVICE);
        // lock.disableKeyguard();

        final Intent activity = new Intent(context, MainActivity.class);
        activity.putExtras(intent.getExtras());
        activity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        activity.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        activity.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        context.startActivity(activity);
    }

    super.onReceive(context, intent);
}

在代理活动只要致电:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    // getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

    final Intent callingIntent = getIntent();

    final String actionToLaunch = callingIntent.getStringExtra(ContactsStackWidgetProvider.ACTION);
    final String number = callingIntent.getStringExtra(ContactsStackWidgetProvider.EXTRAS);

    final Intent activity = new Intent();
    if (actionToLaunch.equals(Intent.ACTION_DIAL)) {
        activity.setAction(Intent.ACTION_DIAL);
        activity.setData(Uri.parse("tel:"+number));
    } else if (actionToLaunch.equals(Intent.ACTION_SENDTO)) {
        activity.setAction(Intent.ACTION_SENDTO);
        activity.setData(Uri.parse("sms:"+number));
    } else {
        throw new IllegalArgumentException("Unrecognized action: "
                + actionToLaunch);
    }

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            startActivity(activity);
            finish();//it is important to finish, but after a small delay
        }
    }, 50L);


}

这篇关于如何从锁屏开始新的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:12