簡體   English   中英

Android Facebook鎖屏通知

[英]Android Facebook lock screen notification

在Android應用程序的最新版本Facebook上顯示鎖定屏幕通知功能,就像在這個截圖上:

截圖

有沒有人試圖實現這個?

我知道在鎖定屏幕上顯示Activity很簡單,但遺憾的是它不適用於半透明背景。 基本上它工作,但在我們的活動下面,我們看到啟動器屏幕,而不是鎖定屏幕(在這種情況下像鎖屏也將是透明的)。

我現在嘗試的是:

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

正如我所描述的 - 一切正常但沒有透明度。

根據我的觀察,Facebook使用主題:

@android:style/Theme.Translucent.NoTitleBar

並且沒有許可:

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

此外,我注意到鎖定屏幕通知會獲取觸摸,因此我們無法通過手勢顯示狀態欄中的通知。

任何想法如何在Android L發布之前創建這種通知。

實際上, ferdy182正在某事上。

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

在此輸入圖像描述

所以,我無法使用Activity執行此操作。 它只是行不通。 我必須實現一個使用WindowManager添加ViewService

一個可能的工作流程是: BroadcastReceiver接收BroadcastReceiver =>它啟動服務=>服務添加所需的視圖。

現在,代碼(評論解釋了一些事情):

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

最后,將權限添加到應用的清單中:

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

我認為它可能與Messenger的聊天頭泡沫使用相同的技巧。

基本上,您使用此權限“android.permission.SYSTEM_ALERT_WINDOW”來顯示您的視圖高於其他應用程序。

我沒有試過自己,但我很確定他們用過這個。

從文檔“允許應用程序使用類型TYPE_SYSTEM_ALERT打開窗口,顯示在所有其他應用程序之上。” http://developer.android.com/reference/android/Manifest.permission.html#SYSTEM_ALERT_WINDOW

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM