MainActivity.java

package com.example.androidtest;import android.os.Bundle;import android.os.PowerManager;import android.app.Activity;import android.content.Intent;import android.view.Menu;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);startService(new Intent(this, MainService.class));}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main, menu);return true;}}
MainService.java

package com.example.androidtest;import android.app.Service;import android.content.Intent;import android.content.IntentFilter;import android.os.IBinder;import android.util.Log;public class MainService extends Service {private String tag="test";@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();Log.i(tag, "onCreate");MainReciver r= new MainReciver();IntentFilter filter= new IntentFilter();filter.addAction(Intent.ACTION_SCREEN_OFF);filter.addAction(Intent.ACTION_SCREEN_ON);registerReceiver(r,filter);}@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubLog.i(tag, "onBind");return null;}@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();}@Overridepublic void onLowMemory() {// TODO Auto-generated method stubsuper.onLowMemory();}}

MainReciver.java

package com.example.androidtest;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.os.PowerManager;import android.util.Log;public class MainReciver extends BroadcastReceiver {String tag = "test";@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubLog.i(tag, intent.getAction());PowerManager pm =(PowerManager)context.getSystemService(Context.POWER_SERVICE);if(pm.isScreenOn()){Log.e(tag, "ScreenOn");}else{Log.e(tag, "ScreenOff");}}}

代码很简单。有问题欢迎留言。

更多相关文章

  1. 如何提高android代码质量
  2. Android日志系统Logcat源代码简要分析
  3. Android 轻松实现语音识别的完整代码
  4. 10.Android之测试代码实现步骤

随机推荐

  1. Android基础(一)
  2. android selector
  3. Android实现2D翻转动画
  4. 快速进入Android世界
  5. Android控件编辑时键盘弹起与关闭处理
  6. android的常用开发包
  7. Android(安卓)UI主线程与子线程
  8. 自定义实现圆形播放进度条(android,飞一般
  9. Android屏幕密度(Density)和分辨率的关系
  10. Android(安卓)ListView更换点击效果