2017-06-19 118 views
0
public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON 
       + WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD| 
       + WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED| 
       + WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); 
     setContentView(R.layout.activity_main); 
     PowerManager.WakeLock wl; 
     PowerManager pm = (PowerManager) getSystemService(
       Context.POWER_SERVICE); 
     wl = pm.newWakeLock(
       PowerManager.PARTIAL_WAKE_LOCK 
         | PowerManager.ACQUIRE_CAUSES_WAKEUP, 
       "ToastActivity"); 
     wl.acquire(); 
     Log.w("TOAST","show"); 
     Toast.makeText(this, "test toast", Toast.LENGTH_LONG).show(); 
    } 

在上面的代碼我已經作出了活動到啓動屏幕關閉時,安全鎖定(銷/刷卡)太無法顯示吐司消息時手機是PIN鎖定

但是,Toast消息是未在其電話被安全鎖定時啓動

當我解鎖設備並再次啓動時,看到吐司消息

最初,我嘗試沒有WAKE_LOCK更改,它沒有工作。然後,我嘗試了WAKE_LOCK更改,但仍然無效。

如何解決此問題。 這是Android的限制嗎?

回答

1

敬酒不能在鎖屏上顯示。

鎖屏小部件也從Android版本5.0開始gone extinct

最好的辦法是使用Notification,像這樣:

  Notification.Builder builder = new Notification.Builder(mContext) 
        .setContentTitle("Alert message here") 
        .setSmallIcon(R.drawable.alert) //required, otherwise throws 'no valid small icon)' 
        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC); //viewable on lock-screen 

      NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
      nm.notify("test", 0, builder.build()); 

您還可以觸發意圖按鈕。