简体   繁体   English

接近传感器-静音呼叫应用

[英]Proximity Sensor- Silence call application

I am trying to build an android application that silences a call when a person waves his/her hand over the phone as it starts ringing/vibrating using the Proximity sensor. 我正在尝试构建一个Android应用程序,当有人使用接近传感器开始响起/振动电话时,当有人在电话上挥手时,该电话将使电话静音。

I am new to android development. 我是android开发的新手。 I have tried to make the application check if the phone is in State-Rining. 我试图让应用程序检查手机是否处于“禁止冲洗”状态。 If it is, then the app checks if the value of the proximity sensor ==0 (near value). 如果是,则应用程序会检查接近传感器的值是否== 0(接近值)。 If the value for proximity sensor is 0, it implies that the sensor was cover during the ringing ad hence it should mute the call. 如果接近传感器的值为0,则表示在响铃广告期间该传感器被遮盖,因此应使通话静音。 But it is still in ringing state. 但是它仍然处于振铃状态。

I think the problem is that the ServiceReceiver only checks for the value of the proximity sensor just as the phone starts to ring. 我认为问题在于,ServiceReceiver仅在电话开始响铃时才检查接近传感器的值。 If the sensor value is not zero at that instant, the phone doesn't get silenced. 如果那一刻传感器值不为零,则电话不会静音。 So how can I make it check for a change in value of proximity sensor throughout the ringing or incoming call such that as soon as the sensor value turns to zero, call is silenced 因此,如何使其在整个振铃或来电过程中检查接近传感器的值是否发生变化,以便一旦传感器值变为零,就立即使呼叫静音

The first class "mainactivity" contains the code for proximity sensor. 第一类“ mainactivity”包含接近传感器的代码。

package example.ringcheck;


import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity implements SensorEventListener {
static TextView proxText;
SensorManager sm;
Sensor proxSensor;
static float proxValue = 8;
// static boolean muteCall = false;
private AudioManager amanager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    sm = (SensorManager) getSystemService(SENSOR_SERVICE);
    proxSensor = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
    proxText = (TextView) findViewById(R.id.ProximityTextView);

    sm.registerListener(this, proxSensor, SensorManager.SENSOR_DELAY_NORMAL);

}



@Override
public 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;
}

@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
    // TODO Auto-generated method stub

}

@Override
public void onSensorChanged(SensorEvent event) {


    // TODO Auto-generated method stub
    proxText.setText(String.valueOf(event.values[0]));
    proxValue = event.values[0];
    Log.d("SENSOR VALUE", "********" + proxValue + "*******");



    }

}

The second class "ServiceReceiver" contains the code for proximity sensor. 第二类“ ServiceReceiver”包含接近传感器的代码。

package example.ringcheck;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.telephony.TelephonyManager
import android.util.Log;

public class ServiceReceiver extends BroadcastReceiver {

private AudioManager amanager;
// static boolean isRinging = false;

public void onReceive(Context context, Intent intent) {

    if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
            TelephonyManager.EXTRA_STATE_RINGING)) {
        // isRinging = true;
            if (MainActivity.proxValue == 0) {
                amanager = (AudioManager) context
                        .getSystemService(Context.AUDIO_SERVICE);
                amanager.setRingerMode(0x00000000);
                // isRinging = false;

            }

    Log.d("MPR", "Its Ringing");



    }
}


}

Thanks in advance :-) 提前致谢 :-)

Instead of 代替

amanager.setRingerMode(0x00000000);  

Try 尝试

amanager.setStreamMute(AudioManager.STREAM_RING, true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM