RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
android打电话发送短信

1.XML布局

创新互联建站-专业网站定制、快速模板网站建设、高性价比桐乡网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式桐乡网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖桐乡地区。费用合理售后完善,十年实体公司更值得信赖。

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;

}

}

}


当前名称:android打电话发送短信
文章链接:http://sczitong.cn/article/jsgpss.html