android实现系统电话通话过程中自动感应黑屏
package com.developmenttools.customui.activity;
import java.util.HashSet;
import java.util.Set;
import com.developmenttools.listener.HomeClickListener;
import com.yzx.tools.CustomLog;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
/**
* 基类Activity
*
* @data:2012-12-6 下午4:44:23
*/
public class BaseCallActivity extends BaseActivity {
private HomeKeyEventBroadCastReceiver homeKeyEventReceiver;
private HomeClickListener homeCliekListener;
private Set<Activity> sProximityDependentActivities = new HashSet<Activity>();
private View activityView;
public synchronized void startProximitySensorForActivity(Activity activity) {
if (!sProximityDependentActivities.contains(activity)) {
activityView = ((ViewGroup) activity.getWindow().getDecorView().findViewById(android.R.id.content)).getChildAt(0);
SensorManager sm = (SensorManager) activity.getSystemService(Context.SENSOR_SERVICE);
Sensor s = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
if (s != null) {
sm.registerListener(sProximitySensorListener, s, SensorManager.SENSOR_DELAY_UI);
}
sProximityDependentActivities.add(activity);
}
}
public synchronized void stopProximitySensorForActivity(Activity activity) {
simulateProximitySensorNearby(activity, false);
sProximityDependentActivities.remove(activity);
SensorManager sm = (SensorManager) activity.getSystemService(Context.SENSOR_SERVICE);
sm.unregisterListener(sProximitySensorListener);
}
private SensorEventListener sProximitySensorListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
if (event.timestamp == 0){
return;
}
proximityNearbyChanged(isProximitySensorNearby(event));
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
};
public Boolean isProximitySensorNearby(final SensorEvent event) {
float threshold = 4.001f; // <= 4 cm is near
final float distanceInCm = event.values[0];
final float maxDistance = event.sensor.getMaximumRange();
boolean isNear = false;
if (maxDistance >= 1023.0f) {
if (distanceInCm < threshold) {
isNear = true;
}
} else {
if (maxDistance >= 255.0f) {
if (distanceInCm <= 0.0f) {
isNear = true;
}
} else {
if (maxDistance <= threshold) {
threshold = maxDistance;
}
if (distanceInCm < threshold) {
isNear = true;
}
}
}
return isNear;
}
private void proximityNearbyChanged(boolean mLastProximitySensorValueNearby) {
for (Activity activity : sProximityDependentActivities) {
simulateProximitySensorNearby(activity, mLastProximitySensorValueNearby);
}
}
private void simulateProximitySensorNearby(Activity activity, boolean nearby) {
Window window = activity.getWindow();
WindowManager.LayoutParams params = activity.getWindow().getAttributes();
if (nearby) {
params.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
window.setAttributes(params);
View view = new View(activity);
view.setBackgroundColor(Color.parseColor("#000000"));
window.setContentView(view);
} else {
params.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.setAttributes(params);
if(activityView != null){
window.setContentView(activityView);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
IntentFilter ift = new IntentFilter();
ift.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
homeKeyEventReceiver = new HomeKeyEventBroadCastReceiver();
registerReceiver(homeKeyEventReceiver, ift);
super.onCreate(savedInstanceState);
}
@Override
protected void onStart() {
super.onStart();
startProximitySensorForActivity(this);
}
public void addHomeClickListener(HomeClickListener homeListener){
homeCliekListener = homeListener;
}
public class HomeKeyEventBroadCastReceiver extends BroadcastReceiver {
static final String SYSTEM_REASON = "reason";
static final String SYSTEM_HOME_KEY = "homekey";// home key
static final String SYSTEM_RECENT_APPS = "recentapps";// long home key
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
String reason = intent.getStringExtra(SYSTEM_REASON);
if (reason != null) {
if (reason.equals(SYSTEM_HOME_KEY) || reason.equals(SYSTEM_RECENT_APPS)) {
if(homeCliekListener != null){
homeCliekListener.onClick();
}
}
}
}
}
};
@Override
protected void onPause() {
super.onPause();
stopProximitySensorForActivity(this);
}
@Override
protected void onRestart() {
super.onRestart();
startProximitySensorForActivity(this);
}
@Override
protected void onDestroy() {
stopProximitySensorForActivity(this);
unregisterReceiver(homeKeyEventReceiver);
super.onDestroy();
}
}