gpt4 book ai didi

android - 在解锁事件中显示屏幕/Activity ?

转载 作者:搜寻专家 更新时间:2023-11-01 09:08:40 26 4
gpt4 key购买 nike

我基本上想在屏幕解锁时显示一个屏幕,而不管应用程序是否已经在运行。

谁能告诉我如何在手机解锁后立即显示文本。从那时起我可以接受它。

到目前为止,我在网上找到了以下代码....

假设我想在手机解锁后立即显示 abc.xml。我如何将它添加到 ScreenReceiver 类中?

此外,我不想在应用程序运行时设置任何屏幕。我需要将下面的代码作为服务运行吗?

public class SampleActivity extends Activity {

//Declare the necessary variables
private BroadcastReceiver mReceiver;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);

filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);

mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);

}


@Override
public void onDestroy()
{
super.onDestroy();
Log.v("$$$$$$", "In Method: onDestroy()");

if (mReceiver != null)
{
unregisterReceiver(mReceiver);
mReceiver = null;
}

}

}

其中Screen Reciever类如下

public class ScreenReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
{
Log.v("$$$$$$", "In Method: ACTION_SCREEN_OFF");
// onPause() will be called.
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
{
Log.v("$$$$$$", "In Method: ACTION_SCREEN_ON");
//onResume() will be called.

// Better check for whether the screen was already locked
//if locked, do not take any resuming action in onResume()

//Suggest you, not to take any resuming action here.
}
else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT))
{
Log.v("$$$$$$", "In Method: ACTION_USER_PRESENT");
// Handle resuming events

}

}
}

最佳答案

首先,您不显示 abc.xml,而是显示 Activity 、对话框或其他 UI 组件。您可以设置一个广播接收器来监听 ACTION_BOOT_COMPLETED Intent 。设备启动完成后,您可以启动一个粘性服务来监听上述操作。假设您希望在 Activity 中显示 abc.xml,因此您需要从 if() 之一触发 startActivity上面的 block 。

关于android - 在解锁事件中显示屏幕/Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9968959/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com