Android開發之打電話和發短息介面呼叫

NO IMAGE

Activity檔案

public class MainActivity extends Activity implements OnLongClickListener {
private EditText et_main_number;
private EditText et_main_sms;
private Button btn_main_call;
private Button btn_main_send;
private OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(v==btn_main_call) {//點選打電話
//Toast.makeText(MainActivity.this, "點選打電話", 0).show();
//1). 建立一個Intent(隱式)
//08-28 03:27:09.976: I/ActivityManager(1222): Displayed com.android.dialer/.DialtactsActivity:  535ms
String action = "android.intent.action.DIAL";
action = Intent.ACTION_DIAL;//一樣的
Intent intent = new Intent(action);
//2). 攜帶資料
String number = et_main_number.getText().toString();
intent.setData(Uri.parse("tel:" number)); //<data android:scheme="tel" />
//3). startActivity(intent)
startActivity(intent);
}else if(v==btn_main_send) {//點選發簡訊
Toast.makeText(MainActivity.this, "點選發簡訊", 0).show();
//1). 建立一個Intent(隱式)
//08-28 04:00:02.420: I/ActivityManager(1222): Displayed com.android.mms/.ui.ComposeMessageActivity:  132ms
Intent intent = new Intent(Intent.ACTION_SENDTO);//android.intent.action.SENDTO
//2). 攜帶資料(號碼/內容)
String number = et_main_number.getText().toString();
String sms = et_main_sms.getText().toString();
intent.setData(Uri.parse("smsto:" number));
//攜帶額外資料
intent.putExtra("sms_body", sms);
//3). startActivity(intent)
startActivity(intent);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化檢視物件
et_main_number = (EditText) findViewById(R.id.et_main_number);
et_main_sms = (EditText) findViewById(R.id.et_main_sms);
btn_main_call = (Button) findViewById(R.id.btn_main_call);
btn_main_send = (Button) findViewById(R.id.btn_main_send);
//給檢視物件設定點選監聽
btn_main_call.setOnClickListener(onClickListener);
btn_main_send.setOnClickListener(onClickListener);
//給檢視物件設定長按監聽
btn_main_call.setOnLongClickListener(this);
btn_main_send.setOnLongClickListener(this);
}
@Override
public boolean onLongClick(View v) {
if(v==btn_main_call) {//長按打電話
//Toast.makeText(MainActivity.this, "長按打電話", 0).show();
//1). 建立一個Intent(隱式)
//08-28 03:38:59.717: I/ActivityManager(1222): Displayed com.android.phone/.PrivilegedOutgoingCallBroadcaster:  570ms
Intent intent = new Intent(Intent.ACTION_CALL);//android.intent.action.CALL
//2). 攜帶資料
String number = et_main_number.getText().toString();
intent.setData(Uri.parse("tel:" number));
//3). startActivity(intent)
startActivity(intent);
}else if(v==btn_main_send) {//長按發簡訊
Toast.makeText(MainActivity.this, "長按發簡訊", 0).show();
//1). 得到SmsManager的物件
SmsManager smsManager = SmsManager.getDefault();
//2). 傳送文字資訊(簡訊)
String number = et_main_number.getText().toString();
String sms = et_main_sms.getText().toString();
smsManager.sendTextMessage(number, null, sms, null, null);
}
return true;//不會再觸發點選事件(表示此事件已經被消費了)
}
}

xml檔案

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="電話號碼: " />
<EditText
android:id="@ id/et_main_number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="請輸入號碼">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="簡訊內容: " />
<EditText
android:id="@ id/et_main_sms"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="請輸入簡訊">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@ id/btn_main_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打電話" />
<Button
android:id="@ id/btn_main_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="發簡訊" />
</LinearLayout>
</LinearLayout>

AndroidManifest檔案

<!-- 打電話的許可權 -->
<uses-permission android:name="android.permission.CALL_PHONE"/>
<!-- 發簡訊的許可權 -->
<uses-permission android:name="android.permission.SEND_SMS"/>