Android realizes a black screen automatically during phone calls

Source: Internet
Author: User

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;

/**
* Base class 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 ();
}
}

Android realizes a black screen automatically during phone calls

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.