android手机通讯服务及手机控制

1、自动调用系统的拨号、上网和发送E-mail的功能

<TextView
        android:text="请输入电话号码或者网址或者E-mail:"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:id="@+id/editText01"
        android:layout_width="200dp"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/textView01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18dp"
        android:autoLink="phone|email|web"/>

在实现中用到了Linkify,它是一个辅助类,通过RegEx样式匹配,自动地在TextView类(和继承的类)中创建超链接。

tx = (TextView)findViewById(R.id.textView01);
        et = (EditText)findViewById(R.id.editText01);
        et.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {

                tx.setText(et.getText());
                Linkify.addLinks(tx, Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS);

                return false;
            }
        });
View Code

2、电话拨号

 1 <LinearLayout
 2         android:orientation="vertical"
 3         android:layout_width="match_parent"
 4         android:layout_height="wrap_content">
 5 
 6         <LinearLayout
 7             android:orientation="horizontal"
 8             android:layout_width="match_parent"
 9             android:layout_height="wrap_content">
10 
11             <TextView
12                 android:layout_width="wrap_content"
13                 android:layout_height="wrap_content"
14                 android:text="请输入电话号码:"
15                 />
16             <EditText
17                 android:id="@+id/phoneEt"
18                 android:layout_width="match_parent"
19                 android:layout_height="wrap_content"
20                 />
21         </LinearLayout>
22 
23         <Button
24             android:id="@+id/sureBtn"
25             android:layout_width="wrap_content"
26             android:layout_height="wrap_content"
27             android:text="确定"
28             />
29 
30 
31     </LinearLayout>

在拨打电话时,需要在AndroidManifest.xml文件中添加权限

<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
 1 final EditText phoneEt = (EditText)findViewById(R.id.phoneEt);
 2         Button sureBtn = (Button)findViewById(R.id.sureBtn);
 3         sureBtn.setOnClickListener(new View.OnClickListener() {
 4             @Override
 5             public void onClick(View view) {
 6                 String number = phoneEt.getText().toString().trim();
 7                 boolean flag = phoneNumber(number);
 8                 if (flag){
 9 
10                     Intent intent = new Intent("android.intent.action.CALL", Uri.parse("tel:" + number));
11                     startActivity(intent);
12                     et.setText("");
13 
14                 }else {
15                     Toast.makeText(Activity01.this, "输入的电话号码格式不正确", Toast.LENGTH_SHORT).show();
16                     et.setText("");
17                 }
18             }
19         });
20 
21 
22 public boolean phoneNumber(String number){
23         boolean flag = false;
24         String pare = "\\d{11}";//11个整数的手机号码正则式
25         String pare2 = "\\d{12}"; //12个
26         CharSequence num = number; //获取电话号码
27         Pattern pattern = Pattern.compile(pare); //判断是否为手机号码
28         Matcher matcher = pattern.matcher(num);
29         Pattern pattern2 = Pattern.compile(pare2); //判断是否为座机号码
30         Matcher matcher2 = pattern2.matcher(num);
31         if (matcher.matches() || matcher2.matches()){
32             flag = true;
33         }
34         return flag;
35     }
View Code

 上述拨号方法的特点就是去到拨号界面,但是实际的拨号实现是由用户点击实现的。下面介绍一种直接拨打电话的方法,也就是对于用户没有直接的提示效果,Android推荐使用第一种方法,如果是第二种方法建议给用户一个提示,是否拨打电话:

 

 1 class myBtnAction implements View.OnClickListener {
 2 
 3         @Override
 4         public void onClick(View view) {
 5             Intent intent = new Intent(Intent.ACTION_CALL);
 6             Uri data = Uri.parse("tel:" + "158********");
 7             intent.setData(data);
 8             if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
 9                 // TODO: Consider calling
10                 //    ActivityCompat#requestPermissions
11                 // here to request the missing permissions, and then overriding
12                 //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
13                 //                                          int[] grantResults)
14                 // to handle the case where the user grants the permission. See the documentation
15                 // for ActivityCompat#requestPermissions for more details.
16                 return;
17             }
18             startActivity(intent);
19         }
20     }

3、发送短信

 1         //获取一个短信的管理器,利用管理器来发送短信
 2         sendMSBtn.setOnClickListener(new View.OnClickListener() {
 3             @Override
 4             public void onClick(View view) {
 5                 String phoneNumber = et_phone.getText().toString().trim();
 6                 String message = et_message.getText().toString().trim();
 7                 if (et_phone.equals("") || et_message.equals("")){
 8                     Toast.makeText(MainActivity.this, "号码或内容不能为空", Toast.LENGTH_SHORT).show();
 9                     return;
10                 }
11 
12                 SmsManager smsManager = SmsManager.getDefault();
13                 //目标地址、服务商、短信内容、发送意图、
14                 smsManager.sendTextMessage(phoneNumber, null, message, null, null);
15             }
16         });
17 
18 
19     }        

发送短信需要添加相应的权限

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

 

posted @ 2017-08-31 11:14  hongsheng  阅读(220)  评论(0编辑  收藏  举报