1、说明
通过Xamarin.Android实现拨打电话的功能。VS2019社区版
2、效果实现
3、实现过程
3.1 Manifest配置
必须得有拨打电话的权限设定。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.1" package="com.companyname.dialdemo" android:installLocation="auto">
<uses-sdk android:minSdkVersion="24" android:targetSdkVersion="30" />
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" /> <!--拨打电话的权限申请-->
<queries>
<intent>
<action android:name="android.intent.action.DIAL" /> <!--引用安卓的拨号功能-->
<data android:scheme="tel" />
</intent>
</queries>
</manifest>
3.2 界面设置
layout_main.xml
文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="158.0dp"
android:layout_height="wrap_content"
android:id="@+id/editText1"
android:inputType="text" />
<Button
android:text="@string/dial_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="25px"
android:minHeight="25px"
android:id="@+id/button1"
android:layout_below="@id/editText1"
/>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button1"
android:id="@+id/coordinatorLayout1"
android:layout_centerVertical="true"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
3.3 String资源
String.xml的内容如下:
<resources>
<string name="app_name">DialDemo</string>
<string name="action_settings">Settings</string>
<string name="permission_call_phone">您是否同意授予拨打电话的权限</string>
<string name="ok">同意授予</string>
<string name="permission_available_call">已同意授权</string>
<string name="permissions_not_granted">未能授权</string>
<string name="dial_btn">拨打电话</string>
</resources>
3.4 具体实现
using Android;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Views;
using Android.Widget;
using AndroidX.AppCompat.App;
using AndroidX.CoordinatorLayout.Widget;
using AndroidX.Core.App;
using Google.Android.Material.Snackbar;
using System;
namespace DialDemo
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
private Button dialBtn;
private EditText phoneNo;
private int REQUEST_LOCATION = 1;
private CoordinatorLayout coordinatorLayout;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
dialBtn = FindViewById<Button>(Resource.Id.button1);
phoneNo = FindViewById<EditText>(Resource.Id.editText1);
coordinatorLayout = FindViewById<CoordinatorLayout>(Resource.Id.coordinatorLayout1);
dialBtn.Click += DialBtn_Click;
}
//判断权限并进行授权
public void checkAndGetPermission()
{
if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.CallPhone))
{
var requiredPermissions = new String[] { Manifest.Permission.CallPhone };
Snackbar.Make(coordinatorLayout,
Resource.String.permission_call_phone,
Snackbar.LengthIndefinite)
.SetAction(Resource.String.ok,
new Action<View>(delegate (View obj)
{
ActivityCompat.RequestPermissions(this, requiredPermissions, REQUEST_LOCATION);
}
)
).Show();
}
else
{
ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.CallPhone }, REQUEST_LOCATION);
}
}
//权限判断这儿
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
if (requestCode == REQUEST_LOCATION)
{
if ((grantResults.Length == 1) && (grantResults[0] == Permission.Granted)) //若同意授权
{
Snackbar.Make(coordinatorLayout, Resource.String.permission_available_call, Snackbar.LengthShort).Show();
call();
}
else //若不同意授权
{
Snackbar.Make(coordinatorLayout, Resource.String.permissions_not_granted, Snackbar.LengthShort).Show();
}
}
else
{
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private void DialBtn_Click(object sender, System.EventArgs e)
{
checkAndGetPermission();
}
//具体的电话的调用方法
private void call()
{
try
{
var intent = new Intent(Intent.ActionCall);
intent.SetData(Android.Net.Uri.Parse("tel:" + phoneNo.Text));
StartActivity(intent);
}
catch (Exception e1)
{
Toast.MakeText(this, e1.Message, ToastLength.Long).Show();
}
}
}
}