1
wxr
2023-03-31 7e42cc13a14b7de31c9f5d5c61cdf24f3246335d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using Android.App;
using Android.Content;
using Android.Net;
using Android.Telephony;
 
namespace Shared
{
    public class MyBroadcastReceive : BroadcastReceiver
    {
        /// <summary>
        /// 响铃事件
        /// </summary>
        public static Action ActioRinging;
        /// <summary>
        /// 电话挂断事件
        /// </summary>
        public static Action ActionIdle;
        /// <summary>
        /// 是否在通话中
        /// </summary>
        public static bool IsCalling;
        public override void OnReceive (Context context, Intent intent)
        {
            //return;
                System.Console.WriteLine ($"接收到广播事:{intent.Action}");
                switch (intent.Action) {
                // 如果是拨打电话
                case "android.intent.action.NEW_OUTGOING_CALL": {
                        System.Console.WriteLine ($"播打电话->{intent.GetStringExtra (Intent.ExtraPhoneNumber)}");
                    }
                    break;
                //被打电话
                case "android.intent.action.PHONE_STATE": {
                        // 如果是来电
                        var tManager = (TelephonyManager)context.GetSystemService (Service.TelephonyService);
                        System.Console.WriteLine ($"通话状态->{tManager.CallState}");
                        switch (tManager.CallState) {
                        //响铃
                        case CallState.Ringing:
                            IsCalling = true;
                            var mIncomingNumber = intent.GetStringExtra ("incoming_number");
                            ActioRinging?.Invoke ();
                            break;
                        //接通
                        case CallState.Offhook:
                            IsCalling = true;
                            ActioRinging?.Invoke ();
                            break;
                        //挂断电话
                        case CallState.Idle:
                            IsCalling = false;
                            ActionIdle?.Invoke ();
                            break;
                        }
                        break;
                    }
                case "android.net.conn.CONNECTIVITY_CHANGE":
                    if (intent.Extras != null) {
                        var ni = (NetworkInfo)intent.Extras.Get (ConnectivityManager.ExtraNetworkInfo);
                        if (ni != null && ni.GetState () == NetworkInfo.State.Connected) {
                            //可以再这里进行一些网络突然断开未完成的操作 
                            System.Console.WriteLine ($"网络变化:{ni.GetState ()}");
                            var type = getConnectedType (context);
                            if (type == 2)
                            {
                                Application.IsWifi = true;
                            }
                            else {
                                Application.IsWifi = false;
                            }
                            BaseActivity.NetworkStateChanged?.Invoke (type);
                        }
                    }
                    break;
                }
        }
 
        int getConnectedType (Context context)
        {
            if (context != null) {
                var mConnectivityManager = (ConnectivityManager)context.GetSystemService (Context.ConnectivityService);
                if (mConnectivityManager == null) {
                    return 0;
                }
                var mNetworkInfo = mConnectivityManager.ActiveNetworkInfo;
                if (mNetworkInfo != null && mNetworkInfo.IsAvailable) {
                    switch (mNetworkInfo.Type) {
                    case ConnectivityType.Mobile:
                        return 1;
                    case ConnectivityType.Wifi:
                        return 2;
                    }
                }
            }
            return 0;
        }
 
    }
}