【问题标题】:Implementing phone call service in Xamarin Android在 Xamarin Android 中实现电话服务
【发布时间】:2018-08-15 05:40:10
【问题描述】:

我是 Xamarin 的新手。我有一个 Xamarin Forms 应用程序,它需要为 ios 和 android 实现电话服务。

这是我在.net共享项目下的界面:

using System;
using System.Collections.Generic;
using System.Text;

namespace CallMeMaybe.Services
{
   public interface ICallService
  {
     void MakePhoneCall(string number);
  }
}

这是视图模型中的命令方法:

   private void OnCall(object s)
    {
        DependencyService.Get<ICallService>().MakePhoneCall(Convert.ToString(s));
    }

这是我的Android Call Service,需要实现MakePhoneCall方法

using Android.App;
using Android.Content;
using CallMeMaybe.Droid.Services;
using CallMeMaybe.Services;
using Xamarin.Forms;

[assembly: Dependency(typeof(CallService))]
namespace CallMeMaybe.Droid.Services
{
    [Activity(Label = "CallService")]
    public class CallService : ICallService
    {

        public void MakePhoneCall(string number)
        {

            var uri = Android.Net.Uri.Parse("tel:" + number);
            var intent = new Intent(Intent.ActionCall, uri);
            //Start Activity here?

        }
    }
}

如何在我的 Android 方法中启动拨号活动?这是一个服务类而不是 Activity 类,所以我没有扩展 Activity。

【问题讨论】:

    标签: android xamarin xamarin.forms xamarin.android


    【解决方案1】:

    首先这个类不是activiry,所以你需要删除[Activity(Label = "CallService")]

        public void MakePhoneCall(string number)
        {
            var intent = new Intent(Intent.ActionCall, Android.Net.Uri.Parse(
                                                                  "tel:" + Uri.EscapeDataString(phoneNumber)));
            Android.App.Application.Context.StartActivity(intent);
        }
    

    此意图将打开通话应用并开始通话

    通过执行StartActivity(intent) 命令Android 打开Calling App Activity 并开始通话

    【讨论】:

      【解决方案2】:

      现在工作正常。使用应用程序上下文启动活动

       public void MakePhoneCall(string number)
          {
              var uri = Android.Net.Uri.Parse("tel:" + number);
              Intent intent = new Intent(Intent.ActionCall, uri);
              intent.AddFlags(ActivityFlags.NewTask);
              Android.App.Application.Context.StartActivity(intent);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多