资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

android代码实现打电话和发送短信功能

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;

}

}

}


网站标题:android代码实现打电话和发送短信功能
网站网址:http://cdkjz.cn/article/jijjed.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220