我想创建一个应用程序,当用户解锁他/她的Android手机时会显示一条消息。我不知道这是否可能。
如果有人有办法做到这一点,请你指点我正确的方向。
答案 0 :(得分:4)
只有 android.intent.action.USER_PRESENT
操作 BroadcastReceiver
足以满足您的需求
答案 1 :(得分:3)
是的,您可以通过在清单中注册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>
和id解锁Receiver show message as:
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();
}
}
}
答案 2 :(得分:1)
是的,你可以这样做
在你的清单文件中写下这个,
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();
}
}