- package com.amaker.call;
- import android.app.Activity;
- import android.content.Intent;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- /**
- * 1,打电话测试
- *,2,从联系人中获取电话号码,拨打
- * ZZL
- */
- public class MainActivity extends Activity {
- private Button btn_select;
- private Button btn_call;
- private EditText et_number;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- btn_select = (Button) findViewById(R.id.button1);
- btn_call = (Button) findViewById(R.id.button2);
- et_number = (EditText) findViewById(R.id.editText1);
- btn_select.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- select();
- }
- });
- btn_call.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- call();
- }
- });
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- Uri uri = data.getData();
- String[] strs = {"number"};
- Cursor c = managedQuery(uri, strs, null, null, null);
- c.moveToFirst();
- String number = c.getString(c.getColumnIndexOrThrow("number"));
- et_number.setText(number);
- }
- //查找联系人
- void select(){
- Intent intent = new Intent();
- String action = Intent.ACTION_GET_CONTENT;
- String type = "vnd.android.cursor.item/phone";
- intent.setAction(action);
- intent.setType(type);
- startActivityForResult(intent, 0);
- }
- //打电话
- void call(){
- String action = Intent.ACTION_CALL;
- String number = et_number.getText().toString();
- Uri data = Uri.parse("tel:"+number);
- Intent intent = new Intent();
- intent.setAction(action);
- intent.setData(data);
- startActivity(intent);
- }
- }
main.xml:
- "1.0" encoding="utf-8"?>
- xmlns:android=""
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
-
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="请输入电话号码:" />
-
- android:id="@+id/editText1"
- android:phoneNumber="true"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- android:text="查询电话号码"
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- android:text="CALL"
- android:id="@+id/button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
AndroidManifest.xml:
注意里面权限的添加:
- "1.0" encoding="utf-8"?>
"" - package="com.amaker.call"
- android:versionCode="1"
- android:versionName="1.0">
-
"8" /> -
"android.permission.READ_CONTACTS" > -
"android.permission.CALL_PHONE" > -
"@drawable/icon" android:label="@string/app_name"> -
".MainActivity" - android:label="@string/app_name">
-
-
"android.intent.action.MAIN" /> -
"android.intent.category.LAUNCHER" /> -