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();
}
}

时间: 2024-12-07 04:50:44

android实现系统电话通话过程中自动感应黑屏的相关文章

Android 通话:后台通话过程中锁屏解锁,发现听筒内存在锁屏提示音

[系统版本]:T0316 [ HW ]:p1 [测试前提]:1.卡1插入移动4G卡 [测试步骤]:1.呼入或呼出一通来电,接听: 2.按home返回后台,锁屏解锁查看. [测试结果]:1.后台通话过程中锁屏解锁,发现听筒内存在锁屏提示音 [预期结果]:1.通话过程中不应存在锁屏提示音 [概 率]:必现 solution: 修改文件:./frameworks/av/services/audiopolicy/AudioPolicyManager.cpp status_t AudioPolicyMan

易信推专线电话 通话双方中只需一方安装即可免费通话

上周微信电话本着实火了一把,通话双方都下载此APP并开通免费通话功能就能使用微信电话本拨打免费网络电话.这个相对有些局限性,易信就抓住这一点于昨日高调发布了3.0预览版,新版本首次新增了专线电话功能,通话双方中只需一方安装易信,即可拨打对方座机或者手机进行免费通话. 在该版本中,易信专线电话作为主打功能,首次以独立菜单的形式出现在易信主界面中.使用易信时,可通过“电话”菜单进行电话直拨,与手机通讯录和易信好友进行高清通话. 据易信CEO胡琛介绍,与市场上目前常见的免费通话APP有所不同,易信的专

Android Java 与 C++ 调用过程中的常量,路径名、文件名、后缀名最大长度

/**************************************************************************** **************************************************************************** *** ***   This header was automatically generated from a Linux kernel header ***   of the same

MPMoviePlayerController播放过程中自动暂停的问题

在使用系统播放器MPMoviePlayerController的过程中,会出现播放器自动暂停的情况,有两种解决办法: (1)在播放开始的时候,设置useApplicationAudioSession = NO; (2)  重启手机,也可以恢复 参考网址: http://stackoverflow.com/questions/3000969/ipad-mpmovieplayercontroller-video-loads-but-automatically-pauses-when-played h

Android关于创建涂鸦板过程中出现的小问题

我前一段时间在制作涂鸦板的过程中在处理橡皮擦功能上碰上了一些小问题,网上部分资源提到的实现方法和我下面说到的橡皮擦基本方法实现思路大仿类似,以下是基本思路: 橡皮擦就是用和画布颜色一致颜色的画笔在屏幕触摸,实现橡皮擦的功能. 1)初始化画笔,并且设置画笔的颜色为白色(这里其实要设置为画布的颜色). 2)设置画笔的大小为合适的大小. 3)用一个变量记住橡皮擦的颜色,用于在其他操作后重新使用橡皮擦. 以上为简易的橡皮擦主要是使用和画布相同的的颜色来覆盖,但当背景图为一张照片(背景图)时是不可行的,因

Android自动问题——黑屏、死机等姐姐方法

今天用了下Android Studio,出现了一些问题,现在将启动过程中遇到的问题和解决方案列出来,方便大家参考. 安装过程不多说,网上一搜一大把. 那直接说问题吧: 1. 无法启动,报错:Failed to allocate memory: 8This application has requested the Runtime to terminate it in an unusual way.Please contact the application's support team for

quick-cocos2d-x3.3中player3打开黑屏问题(已解决)

    在Cocos2dx引擎中文官网下载了quick-cocos2d-x v3.3 Final for win,然后便打开安装 并运行了setup_win.bat自动设置了环境变量,此时桌面已经生成了player3.exe项目模拟器, 可是打开后却是出现了黑屏,起初以为是环境变量没有设置好,于是便重新查看设置了环境 变量并重新启动了一下机器,可是结果还是没有改变,依旧黑屏(带帧率等信息),如图: 之后我就问了度娘,可是依旧没有找到解决方案,于是我就决定自己去尝试重新编译生成一下: (此处pla

ORACLE 11g安装中vnc viewer黑屏现象

1,vnc viewer黑屏问题: 2,可能是centos下面的桌面组件没有安装好 yum groupinstall "X WindowSystem" "KDE Desktop" Desktop -y yumgroupinstall -y   "Desktop"   "DesktopPlatform"   "Desktop Platform Development" "Fonts" &q

一句话解决windows下quick-cocos2d-x中player3打开黑屏问题

运行了setup_win.bat自动设置了环境变量,此时桌面已经生成了player3.exe项目模拟器,打开却是黑屏,只显示FPS那些调试参数. 在网上翻了很多解决方案,都没有完美解决. 第一种方法: 复制cocos.framework两个文件夹到welcome的src目录.(网上很多地方都说复制framework,实际还有cocos目录也要复制才可以) 第二种方法: 打开welcome下src的main.lua,在最前面添加一句代码: package.path = package.path .