0
我正在做一個Android應用程序,啓用不同的功能,人們可以通過搖動手機來撥打預定義的號碼以獲取幫助。當一個人撥打特定號碼時,我做了一次自定義。但是,我沒有任何想法:如何通過搖動手機來結束通話
我如何通過在同一方向搖晃來關閉呼叫? 如何雙搖去另一個電話等..
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER || event.values.length < 2)
return;
currentTime = System.currentTimeMillis();
if ((currentTime - lastUpdate) > 100) {
long diffTime = (currentTime - lastUpdate);
lastUpdate = currentTime;
current_x = event.values[DATA_X];
current_y = event.values[DATA_Y];
current_z = event.values[DATA_Z];
currenForce = Math.abs(current_x+current_y+current_z - last_x - last_y - last_z)/diffTime * 10000;
if (currenForce > FORCE_THRESHOLD && inCall ==0) {
if (sp.contains("number") && sp.getString("number","").length() > 5){
// Device has been shaken now go on and do something
// you could now inform the parent activity ...
String phoneNumber = sp.getString("Number", "1800");
android.net.Uri uri = android.net.Uri.fromParts("tel", phoneNumber, null);
try{
inCall =1;
android.content.Intent callIntent = new android.content.Intent(android.content.Intent.ACTION_CALL, uri);
callIntent.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(callIntent);
}catch(Exception e){
inCall =0;
e.printStackTrace();
}
}else{
Toast.makeText(this, "Please Configure number first", Toast.LENGTH_LONG);
}
}else{
try{
Toast.makeText(getBaseContext(), "Trying to End Call", Toast.LENGTH_LONG);
// Send Key For End Call
new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_ENDCALL);
inCall =0;
}catch(Exception e){
e.printStackTrace();
}
}
last_x = current_x;
last_y = current_y;
last_z = current_z;
}
在此先感謝