2012-04-01 3 views
2

私は基本的に、既に実行されているアプリケーションに関係なく、画面がロックされていないときは常に画面を表示したいと考えています。アンロックイベントの画面/アクティビティを表示しますか?

電話機のロックが解除されるとすぐに、テキストの表示方法を教えてもらえますか?それから私はそれを取ることができます。

私はネットで見つけ、今までの次のコードまで....

を持っているが、私は、すぐに携帯電話のロックが解除されますよう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; 
     }   

    } 

} 

1については

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 

     } 

    } 
} 

答えて

3

を次のように画面のレシーバのクラスがある場合、あなたは活動、ダイアログ、または他のUIコンポーネントを表示し、abc.xmlは表示されません。 ACTION_BOOT_COMPLETEDのインテントをリッスンするブロードキャストレシーバーを設定できます。デバイスの起動が完了したら、スティッキーサービスを開始して上記の操作を聞くことができます。おそらくあなたはアクティビティにabc.xmlを表示したいので、上記のif()ブロックの1つからstartActivityを発射する必要があります。

関連する問題