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



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值