画面の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 |
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]
Tweet
最終更新時間:2011年10月27日 21時29分45秒