如何使用xamarin(如WindowManager)获得类似truecaller的弹出窗口?

时间:2016-10-06 07:59:08

标签: c# android xamarin popup

我正在尝试开发一个类似于呼叫管理器的应用,它可以检测来电并在屏幕上显示来电显示。

我正在显示相同但是我想要一个像truecaller一样的弹出窗口。在Xamarin中是否有任何替代用于Java的[WindowManager] link

请帮助..

我创建了一个broadcastReceiver类

[BroadcastReceiver(Exported = true, Label = "Call Manager")]
[IntentFilter(new[] { "android.intent.action.PHONE_STATE", "IncomingCall" })]
public class CallServiceReceiver : BroadcastReceiver
{

    private const string tag = "CallBroadcastReceiver";
    private const string IntentAction = "android.intent.action.PHONE_STATE";



    public override void OnReceive(Context context, Intent intent)
    {
        string state = intent.GetStringExtra(TelephonyManager.ExtraState);
        if (intent.Action == "IncomingCall")
        {
            string text = intent.GetStringExtra("MyData") ?? "Data Not Available";
            Toast.MakeText(context, text, ToastLength.Short).Show();
            Intent intents = new Intent(context, typeof(MainActivity));
            intents.AddFlags(ActivityFlags.NewTask);
            context.StartActivity(intents);
        }

        if (state == TelephonyManager.ExtraStateRinging)
        {

            string telephone = intent.GetStringExtra(TelephonyManager.ExtraIncomingNumber);

            if (string.IsNullOrEmpty(telephone))
                telephone = string.Empty;

            Toast.MakeText(context, "Incoming Call From" + telephone, ToastLength.Long).Show();

            Notification.Builder builder = new Notification.Builder(context)
                   .SetContentTitle("Call from")
                   .SetContentText("Incoming call from" + telephone)
                   .SetSmallIcon(Resource.Drawable.logo);
            Notification notification = builder.Build();
            NotificationManager notificationManager = context.GetSystemService(Context.NotificationService) as NotificationManager;
            const int notificationId = 0;
            notificationManager.Notify(notificationId, notification);

            Intent intents = new Intent(context, typeof(MainActivity));
            intents.AddFlags(ActivityFlags.NewTask);
            context.StartActivity(intents);

        }

这是我的服务类

[Service]
class CallService : IntentService
{
    protected override void OnHandleIntent(Intent intent)
    {

    }
    [return: GeneratedEnum]
    public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
    {
        new Task(() =>
        {
            while (true)
            {
                Thread.Sleep(1000000);
                Intent intents = new Intent();
                intents.SetAction("IncomingCall");
                intents.PutExtra("MyData", "Data from Service");
                SendBroadcast(intents);
            }
        }
        ).Start();
        return StartCommandResult.Sticky;
    }
}

0 个答案:

没有答案