android打电话发送短信

  

1. xml布局

  

xmlns:应用=癶ttp://schemas.android.com/apk/res-auto"

  

xmlns:工具=癶ttp://schemas.android.com/tools"

  

android: layout_width=癿atch_parent"

  

android: layout_height=癿atch_parent"

  

工具:上下文=?MainActivity"

  

android:取向=皏ertical"在

  

android: layout_width=皐rap_content"

  

android: layout_height=皐rap_content"

  

android:文本=昂怕?“

  

/比;   

android: id=癅 + id/phoneNumber"

  

android: layout_width=癿atch_parent"

  

android: layout_height=皐rap_content"/在

  

android: id=癅 + id/call"

  

android: layout_width=皐rap_content"

  

android: layout_height=皐rap_content"

  

android:文本=安Υ虻缁昂怕搿?祝辞waihui实时点差

  

android: id=癅 + id/message"

  

android: layout_width=癿atch_parent"

  

android: layout_height=皐rap_content"

  

/比;   

android: id=癅 + id/send_sms"

  

android: layout_width=皐rap_content"

  

android: layout_height=皐rap_content"

  

android:文本=胺⑺投绦拧?祝辞

  

android: id=癅 + id/send_sms_noApp"

  

android: layout_width=皐rap_content"

  

android: layout_height=皐rap_content"

  

android:文本=爸苯臃⑺投绦拧?祝辞

  

2. mainactivity   

包com.example.android013;   

进口androidx.appcompat.app.AppCompatActivity;   

进口android.annotation.SuppressLint;   

进口android.app.PendingIntent;   

进口android.content.Intent;   

进口android.net.Uri;   

进口android.os.Bundle;   

进口android.telephony.SmsManager;   

进口android.view.View;   

进口android.widget.Button;   

进口android.widget.EditText;   

进口android.widget.Toast;   

公共类MainActivity AppCompatActivity延伸实现视图。OnClickListener {

  

私人按钮调用;

  

私人按钮send_sms;

  

私人按钮send_sms_noApp;

  

私人EditText phoneNumber;

  

私人EditText消息;

  字符串

私人电话;//定义字符

  

私人字符串味精;

  

@Override   

保护无效onCreate(包savedInstanceState) {

  

super.onCreate (savedInstanceState);

  

setContentView (R.layout.activity_main);

  

调用=findViewById (R.id.call);

  

send_sms=findViewById (R.id.send_sms);

  

send_sms_noApp=findViewById (R.id.send_sms_noApp);

  

phoneNumber=findViewById (R.id.phoneNumber);

  

消息=findViewById (R.id.message);

  

call.setOnClickListener(这);

  

send_sms.setOnClickListener(这);

  

send_sms_noApp.setOnClickListener(这);

  

}   

@SuppressLint (“WrongConstant")

  

@Override   

公共空onClick(查看v) {

  

开关(v.getId ()) {

  

案例R.id.call:   

//意图

  

意图李=new意图();

  

//设置动作

  

li.setAction (Intent.ACTION_DIAL);//拨打

  

//给谁打电话

  

电话=phoneNumber.getText () .toString ();

  

li.setData (Uri.parse(“电话:“+电话));

  

//启动意图

  

startActivity(李);

  

打破;   

案例R.id.send_sms:   

//意图

  

意图li1=new意图();

  

//获取电话

  

电话=phoneNumber.getText () .toString ();

  

//获取短信

  

味精=message.getText () .toString ();

  

//设置动作

  

li1.setAction (Intent.ACTION_SENDTO);//发送到哪里

  

li1.setData (Uri.parse (“smsto:“+电话));//发送到谁

  

//发送内容键值对出现

  

li1.putExtra (“sms_body"、味精);//一条短信只能容纳140个字节70个汉字。判断长度

  

//启动意图

  

startActivity (li1);

  

打破;   

R.id。send_sms_noApp://自己发送,不启动系统自带的应用

  

//短信管理者

  

SmsManager经理=SmsManager.getDefault ();

  

//意图

  

PendingIntent PendingIntent=PendingIntent.getBroadcast (MainActivity。0,这新意图()

0);   

manager.sendTextMessage (“12306“, null,“回家正好啊“,pendingIntent, null);

  

Toast.makeText(这一点,“发送成功“,0),告诉();

  

打破;   

}   

}   

}

android打电话发送短信