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

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

问题描述

在的Andr​​oid应用程序的最新版本的Facebook显示锁屏通知功能(就像这个截图 - http://imgur.com/5eA8iT9)。

On the latest version of Android app Facebook showed lock screen notification feature (like on this screenshot - http://imgur.com/5eA8iT9).

有没有人尝试实现这一点?

Did anyone try to implement this?

我知道,这是简单的显示在锁屏界面上方的活动,但遗憾的是它不具有半透明的后台工作。基本上,它的工作原理,但低于我们的活动,我们看到启动画面,而不是锁屏(如在这种情况下,锁屏会也是透明的)。

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

在我的活动。

我也试过了这个例子:<一href="https://gist.github.com/daichan4649/5352944">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" />

此外,我注意到,锁屏通知aquires触摸,所以我们不能用手势表示从状态栏的通知。

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

任何想法如何大号的Andr​​oid发布之前创建形式的通知。​​

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

所以,我无法用活动做到这一点。这是做不到的。我不得不实施服务里面加了查看使用窗口管理器

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.

现在,在code(注释说明几件事情):

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天全站免登陆