【问题标题】:How to show a message when the phone is unlocked手机解锁时如何显示消息
【发布时间】:2012-06-29 07:21:27
【问题描述】:

我想制作一个应用程序,当用户解锁他/她的 Android 手机时会显示一条消息。我不知道这是否可能。

如果有人有办法做到这一点,请您指出正确的方向。

【问题讨论】:

    标签: android android-layout android-widget


    【解决方案1】:

    只有 android.intent.action.USER_PRESENT 操作 BroadcastReceiver 就足以满足您的需要

    【讨论】:

      【解决方案2】:

      是的,您可以通过在 manifast 中注册 android.intent.action.USER_PRESENT 来做到这一点:

      <receiver android:name=".unlockReceiver">
      <intent-filter android:enabled="true" android:priority="90000" android:exported="false">
          <action android:name="android.intent.action.USER_PRESENT" />
          </intent-filter>
       </receiver>
      

      并且在 unlockReceiver 中显示消息为:

      public class unlockReceiver extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
      if (intent.getAction() != null) {
          if( intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
             Toast msg = Toast.makeText(context,"hello User !!! :)", Toast.LENGTH_LONG);
              msg.show();
            }
          }
      }
      

      【讨论】:

        【解决方案3】:

        是的,你可以这样做

        在你的清单文件中写下这个,

        receiver android:name=".MyBroadCastReceiver" >
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
        
                    <category android:name="android.intent.category.HOME" />
        
                    <action android:name="android.intent.action.SCREEN_ON" />
                    <action android:name="android.intent.action.USER_PRESENT" />
                </intent-filter>
            </receiver>
        

        而MyBroadCastReceiver的实现是这样的

        public class MyBroadCastReceiver extends BroadcastReceiver {
        
            Context mContext;
        
            @Override
            public void onReceive(Context context, Intent intent) {
                mContext = context;
                Toast.makeText(mContext, "Phone UNLOCKED", Toast.LENGTH_LONG)
                        .show();
        
            }
        
        }
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-30
        • 2019-10-12
        • 1970-01-01
        相关资源
        最近更新 更多