Android Facebook lock screen notification

后端 未结 2 1529
盖世英雄少女心
盖世英雄少女心 2020-12-12 18:39

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

Did anyone try to implement this?

I

相关标签:
2条回答
  • 2020-12-12 18:58

    I think it is probably using the same trick as the Chat heads bubbles for Messenger.

    Basically you use this permission "android.permission.SYSTEM_ALERT_WINDOW" to display your views above other apps.

    I haven't tried myself but I am pretty sure they used this.

    From the docs "Allows an application to open windows using the type TYPE_SYSTEM_ALERT, shown on top of all other applications. " http://developer.android.com/reference/android/Manifest.permission.html#SYSTEM_ALERT_WINDOW

    0 讨论(0)
  • 2020-12-12 19:16

    Actually, ferdy182 was/is onto something.

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

    enter image description here

    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.

    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" />
    
    0 讨论(0)
提交回复
热议问题