發送短息和撥打電話功能
Email:chentravelling@163.com
搗騰了一小會,實現了撥打電話和發送短信的功能,作為手機的基本功能,還是值得一嘗試,在此小結一下。
環境:
IDE:Android Studio
JDK:1.8
系統:win7 64位
1.撥打電話
- 步驟1:在AndroidMainfest.xml文件中獲取撥打電話的權限,增加以下內容:
~~~
<uses-permission android:name="android.permission.CALL_PHONE" />
~~~
- 步驟2:通過Intent作為通信橋梁,實現打電話。在Activity中增加以下代碼:
~~~
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+phone));
try{
startActivity(callIntent);
}catch(Exception e)
{
return;
}
~~~
備注:
(1)這段代碼可以有其他形式,無非都是設置intent對象的一些參數,主要是撥打的電話號碼。
(2)如果沒有try{}..catch{}塊,會提示錯誤的,但是看到網上很多版本都沒有try..catch,至少我遇到錯誤了。
(3)其次可以通過checkPermission()來判斷是否獲取了權限。
(4)Intent.ACTION_CALL是可以直接撥打電話的,如果使用Intent.ACTION_DIAL,只是進入撥號界面。
2.發送短息
- 步驟1:在AndroidMainfest.xml文件中獲取發送短信的權限,增加以下內容:
~~~
<uses-permission android:name="android.permission.SEND_SMS" />
~~~
- 步驟2:自定義編輯短信的edit_message.xml:
~~~
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="3">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="53dp"
android:layout_row="0"
android:layout_column="0">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/phoneText"
android:layout_weight="1"
android:hint="聯系人" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="443dp"
android:layout_row="2"
android:layout_column="0">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/contentText"
android:layout_weight="0.75"
android:layout_gravity="bottom"
android:hint="輸入信息" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="發送"
android:id="@+id/sendButton"
android:layout_gravity="bottom" />
</LinearLayout>
</GridLayout>
~~~
- 步驟3:(本人的需求是點擊listView中的一個聯系人,選擇發短信,再跳轉到自定義的編輯短信的界面)activity跳轉,傳遞電話號碼,在Activity中增加以下代碼:
~~~
Intent messageIntent = new Intent(ShowAddressActivity.this,SendMessageActivity.class);
messageIntent.putExtra("phone",phone);
startActivity(messageIntent);
~~~
- 步驟4:在發送短信的activity中獲取電話號碼,顯示在editText中,在發送時重新獲取EditText中電話號碼,因為電話號碼可以手動編輯
~~~
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.List;
public class SendMessageActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_message);
//將選擇的電話號碼顯示
String phone = this.getIntent().getStringExtra("phone");
final EditText phoneText = (EditText)findViewById(R.id.phoneText);
phoneText.setText(phone);
//獲取短信內容
final EditText contentText = (EditText)findViewById(R.id.contentText);
Button sendButton = (Button)findViewById(R.id.sendButton);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//獲取電話號碼
String phone = phoneText.getText().toString();
String content =contentText.getText().toString();
if("".equals(content)){
new AlertDialog.Builder(SendMessageActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("警告")
.setMessage("請輸入短信內容")
.setPositiveButton("確定",null)
.show();
return;
}
if("".equals(phone)){
new AlertDialog.Builder(SendMessageActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("警告")
.setMessage("請輸入聯系人號碼")
.setPositiveButton("確定",null)
.show();
return;
}
SmsManager smsManager = SmsManager.getDefault();
if(content.length()>70)
{
List<String> contents = smsManager.divideMessage(content);
for(String sms:contents)
{
smsManager.sendTextMessage(phone,null,sms,null,null);
}
}
else
{
smsManager.sendTextMessage(phone,null,content,null,null);
}
Toast.makeText(SendMessageActivity.this, R.string.str_remind_sms_send_finish, Toast.LENGTH_SHORT).show();
}
});
}
}
~~~
備注:R.string.str_remind_sms_send_finish ="發送完成"。
綜上就實現了撥打電話和發送短信的功能,安裝在手機上測試了一下,還是挺滿意,除了界面~需要進一步完善的地方:1)短信功能暫時不支持多人同時發送,其實想想這個功能也是比較容易實現的~2)通話記錄和短信記錄暫時沒有設計數據庫進行保存。3)支持發送圖片等。