使用Android拨打电话功能

1、要使用Android系统中的电话拨号功能,首先必须在AndroidManifest.xml功能清单中加入允许拨打电话的权限:

      <uses-permission android:name="android.permission.CALL_PHONE" /> // 允许拨打电话权限

 2、进行拨打电话的代码:

       a、调用Android系统的拨号界面,但不发起呼叫,用户按下拨号键才会进行呼叫

 1 @Override  
 2    public void onCreate(Bundle savedInstanceState) {  
 3        super.onCreate(savedInstanceState);  
 4        setContentView(R.layout.main);  
 5          
 6        Button callBut = (Button)findViewById(R.id.callBut);  
 7          
 8        callBut.setOnClickListener(new View.OnClickListener() {  
 9           
10         @Override  
11         public void onClick(View v) {  
12             Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel://13800138000"));  
13             startActivity(intent);  
14         }  
15     });  
16 }  

b、直接拨号发起呼叫 

 1 @Override  
 2    public void onCreate(Bundle savedInstanceState) {  
 3        super.onCreate(savedInstanceState);  
 4        setContentView(R.layout.main);  
 5          
 6        Button callBut = (Button)findViewById(R.id.callBut);  
 7          
 8        callBut.setOnClickListener(new View.OnClickListener() {  
 9           
10         @Override  
11         public void onClick(View v) {  
12             Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel://13800138000"));  
13             startActivity(intent);  
14         }  
15     });  
16 }  

注:其中Uri.parse("tel://13800138000")中的格式写成Uri.parse("tel:13800138000"),测试中也通过的。

   3、拨号相关的知识点——Linkify的使用以及android:autoLink属性的使用,自动判断字符串是电话,网址或者邮件地址:

 

        a、使用代码设置:

 

             import android.text.util.Linkify;     

           

             Linkify.addLinks(textView, Linkify.WEB_URLS|Linkify.EMAIL_ADDRESSES|Linkify.PHONE_NUMBERS);

        b、在配置中设置:android:autoLink="web|phone|email"           

             <TextView
                     android:id="@+id/tv1"
                     android:layout_width="fill_parent"
                     android:layout_height="wrap_content"

                     android:autoLink="web|phone|email"
                    />

 

 

   4、为你的电话拨号程序添加拨号键意图过滤器配置:

        <activity android:name=".CallPhoneActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            
            <intent-filter> // 当用户按下拨号键时,Android系统会弹出选择菜单让用户选择使用那个拨号器
                <action android:name="android.intent.action.CALL_BUTTON" />   
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter> 
        </activity>

 

        <intent-filter> // 功能跟上一个过滤器一样
                <action android:name="android.intent.action.CALL_PRIVILEGED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="tel" />
            </intent-filter>

注:需要加上<category android:name="android.intent.category.BROWSABLE" />才能使调用的activity收到getIntent().getAction();

posted @ 2015-04-30 17:20  骨头森林  阅读(1367)  评论(0编辑  收藏  举报