一、界面设计
二、程序设计
MainActivity.java
package com.example.yxp.phone;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt_dial = (Button) findViewById(R.id.button1);
bt_dial.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String str1 = "";
EditText editText1 = (EditText) findViewById(R.id.ed_text);
str1 = editText1.getText().toString();
callPhone(str1);
}
});
Button bt_dial1 = (Button) findViewById(R.id.button2);
bt_dial1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText editText1 = (EditText) findViewById(R.id.ed_text);
String str1 = editText1.getText().toString();
String str2="";
EditText editText2 =(EditText)findViewById(R.id.ed_text1);
str2=editText2.getText().toString();
sendSMS(str1,str2);
}
});
}
/**
* 拨打电话(直接拨打电话)
* @param phoneNum 电话号码
*/
public void callPhone(String phoneNum){
Intent intent = new Intent(Intent.ACTION_CALL);
Uri data = Uri.parse("tel:" + phoneNum);
intent.setData(data);
startActivity(intent);
}
/**
* 直接调用短信接口发短信
*
* @param phoneNumber
* @param message
*/
public void sendSMS(String phoneNumber, String message) {
// 获取短信管理器
android.telephony.SmsManager smsManager = android.telephony.SmsManager
.getDefault();
// 拆分短信内容(手机短信长度限制)
List<String> divideContents = smsManager.divideMessage(message);
for (String text : divideContents) {
smsManager.sendTextMessage(phoneNumber,null,text,null,null);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入号码"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ed_text"
android:hint="需要拨打的电话"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ed_text1"
android:hint="发送短信"
android:lines="5"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="拨打"
android:id="@+id/button1" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="发短信"
android:id="@+id/button2" />
</LinearLayout>
在AndroidMainfest.xml中添加
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS" />