19

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

screenshot

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

What I tried right now is:

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

in my Activity.

Also I tried this example: https://gist.github.com/daichan4649/5352944

And as I described - everything works but no transparency.

From my observation Facebook uses theme:

@android:style/Theme.Translucent.NoTitleBar

and doesn't have permission:

<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.

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

gre_gor
  • 6,669
  • 9
  • 47
  • 52
froger_mcs
  • 13,972
  • 5
  • 23
  • 34
  • How about creating an activity with a background image(which is translucent)? – Seshu Vinay Aug 01 '14 at 11:06
  • The same - if I use translucent style I see launcher screen below my Actvity, not the lock screen. If I don't use translucent I have black background (from window) below image background. – froger_mcs Aug 01 '14 at 13:10
  • I feel its not possible on vanilla android..Its specifically running on HTC One M8 running Android 4.4.2 with Sense 6. refer-http://techcrunch.com/2014/07/08/facebook-tests-android-l-style-lock-screen-notifications/ and http://lifehacker.com/floatify-brings-android-ls-new-notifications-to-any-and-1601708892 – rupesh jain Aug 01 '14 at 14:55
  • One way to do this is to implement custom lockscreen to show notifications..That is what many apps in the market are doing. – rupesh jain Aug 01 '14 at 14:56
  • Facebook lockscreen notifications are device and lockscreen independent I think. I'm pretty sure that screen which I attached shows Nexus 5 with vanilla android. I have HTC M8 and see the same notifications. – froger_mcs Aug 01 '14 at 15:03
  • That Nexus 5 could be running custom OS or custom lockscreen. – rupesh jain Aug 01 '14 at 16:19
  • How about creating a lockscreen widget to display notifiactions? – Gumbo Aug 01 '14 at 20:18
  • @Gumbo that would be a solution to displaying notifications, but not a solution to the question asked by froger_mcs. – Richard Le Mesurier Aug 06 '14 at 11:31

2 Answers2

19

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" />
Vikram
  • 51,313
  • 11
  • 93
  • 122
0

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

Fernando Gallego
  • 4,064
  • 31
  • 50
  • 1
    It shows on to of all other applications but not on top of lockscreen. That's why you can't see chatheads on your lockscreen. – froger_mcs Aug 01 '14 at 13:11