我正在尝试实施一个程序,当磁场传感器被某个值改变时,该程序结束通话。我设法显示一个toast,以显示应用程序在应用程序打开时检测到这一点。 我现在想扩展它,以便:
基本上,我希望仅在选中此框并且呼叫响铃时才调用传感器更改类,即使应用程序未打开,应用程序也应检查此类。
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());
}
}
});
}