android應用開發全程實錄-實現甩動撥打和掛斷電話


今天繼續給大家帶來《Android應用開發全程實錄》中的章節,這部分是講傳感器中的一個實例。

通過上面的例子我們學會了如何獲得某種類型的傳感器,下面通過一個實例來學習如何使用某一個類型的傳感器。我們以加速傳感器為例,來實現這樣一個功能:搖動手機便自動撥打某親情號碼,並實現再次搖動則掛機的功能。

 

工程目錄:EX_12_03

第一步,UI布局main.xml的代碼如下,其運行效果如圖12-10所示。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent" >
<TextView android:layout_width="fill_parent"
android:layout_height
="wrap_content"
android:text
="當前狀態:就緒"
android:id
="@+id/state"
android:textColor
="#ff0000" />
<Button android:layout_width="fill_parent"
android:layout_height
="wrap_content"
android:id
="@+id/call"
android:text
="打電話(10086)"/>
<Button android:layout_width="fill_parent"
android:layout_height
="wrap_content"
android:id
="@+id/stop"
android:text
="掛機"/>
</LinearLayout>

第二步,掛電話需要用到系統的Itelephony類的支持,由於Google從Android 2.1 SDK版本后,屏蔽了com.android.internal.telephony. Itelephony類,因此,需要從Android源碼下載,找到該類並導入到工程目錄下,記得包名一致。

第三步,Activity類ShakeSensorActivity的實現。該類實現SensorListener接口,添加加速度偵聽事件,通過判斷設備X、Y、Z方向的總晃動值來判斷是否啟動打電話和掛機操作。以撥打10086測試為例,當設備總晃動值大於100作為一個測試判斷點,如果當前沒有在通話界面,就通過Intent啟動撥打電話,否則就掛機操作。設備搖動時,啟動電話、掛機的界面狀態如圖圖12-11、圖12-12所示。

 

 

 

▲圖12-10  軟件運行效果圖            12-11  電話啟動界面                                   12-12  搖動設備掛機時的狀態界面

下面就來看看代碼:

public class ShakeSensorActivity extends Activity implements SensorListener {
private float lastX;
private float lastY;
private float lastZ;
private View mainView;
private long currTime;
private long lastTime;
private long duration;// 持續時間 
private float currShake;
private float totalShake;
private ITelephony iTelephony;
private boolean isCalling = false;
SensorManager sm = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainView = LinearLayout.inflate(this, R.layout.main, null);
setContentView(mainView);
((Button) mainView.findViewById(R.id.call)).setOnClickListener(new
OnClickListener() {
@Override
public void onClick(View v) {
// 打電話
callPhoneNumber10086();
}
});
((Button) mainView.findViewById(R.id.stop)).setOnClickListener(new OnClick- Listener() {
@Override
public void onClick(View v) {
// 掛機
closePhone();
}
});
// 獲取傳感器管理器
sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
// 注冊加速度傳感器 
sm.registerListener(this,
SensorManager.SENSOR_ACCELEROMETER,SensorManager. SENSOR_DELAY_NORMAL);

}
@Override
public void finish() {
// TODO Auto-generated method stub
super.finish();
sm.unregisterListener(this);// 注銷偵聽

}
@Override
public void onAccuracyChanged(int sensor, int accuracy) {
// 精度改變,該方法實質上不做任何操作,它只在每次調用時,添加一個日志項
}
@Override
public void onSensorChanged(int sensor, float[] values) {
float x = values[0];
float y = values[1];
float z = values[2];
currTime = System.currentTimeMillis();
if (lastX == 0 && lastY == 0 && lastZ == 0) {
// 第一次shake
lastTime = currTime;
}
if (currTime - lastTime > 200) {// 200毫秒檢測一次
duration = currTime - lastTime;
currShake = (Math.abs(x - lastX) + Math.abs(y - lastY) + Math.abs(z - lastZ))/ duration * 200;
}
totalShake = totalShake + currShake;
if (totalShake > 100) {
totalShake = 0;// 重置為0,重新累計計數
lastX = 0;
lastY = 0;
lastZ = 0;
lastTime = 0;
currTime = 0;
if (!isCalling) {
callPhoneNumber10086();
((TextView) mainView.findViewById(R.id.state)).setText("當前狀態: 通話中...");
} else {
closePhone();
((TextView) mainView.findViewById(R.id.state)).setText("當前狀態:通話結束...");
}
}
lastX = x;
lastY = y;
lastZ = z;
lastTime = currTime;
}

/**
* tell 10086打開通話界面
*/
private synchronized void callPhoneNumber10086() {
isCalling = true;
Intent myIntentDial = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + 10086));
startActivity(myIntentDial);
}

/**
* 結束通話
*/
private synchronized void closePhone() {
try {
getTelephony();
iTelephony.endCall();
isCalling = false;
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

/**
* 初始電話 實例
*/
public void getTelephony() {

TelephonyManager telMgr = (TelephonyManager)
this.getSystemService(Service. TELEPHONY_SERVICE);
Class<TelephonyManager> c = TelephonyManager.class;
Method getITelephonyMethod = null;
try {
getITelephonyMethod = c.getDeclaredMethod("getITelephony",(Class[]) null);
getITelephonyMethod.setAccessible(true);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}

try {
iTelephony = (ITelephony)
getITelephonyMethod.invoke(telMgr,(Object[])null);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}

}

通過getTelephony()方法,初始一個iTelephony實例,方便調用,目前只用到了掛機關閉通話,打電話也可以通過iTelephony.dial(“10086”)直接撥打。這樣就輕松實現了用傳感器實現甩動打、掛電話功能。



 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM