当传感器接收到某些值的android时结束来电

时间:2013-05-14 22:59:03

标签: java android service checkbox

我正在尝试实施一个程序,当磁场传感器被某个值改变时,该程序结束通话。我设法显示一个toast,以显示应用程序在应用程序打开时检测到这一点。 我现在想扩展它,以便:

  1. 勾选复选框后,它将作为服务在后台运行 (我不确定我是否正确调用了服务内容)
  2. 传感器只应在有来电时收听。
  3. 基本上,我希望仅在选中此框并且呼叫响铃时才调用传感器更改类,即使应用程序未打开,应用程序也应检查此类。

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

0 个答案:

没有答案