拨打电话简单Demo

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:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请输入电话号码:" />
<EditText
android:id="@+id/editText1"
android:phoneNumber="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="查询电话号码"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="CALL"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

AndroidManifest.xml: 
注意里面权限的添加:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.amaker.call"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>

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

</application>
</manifest>

[DllImport("phone.dll")] private static extern IntPtr PhoneMakeCall(ref PhoneMakeCallInfo ppmci); /// /// Dials the specified phone number. /// /// Phone number to dial. public static void MakeCall(string PhoneNumber) { MakeCall(PhoneNumber, false); } /// /// Dials the specified phone number. /// /// Phone number to dial. /// Prompts the user before the call is placed. unsafe public static void MakeCall(string PhoneNumber, bool PromptBeforeCall) { IntPtr res; PhoneNumber += '\0'; char[] cPhoneNumber = PhoneNumber.ToCharArray(); fixed (char* pAddr = cPhoneNumber) { PhoneMakeCallInfo info = new PhoneMakeCallInfo(); info.cbSize = (IntPtr)Marshal.SizeOf(info); info.pszDestAddress = (IntPtr)pAddr; if (PromptBeforeCall) { info.dwFlags = (IntPtr)PMCF_PROMPTBEFORECALLING; } else { info.dwFlags = (IntPtr)PMCF_DEFAULT; } res = PhoneMakeCall(ref info); } } } /// /// Reads information from the Subscriber Identity Module (SIM) /// public class Sim { private static long SERVICE_PROVIDER = 0x00006F46; [StructLayout(LayoutKind.Sequential)] private struct SimRecord { public IntPtr cbSize; public IntPtr dwParams; public IntPtr dwRecordType; public IntPtr dwItemCount; public IntPtr dwSize; } [DllImport("sms.dll")] private static extern IntPtr SmsGetPhoneNumber(IntPtr psmsaAddress); [DllImport("cellcore.dll")] private static extern IntPtr SimInitialize(IntPtr dwFlags, IntPtr lpfnCallBack, IntPtr dwParam, out IntPtr lphSim); [DllImport("cellcore.dll")] private static extern IntPtr SimGetRecordInfo(IntPtr hSim, IntPtr dwAddress, ref SimRecord lpSimRecordInfo); [DllImport("cellcore.dll")] private static extern IntPtr SimReadRecord(IntPtr hSim, IntPtr dwAddress, IntPtr dwRecordType, IntPtr dwIndex, byte[] lpData, IntPtr dwBufferSize, ref IntPtr lpdwBytesRead); [DllImport("cellcore.dll")] private static extern IntPtr SimDeinitialize(IntPtr hSim); /// /// Gets the phone number from the SIM. /// /// PhoneAddress structure with phone number. unsafe public static PhoneAddress GetPhoneNumber() { PhoneAddress phoneaddr = new PhoneAddress(); Byte[] buffer = new Byte[516]; fixed (byte* pAddr = buffer) { IntPtr res = SmsGetPhoneNumber((IntPtr)pAddr); if (res != IntPtr.Zero) throw new Exception("Could not get phone number from SIM"); byte* pCurrent = pAddr; phoneaddr.AddressType = (AddressType)Marshal.ReadInt32((IntPtr)pCurrent); pCurrent += Marshal.SizeOf(phoneaddr.AddressType); phoneaddr.Address = Marshal.PtrToStringUni((IntPtr)pCurrent); } return phoneaddr; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值