如何在手机解锁时显示消息

时间:2012-06-29 07:21:27

标签: android android-layout android-widget

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

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

3 个答案:

答案 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();

    }

}