解锁事件时显示屏幕/活动?

时间:2012-04-01 22:36:33

标签: android

无论应用程序是否已经运行,我都希望在屏幕解锁时显示一个屏幕。

有人可以告诉我如何在手机解锁后立即显示文字。我可以从那时开始接受它。

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

假设我想在手机解锁后立即显示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;
        }          

    }

}

屏幕接收器类如下

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

      }

   }
}

1 个答案:

答案 0 :(得分:3)

例如,您不显示abc.xml,而是显示活动,对话框或其他UI组件。您可以设置一个侦听ACTION_BOOT_COMPLETED意图的广播接收器。设备启动完成后,您可以启动粘性服务来监听上面的操作。大概你想要在活动中显示abc.xml,因此你需要从上面的startActivity块之一中激活if()