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

时间:2022-11-10 13:06:37

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

<span style="font-size:18px;">Intent intent = new Intent(<span style="color:#ff0000;">Intent.ACTION_DIAL</span>, Uri.parse("tel:10086");
startActivity(intent);</span>

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

<span style="font-size:18px;">Intent intent = new Intent(<span style="color:#ff0000;">Intent.ACTION_CALL</span>,Uri.parse("tel:10086"));  
startActivity(intent);
</span>

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

Uri uri = Uri.parse("smsto:10010");          
Intent it = new Intent(Intent.<span style="color:#ff0000;">ACTION_SENDTO</span>, 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<String> 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<String> 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));