1.XML布局

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity"

android:orientation="vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="号码:"

/>

android:id="@+id/phoneNumber"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

android:id="@+id/call"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="拨打电话号码"/>waihui实时点差

android:id="@+id/message"

android:layout_width="match_parent"

android:layout_height="wrap_content"

/>

android:id="@+id/send_sms"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="发送短信"/>

android:id="@+id/send_sms_noApp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="直接发送短信"/>

2.MainActivity

package com.example.android013;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;

import android.app.PendingIntent;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.telephony.SmsManager;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private Button call;

private Button send_sms;

private Button send_sms_noApp;

private EditText phoneNumber;

private EditText message;

private String Phone; //定义字符

private String msg;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

call=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);

message=findViewById(R.id.message);

call.setOnClickListener(this);

send_sms.setOnClickListener(this);

send_sms_noApp.setOnClickListener(this);

}

@SuppressLint("WrongConstant")

@Override

public void onClick(View v) {

switch (v.getId()){

case R.id.call:

//意图

Intent li=new Intent();

//设置动作

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

//给谁打电话

Phone=phoneNumber.getText().toString();

li.setData(Uri.parse("tel:"+Phone));

//启动意图

startActivity(li);

break;

case R.id.send_sms:

//意图

Intent li1=new Intent();

//获取电话

Phone=phoneNumber.getText().toString();

//获取短信

msg=message.getText().toString();

//设置动作

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

li1.setData(Uri.parse("smsto:"+Phone)); //发送到谁

//发送内容 键值对出现

li1.putExtra("sms_body",msg); //一条短信只能容纳140个字节70个汉字.判断长度

//启动意图

startActivity(li1);

break;

case R.id.send_sms_noApp: //自己发送,不启动系统自带的APP

//短信管理者

SmsManager manager=SmsManager.getDefault();

//意图

PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0,new Intent(),0);

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

Toast.makeText(this,"发送成功",0).show();

break;

}

}

}

更多相关文章

  1. Android 设置隐式意图
  2. Android之路——第二个Android小程序(Android短信发送)
  3. Android短信----发送流程---框架层(Frameworks)
  4. Android调用拨号程序,短信发送
  5. Android 短信发送
  6. Android android下的短信发送器
  7. Android 短信 彩信 wap push的接收
  8. android rom短信模块最后获取的Cursor字段内容

随机推荐

  1. 解决Gradle minifyEnabled无法找到错误
  2. 在Android系统中调用系统前置摄像头
  3. Android的图片叠加
  4. Android(安卓)抓包的一些命令 及 adb使用
  5. Android下执行Runtime.getRuntime().exec
  6. Android(安卓)自动化测试―robotium(六)只
  7. Android的Adapter
  8. [React-Native]环境安装问题
  9. Android中的对话框AlertDialog使用技巧合
  10. Android(安卓)控件之ImageSwitcher图片切