手机解锁时如何显示消息



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

如果有人有办法做到这一点,你能不能指出我正确的方向。

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

是的,您可以通过在 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>

并在解锁接收器中显示消息为:

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();
      }
    }
}

是的,你可以这样做

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

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();
    }
}

最新更新