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();
}
In the above code I have made my Activity to Launch when the screen is security locked(pin/swipe) too.
However the Toast message is not displayed while its launched when the phone is security locked.
When I unlock the device and launch again, the toast message is seen.
Initially I tried without the WAKE_LOCK changes, it did not work. Then I tried with WAKE_LOCK changes too, still it did not work.
How to fix this issue. Is this an Android limitation?.
答案 0 :(得分:1)
Toast无法在锁定屏幕上显示。
从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());
您还可以添加触发意图的按钮。