android实现打电话和发送短信功能例子,复制代码即可使用。
解决方法:
1、activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/root"
tools:context="com.example.ff.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打电话和发短信" />
<EditText
android:id="@+id/editNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="15dp"
android:hint="输入电话号码"
android:ems="10"
android:inputType="phone" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editSms"
android:layout_marginTop="10dp"
android:minLines="3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editNum"
android:layout_below="@+id/editNum"
android:hint="请输入短信内容"
android:ems="10" />
<Button
android:id="@+id/btnCall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editSms"
android:layout_below="@+id/editSms"
android:layout_marginLeft="21dp"
android:layout_marginTop="10dp"
android:text="拨打电话" />
<Button
android:id="@+id/btnSms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/editSms"
android:layout_alignTop="@+id/btnCall"
android:layout_marginRight="50dp"
android:text="发短信" />
</RelativeLayout>
2、MainActivity.java
package com.example.ff;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText editNum;
Button btnCall; //打电话
EditText editSms;
Button btnSms; //发短信
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editNum = (EditText) findViewById(R.id.editNum);
btnCall = (Button) findViewById(R.id.btnCall);
editSms = (EditText) findViewById(R.id.editSms);
btnSms = (Button) findViewById(R.id.btnSms);
//打电话
btnCall.setOnClickListener(listenerCall);
btnSms.setOnClickListener(listenerSms);
}
View.OnClickListener listenerCall = new View.OnClickListener() {
@Override
public void onClick(View v) {
//得到电话号码
String phone =editNum.getText().toString();
//判断电话是否为空
if("".equals(phone)){
Toast.makeText(MainActivity.this, "电话号码不能为空", Toast.LENGTH_SHORT)
.show();
return;
}
//使用Intent调用打电话的功能
Intent intent = new Intent();
//设置Action和Uri
intent.setAction(Intent.ACTION_CALL);
//设置数据
intent.setData(Uri.parse("tel:"+phone));
startActivity(intent);
//电话号码清空
editNum.setText("");
}
};
View.OnClickListener listenerSms = new View.OnClickListener() {
@Override
public void onClick(View v) {
//得到电话号码
String phone = editNum.getText().toString();
//判断电话是否为空
if("".equals(phone)){
Toast.makeText(MainActivity.this, "电话号码不能为空", Toast.LENGTH_LONG)
.show();
return;
}
//得到短信内容
String smsContent = editSms.getText().toString();
if("".equals(smsContent)){
Toast.makeText(MainActivity.this, "短信内容不能为空", Toast.LENGTH_LONG)
.show();
return;
}
//使用Intent调用打电话的功能
Intent intent = new Intent();
//设置Action和Uri
intent.setAction(Intent.ACTION_SENDTO);
//设置数据
intent.setData(Uri.parse("smsto:"+phone));
//指定短信的内容
intent.putExtra("sms_body", smsContent);
startActivity(intent);
//电话号码清空
editNum.setText("");
editSms.setText("");
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("电话拨号").setIcon(android.R.drawable.ic_menu_call);
menu.add("回到Home").setIcon(android.R.drawable.ic_menu_view);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
String title = item.getTitle().toString();
//调用系统拨号界面
if(title.equals("电话拨号")){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+editNum.getText().toString()));
startActivity(intent);
}else if(title.equals("回到Home")){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
//添加category属性
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
return true;
}
}
3、AndroidManifest.xml修改
在uses-sdk标签后面添加(设置打电话权限):<uses-permission android:name="android.permission.CALL_PHONE"/>
本文地址:http://www.yayihouse.com/yayishuwu/chapter/1155