成都网站建设设计

将想法与焦点和您一起共享

android代码实现打电话和发送短信功能-创新互联

1.XML布局

创新互联从2013年成立,先为城固等服务建站,城固等地企业,进行企业商务咨询服务。为城固企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

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;

}

}

}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻名称:android代码实现打电话和发送短信功能-创新互联
网页路径:http://chengdu.cdxwcx.cn/article/hidho.html