簡體   English   中英

Android:如何通過鎖定屏幕顯示對話框或活動[未解鎖屏幕]

[英]Android : How to show dialog or activity over lock screen[ not unlock the screen]

我有一個應用程序,它將從服務器接收消息並向用戶發出一個對話框。所以當手機在鎖定屏幕上時我希望對話框顯示在鎖定屏幕的頂部但不解鎖它。是否有人給我建議?

我用以下方式解決了類似問題。 創建服務,廣播動作“ ACTION_SCREEN_ONACTION_USER_PRESENTACTION_SCREEN_OFF ”,創建用WINDOW_SERVICE顯示窗口的功能。 我根據我的要求使用服務,但它可以適應。

public class OverlayService extends Service {

    private static final String TAG = OverlayService.class.getSimpleName();
    WindowManager mWindowManager;
    View mView;
    Animation mAnimation;

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        registerOverlayReceiver();
        return super.onStartCommand(intent, flags, startId);
    }

    private void showDialog(String aTitle){
        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        mView = View.inflate(getApplicationContext(), R.layout.fragment_overlay, null);
        mView.setTag(TAG);

        int top = getApplicationContext().getResources().getDisplayMetrics().heightPixels / 2;

        RelativeLayout dialog = (RelativeLayout) mView.findViewById(R.id.dialog);
        LayoutParams lp = (LayoutParams) dialog.getLayoutParams();
        lp.topMargin = top;
        lp.bottomMargin = top;
        mView.setLayoutParams(lp);

        ImageButton imageButton = (ImageButton) mView.findViewById(R.id.close);
        lp = (LayoutParams) imageButton.getLayoutParams();
        lp.topMargin = top - 58;
        imageButton.setLayoutParams(lp);
        imageButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mView.setVisibility(View.INVISIBLE);
            }
        });

        TextView title = (TextView) mView.findViewById(R.id.Title);
        title.setText(aTitle);

        final WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT, 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);

        mView.setVisibility(View.VISIBLE);
        mAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.in);
        mView.startAnimation(mAnimation);
        mWindowManager.addView(mView, mLayoutParams);

    }

    private void hideDialog(){
        if(mView != null && mWindowManager != null){
            mWindowManager.removeView(mView);
            mView = null;
        }
    }

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

    private void registerOverlayReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_USER_PRESENT);
        registerReceiver(overlayReceiver, filter);
    }

    private void unregisterOverlayReceiver() {
        hideDialog();
        unregisterReceiver(overlayReceiver);
    }


    private BroadcastReceiver overlayReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.d(TAG, "[onReceive]" + action);
            if (action.equals(Intent.ACTION_SCREEN_ON)) {
                showDialog("Esto es una prueba y se levanto desde");
            }
            else if (action.equals(Intent.ACTION_USER_PRESENT)) {
                hideDialog();
            }
            else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
                hideDialog();
            }
        }
    };
}

我希望它有用!

public void onAttachedToWindow() {
      Window window = getWindow();
      window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
           | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
           | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
           | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
}

最后我實現了同樣的目標。 不要去活動,因為安全原因,android不會在你的活動后面顯示鎖屏,所以使用服務代替Activity。

下面是我服務的onStartCommand中的代碼

WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

View mView = mInflater.inflate(R.layout.score, null);

WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0, 0,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
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);

mWindowManager.addView(mView, mLayoutParams);

並將<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />到清單

暫無
暫無

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

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