【问题标题】:Unable to display Toast message when phone is PIN locked手机 PIN 码锁定时无法显示 Toast 消息
【发布时间】:2017-08-17 13:52:35
【问题描述】:
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();
    }

在上面的代码中,我也让我的 Activity 在屏幕被安全锁定(pin/swipe)时启动

但是,Toast 消息在手机安全锁定时启动时不显示

当我解锁设备并再次启动时,会看到 toast 消息

最初我尝试不更改 WAKE_LOCK,但它不起作用。然后我也尝试了 WAKE_LOCK 更改,但它仍然不起作用。

如何解决此问题。 这是 Android 限制吗?

【问题讨论】:

    标签: android android-toast android-security


    【解决方案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());
    

    您还可以包含触发意图的按钮。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-26
      • 1970-01-01
      • 2019-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      相关资源
      最近更新 更多