android代碼實(shí)現(xiàn)打電話和發(fā)送短信功能-創(chuàng)新互聯(lián)

1.XML布局

創(chuàng)新互聯(lián)從2013年成立,先為城固等服務(wù)建站,城固等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為城固企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

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實(shí)時點(diǎn)差

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="發(fā)送短信"/>

android:id="@+id/send_sms_noApp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="直接發(fā)送短信"/>

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();

//設(shè)置動作

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();

//設(shè)置動作

li1.setAction(Intent.ACTION_SENDTO);//發(fā)送到哪里

li1.setData(Uri.parse("smsto:"+Phone)); //發(fā)送到誰

//發(fā)送內(nèi)容 鍵值對出現(xiàn)

li1.putExtra("sms_body",msg); //一條短信只能容納140個字節(jié)70個漢字.判斷長度

//啟動意圖

startActivity(li1);

break;

case R.id.send_sms_noApp: //自己發(fā)送,不啟動系統(tǒng)自帶的APP

//短信管理者

SmsManager manager=SmsManager.getDefault();

//意圖

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

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

Toast.makeText(this,"發(fā)送成功",0).show();

break;

}

}

}

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)站名稱:android代碼實(shí)現(xiàn)打電話和發(fā)送短信功能-創(chuàng)新互聯(lián)
路徑分享:http://muchs.cn/article40/hidho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站標(biāo)簽優(yōu)化、搜索引擎優(yōu)化、定制開發(fā)、網(wǎng)站內(nèi)鏈、營銷型網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司