700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Android调用系统打电话和发短信功能

Android调用系统打电话和发短信功能

时间:2019-01-18 20:56:41

相关推荐

Android调用系统打电话和发短信功能

一、打电话

1、添加打电话的权限在manifast文件中。

<uses-permissionAndroid:name="android.permission.CALL_PHONE"/>

2、使用Uri.parse(String a)创建Uri。

Uri uri = Uri.parse("tel:"+1008611);

3、创建打电话的意图。

Intent intent = new Intent(Intent.ACTION_CALL, uri);

4、启动系统打电话页面。

startActivity(intent);

二、发短信

方式一:直接发送短信

1、添加发送短信的权限在manifast文件中。

<uses-permission android:name="android.permission.SEND_SMS"/>

2、获取android.telephony.SmsManager对象(PS:android.telephony.gsm.SmsManager已经废弃)。

SmsManager smsManager = SmsManager.getDefault();

3、声明一个短信内容的常量。

String content = "Hello World!";

4、将短信内容分块,发送一条短信最多能够发送70个中文字符,超过这个值系统会将短信内容分为多块进行发送。

ArrayList<String> list = smsManager.divideMessage(content);

5、分条进行发送。

for (int i = 0; i < list.size(); i++) {

smsManager.sendTextMessage("10086", null, list.get(i), null, null);

}

方式二:调用系统的发送短信的界面,需要输入号码

1、创建意图

Intent intentFinalMessage = new Intent(Intent.ACTION_VIEW);

2、设置类型

intentFinalMessage.setType("vnd.android-dir/mms-sms");

3、打开系统短信界面

startActivity(intentFinalMessage);

方式三:调用系统的发送短信的界面,不需要输入号码

1、创建Uri,设置行为和号码

Uri uri2 = Uri.parse("smsto:"+10086);

2、创建意图。

Intent intentMessage = new Intent(Intent.ACTION_VIEW,uri2);

3、打开系统短信界面,号码已经填写,只需填写要发送

startActivity(intentMessage);

三、sendTextMessage(StringdestinationAddress,StringscAddress,Stringtext,PendingIntentsentIntent,PendingIntentdeliveryIntent)参数说明:

1、destinationAddress:给这个号码发送短信。

2、scAddress:使用这个号码发送短信,为null时表示使用本机发送。

3、text:短信内容。

4、sentIntent:发送短信成功或失败之后发送广播。

5、deliveryIntent:对方接受到短信之后发送广播。

四、发送广播的短信发送

1、注册广播

注册自己发送短信的广播:

SEND_SMS_ACTION = "3";

registerReceiver(new BroadcastReceiver(){

@Override

public void onReceive(Context context, Intent intent) {

//根据结果码判断是否发送成功

if(Activity.RESULT_OK == getResultCode()){

Toast.makeText(MainActivity.this, "发送成功", Toast.LENGTH_SHORT).show();

}else{

Toast.makeText(MainActivity.this, "发送失败", Toast.LENGTH_SHORT).show();

}

}

}, new IntentFilter(SEND_SMS_ACTION));

注册对方接受到短信的广播:

BACK_SMS_ACTION = "4";

registerReceiver(new BroadcastReceiver(){

@Override

public void onReceive(Context context, Intent intent) {

//只要接收到这个广播,表示接收短信成功

Toast.makeText(MainActivity.this, "对方接收到短信", Toast.LENGTH_SHORT).show();

}

}, new IntentFilter(BACK_SMS_ACTION));

2、发送 短信,监听发送情况、监听对方接受情况,如果短信发送成功或失败

PendingIntent.getBroadcast(MainActivity.this, 0, new Intent(SEND_SMS_ACTION), 0)会发送广播,

如果对方接受到短信

PendingIntent.getBroadcast(MainActivity.this, 0, new Intent(BACK_SMS_ACTION), 0));会发送广播

for (int i = 0; i < list.size(); i++) {

smsManager.sendTextMessage("10086", null, list.get(i),

PendingIntent.getBroadcast(MainActivity.this, 0, new Intent(SEND_SMS_ACTION), 0),

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

}

五、使用smsManager发送其它格式的短信

1、sendDataMessage(StringdestinationAddress,StringscAddress, short destinationPort, byte[] data,PendingIntentsentIntent,PendingIntent

deliveryIntent)

其它参数一样,第三个参数short destinationPort,给这个号码的这个端口号发送这条短信,短信内容为字节数组格式。

2、sendMultipartTextMessage(StringdestinationAddress,StringscAddress,ArrayList<String> parts,ArrayList<PendingIntent> sentIntents,ArrayList<PendingIntent> deliveryIntents)

给这个号码发送多条短信。

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