How to show a message when the phone is unlocked

拈花ヽ惹草 提交于 2019-12-10 10:59:44

问题


I want to make an application that would display a message when a user unlocks his/her Android phone. I do not know if this is possible or not.

If anyone has a way to do this, could you please point me in the right direction.


回答1:


Yes you can do this

In your manifest file write this,

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>

and MyBroadCastReceiver implementation is like this

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

    }

}



回答2:


Only android.intent.action.USER_PRESENT action BroadcastReceiver is enough to do what you need




回答3:


Yes you can do it by registering android.intent.action.USER_PRESENT in manifast as:

<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>

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


来源:https://stackoverflow.com/questions/11257691/how-to-show-a-message-when-the-phone-is-unlocked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!