當你要打電話給女朋友總是有股衝動拿手機撥打吧?也就是因為那股衝動,使你必須Intent去撥電話。
所以在Android的世界裡,你要做某些事情必須透過Intent去處理,Intent對我來說是一個關鍵元件,也是一個很爛的元件。
程式片段主要建構一個Intent,將動作指定為打電話,設定意圖要打給誰的號碼,切記一定要加上『tel:』,不然會出現錯誤,要打電話前面本來就應該要加上tel讓該方法去篩選Action的Method
package com.cyfang.callphone;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class CallPhone extends Activity {
private Button button = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_call_phone);
init();
}
private final void init() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(clickListener);
}
private Button.OnClickListener clickListener = new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:09xxxxxxxx"));
startActivity(intent);
}
};
}
要記得加入以下這行權限到AndroidManifest.xml:
uses-permission android:name="android.permission.CALL_PHONE"/>
參考文章:
http://developer.android.com/reference/android/content/Intent.html
http://www.helloandroid.com/tutorials/how-make-phone-call-your-application