トップ 差分 一覧 ソース 置換 検索 ヘルプ PDF RSS ログイン

Android 画面のON、OFFを受け取る

 画面のON、OFFの変化を受け取る

http://www.bpsinc.jp/blog/archives/1615

<action android:name="android.intent.action.ACTION_SCREEN_OFF"/>
<action android:name="android.intent.action.ACTION_SCREEN_ON"/>

を入れても受け取れない。プログラムで仕掛ける必要があるみたい。

getApplicationContext

を使わなくても動くようだけど、素直にgetApplicationContext使った方が良いかも。

ScreenOff.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package test.screenoff;

import android.app.Activity;
import android.os.Bundle;
import android.content.*;

public class ScreenOff extends Activity
{
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    this.getApplicationContext().registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
          System.out.println("onReceive 2");
        }
    }, filter);
    
    filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
    this.getApplicationContext().registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
          System.out.println("onReceive 3");
        }
    }, filter);
  }
}

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="test.screenoff"
   android:versionCode="1"
   android:versionName="1.0">
   <application android:label="@string/app_name">
     <activity android:name="ScreenOff"
       android:label="@string/app_name">
       <intent-filter>
         <action android:name="android.intent.action.MAIN" />
         <category android:name="android.intent.category.LAUNCHER" />
       </intent-filter>
     </activity>
     
   </application>
 </manifest> 


[カテゴリ: プログラミング言語 > Java > Android]

[通知用URL]



  • Hatenaブックマークに追加
  • livedoorクリップに追加
  • del.icio.usに追加
  • FC2ブックマークに追加

最終更新時間:2011年10月27日 21時29分45秒