0

我正在尝试实现一个程序,该程序在磁场传感器已更改某个值时结束呼叫。我设法显示了一个 toast,以表明应用程序在打开应用程序时确实检测到了这一点。我现在想扩展它,以便:

  1. 勾选复选框后,它作为服务在后台运行(我不确定我是否正确调用了服务内容)
  2. 传感器应该只在有来电时监听。

基本上,我希望仅在选中该框并且呼叫正在响铃时才调用我的传感器更改类,即使应用程序未打开,应用程序也应检查这一点。

public class MainActivity extends Activity implements SensorEventListener {

CheckBox calls, texts, events;
TextView one, two, three;
SensorManager sm;
Sensor sensor;
float sensorX, sensorY, sensorZ, sensorX2, sensorY2, sensorZ2;
float ex, why, zed, diff, mdiff;
int count;  
TelephonyManager tm;
AudioManager audio;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    linktoxml();

    sensorX = sensorY = sensorZ = sensorX2 = sensorY2= sensorZ2=0;
    mdiff = -30;
    diff = 30;
    count = 0;


    calls.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //turn on call magnets

             if (calls.isChecked()){
                 startService(new Intent());


                 if(TelephonyManager.EXTRA_STATE.equals(TelephonyManager.EXTRA_STATE_RINGING))
                 {
                     sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
                    sensor = sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
                    sm.registerListener(MainActivity.this, sensor, SensorManager.SENSOR_DELAY_FASTEST);
                    audio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
                     //start listener for magnet    
                  //Toast.makeText(getApplicationContext(),"Phone Is Riging", Toast.LENGTH_LONG).show();

                 }


             }else{
                 stopService(new Intent());
             }
            }
    });


}   
4

0 回答 0