MyBoot用於監聽開機並且啟動MyService,透過MyService去攔截螢幕ON的訊號,攔截到則會有在LogCat info顯示出來
package com.cyfang.filter.screen; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class MyBoot extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { Intent start = new Intent(context, MyService.class); context.startService(start); } } }
package com.cyfang.filter.screen; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class FilterScreen extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { Log.i("Tag", "Screen On"); } } }
package com.cyfang.filter.screen; import android.app.Service; import android.content.Intent; import android.content.IntentFilter; import android.os.IBinder; public class MyService extends Service { @Override public IBinder onBind(Intent intent) { return null; } @Override public void onStart(Intent intent, int startId) { IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); filter.addAction("android.intent.action.SCREEN_ON"); FilterScreen filterScreen = new FilterScreen(); registerReceiver(filterScreen, filter); } }
參考資料:
http://blog.csdn.net/kay_wyong/article/details/6631870
http://jimmy319.blogspot.tw/2011/12/android-boot-up-completeservice.html
http://haiyang08101.iteye.com/blog/1541832
http://cooking-java.blogspot.tw/2010/04/android-service.html
http://express.ruanko.com/ruanko-express_22/webpage/tech-overnight_1.html
http://billhoo.blog.51cto.com/2337751/761230
http://developer.android.com/reference/android/content/Intent.html#ACTION_USER_PRESENT