2013-09-29 169 views

回答

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

最後我達到了同樣的效果。不要參加活動,因爲出於安全原因,android不會出現鎖定屏幕,所以請使用服務而不是活動。

下面是我的代碼在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" />體現

+1

謝謝,這對我有所不同,如果你想在鎖屏上顯示它並在背景上顯示鎖屏,請不要使用活動 –

+0

@AdibFara那麼,我們使用什麼? – kadik

+0

@kadik你可以使用服務,請檢查上面的答案 –

11

我解決了以下方式類似的東西。 創建服務,播放動作「ACTION_SCREEN_ON & ACTION_USER_PRESENT & ACTION_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(); 
      } 
     } 
    }; 
} 

我希望它有用!

+1

我正在尋找確切的解決方案,感謝gfirem和Unihedro。 –

+0

你可以分享fragment_overlay嗎? –

+0

謝謝!它的工作原理,但不與FLAG_DISMISS_KEYGUARD https://stackoverflow.com/a/32233473/4229910 – ashwinx