您的位置:首页 > 移动开发 > Android开发

Android-服务中监听电源键和Home键的广播

2015-10-30 15:33 531 查看
添加权限:<uses-permission android:name="android.permission.WAKE_LOCK" />

服务与广播接收者

<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.copytest;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
/**
*
* @author yu_longji
*
*/
public class KeyCodeUnlock extends Service {

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onCreate() {
super.onCreate();
// onCreate()方法中注册
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
registerReceiver(mBatInfoReceiver, filter);

final IntentFilter homeFilter = new IntentFilter(
Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
registerReceiver(homePressReceiver, homeFilter);

}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(homePressReceiver != null) {
try {
unregisterReceiver(homePressReceiver);
}
catch(Exception e) {
}
}

// onDestory()方法中解除注册
if(mBatInfoReceiver != null) {
try {
unregisterReceiver(mBatInfoReceiver);
}
catch(Exception e) {
}
}
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
return super.onStartCommand(intent, flags, startId);
}
//home键
private final BroadcastReceiver homePressReceiver = new BroadcastReceiver() {
final String SYSTEM_DIALOG_REASON_KEY = "reason";
final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
if(reason != null
&& reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
// 自己随意控制程序,关闭...
Log.e("test", "HomeKey");
}
}
}
};
//电源键
// Intent.ACTION_SCREEN_OFF;
// Intent.ACTION_SCREEN_ON;
private final BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
@Override
public void onReceive(final Context context, final Intent intent) {
final String action = intent.getAction();
if(Intent.ACTION_SCREEN_OFF.equals(action)) {
Log.e("test", "PowerKey-off");
}else if (Intent.ACTION_SCREEN_ON.equals(action)) {
Log.e("test", "PowerKey-on");
}
}
};

}
</span>

Activity中启动服务
<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.copytest;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(MainActivity.this, KeyCodeUnlock.class);
startService(intent);

}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Intent intent = new Intent(MainActivity.this, KeyCodeUnlock.class);
stopService(intent);
}

}
</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息