显示解锁事件的屏幕/活动?
问题描述:
我基本上想要显示一个屏幕,无论应用程序已经运行,屏幕解锁。显示解锁事件的屏幕/活动?
有人可以告诉我如何在手机解锁后立即显示文本。我可以从此继续。
我有下面的代码到现在,我在网上....
假设我想尽快把手机被解锁显示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;
}
}
}
其中屏幕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
,则显示一个活动,对话框或其他UI组件。您可以设置一个广播接收机,用于监听意图。一旦设备启动完成,您可以启动一项粘性服务来监听上面的操作。想必您在一项活动中需要显示abc.xml
,因此您需要从if()
块之一中触发startActivity
。