1.拨打电话:
核心代码:
try {
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + telnum));
startActivity(intent);
}
catch (Exception ex) {
ex.printStackTrace();
}
在AndroidManifest.xml 文件中添加拨打电话权限:
<uses-permission android:name="android.permission.CALL_PHONE"/>
2.发送短信:
核心代码:
1 //---定义发送完动作
2 public final String SENT_SMS_ACTION = "SENT_SMS_ACTION";
3
4 //---在onCreate 函数中注册发送完广播接收器(可省略,若省略,后面就不用绑定发送广播的Intent了)
5 this.registerReceiver(new BroadcastReceiver() {
6 @Override
7 public void onReceive(Context context, Intent intent) {
8 switch (getResultCode()) {
9 case Activity.RESULT_OK:
10 Toast.makeText(context, "短信发送成功!", Toast.LENGTH_LONG).show();
11 break;
12 case SmsManager.RESULT_ERROR_RADIO_OFF:
13 break;
14 case SmsManager.RESULT_ERROR_NULL_PDU:
15 break;
16 case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
17 break;
18 default:
19 break;
20 }
21 }
22 }, new IntentFilter(SENT_SMS_ACTION));
23
24
25 //---发送短信的函数
26 public void sendShortMsg(String phoneNum, String msg) {
27 SmsManager manager = SmsManager.getDefault();
28 ArrayList<String> msgList = manager.divideMessage(msg);
29 PendingIntent intent = PendingIntent.getBroadcast(this, 0,
30 new Intent(SENT_SMS_ACTION), 0);
31 try {
32 for (String msgItem : msgList) {
33 manager.sendTextMessage(phoneNum, null, msgItem, intent, null);
34 }
35 }
36 catch (Exception ex) {
37 ex.printStackTrace();
38 }
39 }
在AndroidManifest.xml 文件中添加发送短信的权限:
<uses-permission android:name="android.permission.SEND_SMS"/>