Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1063620
  • 博文数量: 403
  • 博客积分: 10272
  • 博客等级: 上将
  • 技术积分: 4407
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:22
文章分类

全部博文(403)

文章存档

2012年(403)

分类: 嵌入式

2012-04-22 20:34:42

Java代码  收藏代码
  1. package com.amaker.call;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.database.Cursor;  
  6. import android.net.Uri;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.EditText;  
  12.   
  13. /** 
  14.  * 1,打电话测试 
  15.  *,2,从联系人中获取电话号码,拨打 
  16.  * ZZL 
  17.  */  
  18. public class MainActivity extends Activity {  
  19.     private Button btn_select;  
  20.     private Button btn_call;  
  21.     private EditText et_number;  
  22.   
  23.     @Override  
  24.     public void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.main);  
  27.   
  28.         btn_select = (Button) findViewById(R.id.button1);  
  29.         btn_call = (Button) findViewById(R.id.button2);  
  30.         et_number = (EditText) findViewById(R.id.editText1);  
  31.   
  32.         btn_select.setOnClickListener(new OnClickListener() {  
  33.   
  34.             @Override  
  35.             public void onClick(View v) {  
  36.                 select();  
  37.             }  
  38.         });  
  39.   
  40.         btn_call.setOnClickListener(new OnClickListener() {  
  41.   
  42.             @Override  
  43.             public void onClick(View v) {  
  44.                 call();  
  45.             }  
  46.         });  
  47.     }  
  48.       
  49.     @Override  
  50.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  51.         super.onActivityResult(requestCode, resultCode, data);  
  52.             Uri uri = data.getData();  
  53.             String[] strs = {"number"};  
  54.             Cursor c = managedQuery(uri, strs, nullnullnull);  
  55.             c.moveToFirst();  
  56.             String number = c.getString(c.getColumnIndexOrThrow("number"));  
  57.             et_number.setText(number);  
  58.               
  59.           
  60.           
  61.     }  
  62.     //查找联系人  
  63.     void select(){  
  64.         Intent intent = new Intent();  
  65.         String action = Intent.ACTION_GET_CONTENT;  
  66.         String type = "vnd.android.cursor.item/phone";  
  67.         intent.setAction(action);  
  68.         intent.setType(type);  
  69.         startActivityForResult(intent, 0);  
  70.           
  71.     }  
  72.     //打电话  
  73.     void call(){  
  74.         String action = Intent.ACTION_CALL;  
  75.         String number = et_number.getText().toString();  
  76.         Uri data = Uri.parse("tel:"+number);  
  77.         Intent intent = new Intent();  
  78.         intent.setAction(action);  
  79.         intent.setData(data);  
  80.         startActivity(intent);  
  81.           
  82.     }  
  83. }  


main.xml:
Java代码  收藏代码
  1. "1.0" encoding="utf-8"?>  
  2.     xmlns:android=""  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.     
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="wrap_content"  
  9.         android:text="请输入电话号码:" />  
  10.     
  11.         android:id="@+id/editText1"  
  12.         android:phoneNumber="true"  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content" />  
  15.     
  16.         android:text="查询电话号码"  
  17.         android:id="@+id/button1"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content" />  
  20.     
  21.         android:text="CALL"  
  22.         android:id="@+id/button2"  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content" />  
  25.   


AndroidManifest.xml:
注意里面权限的添加:
Java代码  收藏代码
  1. "1.0" encoding="utf-8"?>  
  2. ""  
  3.       package="com.amaker.call"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     "8" />  
  7.     "android.permission.READ_CONTACTS">  
  8.     "android.permission.CALL_PHONE">  
  9.   
  10.     "@drawable/icon" android:label="@string/app_name">  
  11.         ".MainActivity"  
  12.                   android:label="@string/app_name">  
  13.               
  14.                 "android.intent.action.MAIN" />  
  15.                 "android.intent.category.LAUNCHER" />  
  16.               
  17.           
  18.   
  19.       
  20.  
阅读(1800) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~