Android Facebook 锁屏通知 [英] Android Facebook lock screen notification

查看:21
本文介绍了Android Facebook 锁屏通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最新版本的 Android 应用 Facebook 上显示了锁屏通知功能,如下图所示:

On the latest version of Android app Facebook showed lock screen notification feature, like on this screenshot:

有没有人尝试实现这个?

Did anyone try to implement this?

我知道在锁定屏幕上显示 Activity 很简单,但不幸的是它不适用于半透明背景.基本上它可以工作,但在我们的活动下方,我们看到启动器屏幕,而不是锁定屏幕(在这种情况下,锁定屏幕也是透明的).

I know that It's simple to show Activity on top of lock screen, but unfortunately It doesn't work with translucent background. Basically it works but below our activity we see launcher screen, not lock screen (like lock screen in this case would be also transparent).

我现在尝试的是:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

在我的活动中.

我也试过这个例子:https://gist.github.com/daichan4649/5352944

正如我所描述的 - 一切正常,但没有透明度.

And as I described - everything works but no transparency.

根据我的观察,Facebook 使用主题:

From my observation Facebook uses theme:

@android:style/Theme.Translucent.NoTitleBar

并且没有权限:

<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

我还注意到锁屏通知需要触摸,因此我们无法通过手势显示来自状态栏的通知.

Also I noticed that lock screen notification aquires touches so we cannot show notifications from statusbar by gesture.

任何想法如何在 Android L 发布之前创建这种通知.

Any ideas how to create that kind of notification before Android L release.

推荐答案

实际上,ferdy182 曾经/正在做某事.

Actually, ferdy182 was/is onto something.

这是我使用 android.permission.SYSTEM_ALERT_WINDOW 得到的:

Here's what I got using the android.permission.SYSTEM_ALERT_WINDOW:

所以,我不能用 Activity 做到这一点.它只是行不通.我必须实现一个 Service,它使用 WindowManager 添加了一个 View.

So, I couldn't do this with an Activity. It just wouldn't work. I had to implement a Service which added a View using the WindowManager.

一种可能的工作流程是:您的 BroadcastReceiver 收到广播 => 它启动一个服务 => 服务添加所需的视图.

One possible workflow would be: a broadcast is received by your BroadcastReceiver => it starts a Service => the Service adds the required view.

现在,代码(注释解释了一些事情):

Now, the code (the comments explain a few things):

public class MyService extends Service {

    View mView;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        // instance of WindowManager
        WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        LayoutInflater mInflater = (LayoutInflater) 
                                      getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // inflate required layout file
        mView = mInflater.inflate(R.layout.abc, null);

        // attach OnClickListener
        mView.findViewById(R.id.some_id).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // you can fire an Intent accordingly - to deal with the click event
                // stop the service - this also removes `mView` from the window
                // because onDestroy() is called - that's where we remove `mView`
                stopSelf();
            }
        });

        // the LayoutParams for `mView`
        // main attraction here is `TYPE_SYSTEM_ERROR`
        // as you noted above, `TYPE_SYSTEM_ALERT` does not work on the lockscreen
        // `TYPE_SYSTEM_OVERLAY` works very well but is focusable - no click events
        // `TYPE_SYSTEM_ERROR` supports all these requirements
        WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, 
            ViewGroup.LayoutParams.WRAP_CONTENT, 0, 0,
            WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                    | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, 
                      PixelFormat.RGBA_8888);

        // finally, add the view to window
        mWindowManager.addView(mView, mLayoutParams);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        // remove `mView` from the window
        removeViewFromWindow();
    }

    // Removes `mView` from the window
    public void removeNow() {
        if (mView != null) {
            WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
            wm.removeView(mView);
        }
    }
}

最后,将权限添加到您的应用清单:

And finally, add the permission to your app's manifest:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

这篇关于Android Facebook 锁屏通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆