300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Android实现拨打电话和发送短信 Android手机拨打电话 手动发送短信与自动拨打电话

Android实现拨打电话和发送短信 Android手机拨打电话 手动发送短信与自动拨打电话

时间:2019-02-21 02:11:43

相关推荐

Android实现拨打电话和发送短信 Android手机拨打电话 手动发送短信与自动拨打电话

Android实现手动拨打电话,即点击后跳转到手机默认电话号码输入页面,可以将相应号码传送过去:

Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:10086");

startActivity(intent);

Android实现自动拨打电话,即点击后直接拨通电话,显示为通话中的页面:

Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:10086"));

startActivity(intent);

Android实现手动发送短信,即点击后 跳转到发送短信的页面,可以将把相应内容传送过去:

Uri uri = Uri.parse("smsto:10010");

Intent it = new Intent(Intent.ACTION_SENDTO, uri);

it.putExtra("sms_body", "102");

activity.startActivity(it);

Android实现自动发送短信,即点击后直接发送短信到设置的号码:

方法一:

package com.example.smstest;

import java.util.List;

import android.app.Activity;

import android.app.PendingIntent;

import android.content.Intent;

import android.os.Bundle;

import android.telephony.SmsManager;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class MainActivity extends Activity {

private Button btn;

private String strSms = "";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btn = (Button) findViewById(R.id.send);

strSms = "在 Android 2.0 以前 应该使用 android.telephony.gsm.SmsManager 之后应该用 android.telephony.SmsManager;";

btn.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

/*

* 在 Android 2.0 以前 应该使用 android.telephony.gsm.SmsManager 之后应该用

* android.telephony.SmsManager;

*/

// 获取系统默认的短信管理器

SmsManager smsManager = SmsManager.getDefault();

PendingIntent sentIntent = PendingIntent.getBroadcast(

MainActivity.this, 0, new Intent(), 0);

// 如果字数超过70,需拆分成多条短信发送

// 按照每条短信最大字数来拆分短信

if (strSms.length() > 70) {

List msgs = smsManager.divideMessage(strSms);

for (String msg : msgs) {

/*

* 发送短信

*

* smsManager.sendTextMessage(destinationAddress,

* scAddress, text, sentIntent, deliveryIntent)

*

* -- destinationAddress:目标电话号码

*

* -- scAddress:短信中心号码,测试可以不填

*

* -- text: 短信内容

*

* -- sentIntent:发送 -->中国移动 --> 中国移动发送失败 --> 返回发送成功或失败信号

* --> 后续处理 即,这个意图包装了短信发送状态的信息

*

* -- deliveryIntent: 发送 -->中国移动 --> 中国移动发送成功 -->

* 返回对方是否收到这个信息 --> 后续处理

* 即:这个意图包装了短信是否被对方收到的状态信息(供应商已经发送成功,但是对方没有收到)。

*/

smsManager.sendTextMessage("18352513553", null, msg,

sentIntent, null);

}

} else {

smsManager.sendTextMessage("18352513553", null, strSms,

sentIntent, null);

}

}

});

}

}

方法二:

//直接调用短信接口发短信

SmsManager smsManager = SmsManager.getDefault();

List divideContents = smsManager.divideMessage(content);

for (String text : divideContents) {

smsManager.sendTextMessage("150xxxxxxxx", null, text, sentPI, deliverPI);

}

处理返回的发送状态

String SENT_SMS_ACTION = "SENT_SMS_ACTION";

Intent sentIntent = new Intent(SENT_SMS_ACTION);

PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, sentIntent,

0);

// register the Broadcast Receivers

context.registerReceiver(new BroadcastReceiver() {

@Override

public void onReceive(Context _context, Intent _intent) {

switch (getResultCode()) {

case Activity.RESULT_OK:

Toast.makeText(context,

"短信发送成功", Toast.LENGTH_SHORT)

.show();

break;

case SmsManager.RESULT_ERROR_GENERIC_FAILURE:

break;

case SmsManager.RESULT_ERROR_RADIO_OFF:

break;

case SmsManager.RESULT_ERROR_NULL_PDU:

break;

}

}

}, new IntentFilter(SENT_SMS_ACTION));处理返回的接收状态

String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";

// create the deilverIntent parameter

Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);

PendingIntent deliverPI = PendingIntent.getBroadcast(context, 0,

deliverIntent, 0);

context.registerReceiver(new BroadcastReceiver() {

@Override

public void onReceive(Context _context, Intent _intent) {

Toast.makeText(context,

"收信人已经成功接收", Toast.LENGTH_SHORT)

.show();

}

}, new IntentFilter(DELIVERED_SMS_ACTION));



Android实现拨打电话和发送短信 Android手机拨打电话 手动发送短信与自动拨打电话 自动发送短信(代码很简单哦)...

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。