添加项目文件。
This commit is contained in:
121
XXCpzs.Android/Services/CommonService.cs
Normal file
121
XXCpzs.Android/Services/CommonService.cs
Normal file
@@ -0,0 +1,121 @@
|
||||
using Android.App;
|
||||
using Android.Content;
|
||||
using Android.Content.PM;
|
||||
using Android.Content.Res;
|
||||
using Android.Media;
|
||||
using Android.OS;
|
||||
using System;
|
||||
using Xamarin.Forms;
|
||||
using XXCpzs.Droid.Services;
|
||||
using XXCpzs.Services;
|
||||
|
||||
[assembly: Dependency(typeof(CommonService))]
|
||||
namespace XXCpzs.Droid.Services
|
||||
{
|
||||
public class CommonService : ICommonService
|
||||
{
|
||||
public string GetVersion()
|
||||
{
|
||||
string version = "1.00";
|
||||
try
|
||||
{
|
||||
PackageManager packageManager = MainActivity.Instance.PackageManager;
|
||||
// getPackageName()是你当前类的包名,0代表是获取版本信息
|
||||
PackageInfo packInfo = packageManager.GetPackageInfo(MainActivity.Instance.PackageName, 0);
|
||||
|
||||
version = packInfo.VersionName;
|
||||
}
|
||||
catch (PackageManager.NameNotFoundException e)
|
||||
{
|
||||
// e.printStackTrace();
|
||||
}
|
||||
return version;
|
||||
}
|
||||
public void CloseApp()
|
||||
{
|
||||
//App.Current.Quit();
|
||||
Process.KillProcess(Process.MyPid());
|
||||
}
|
||||
public void UpdateApp(string DownloadUrl)
|
||||
{
|
||||
|
||||
var thread = new System.Threading.Thread(new System.Threading.ThreadStart(() =>
|
||||
{
|
||||
|
||||
//更新方式1
|
||||
FileDownload.DownloadUpdateFile(MainActivity.Instance, DownloadUrl);
|
||||
//更新方式2
|
||||
//DownloadUpdateFile2(context, upadtePackageAddress);
|
||||
}))
|
||||
{
|
||||
IsBackground = true
|
||||
};
|
||||
thread.Start();
|
||||
|
||||
}
|
||||
// Unique ID for our notification:
|
||||
static readonly int NOTIFICATION_ID = 1000;
|
||||
static readonly string CHANNEL_ID = "location_notification";
|
||||
internal static readonly string COUNT_KEY = "count";
|
||||
|
||||
// Number of times the button is tapped (starts with first tap):
|
||||
int count = 1;
|
||||
public void SendNotity(string title, string content)
|
||||
{
|
||||
// 将当前按钮按下计数值传递给下一个活动:
|
||||
var valuesForActivity = new Bundle();
|
||||
valuesForActivity.PutInt(COUNT_KEY, count);
|
||||
|
||||
//当用户单击通知时,将启动第二个活动。
|
||||
var resultIntent = new Intent(MainActivity.mContext, typeof(MainActivity));
|
||||
|
||||
// 将一些值传递给 Activity
|
||||
resultIntent.PutExtras(valuesForActivity);
|
||||
|
||||
// 构建跨任务导航的后堆栈:
|
||||
var stackBuilder = TaskStackBuilder.Create(MainActivity.mContext);
|
||||
stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(MainActivity)));
|
||||
stackBuilder.AddNextIntent(resultIntent);
|
||||
|
||||
var resultPendingIntent = stackBuilder.GetPendingIntent(0, PendingIntentFlags.UpdateCurrent);
|
||||
|
||||
// 使用后堆栈创建PendingEntent
|
||||
var builder = new Android.Support.V4.App.NotificationCompat.Builder(MainActivity.mContext, CHANNEL_ID)
|
||||
.SetAutoCancel(true) // 当用户单击通知时,从通知区域中取消通知
|
||||
.SetContentIntent(resultPendingIntent) // 当用户单击意图时启动此活动。
|
||||
.SetContentTitle("Button Clicked") // 设置标题
|
||||
.SetNumber(count) //在内容信息中显示计数
|
||||
.SetSmallIcon(Resource.Drawable.ico) // 这是要显示的图标
|
||||
.SetContentText($"The button has been clicked {count} times."); // 要显示的消息。
|
||||
|
||||
// 最后,发布通知
|
||||
var notificationManager = Android.Support.V4.App.NotificationManagerCompat.From(MainActivity.mContext);
|
||||
notificationManager.Notify(NOTIFICATION_ID, builder.Build());
|
||||
|
||||
// 增加按钮按下次数
|
||||
count++;
|
||||
}
|
||||
|
||||
void CreateNotificationChannel()
|
||||
{
|
||||
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
|
||||
{
|
||||
//通知通道在API 26中是新的(不是 支持库)。不需要创建通知
|
||||
|
||||
//Android旧版本上的频道。
|
||||
return;
|
||||
}
|
||||
|
||||
//var name = Resources.GetString(Resource.String.channel_name);
|
||||
//var description =MainActivity.Instance. GetString("The count from MainActivity.");
|
||||
var channel = new NotificationChannel(CHANNEL_ID, "Local Notifications", NotificationImportance.Default)
|
||||
{
|
||||
Description = "The count from MainActivity."
|
||||
};
|
||||
var notificationManager = (NotificationManager)MainActivity.mContext. GetSystemService(Context. NotificationService);
|
||||
notificationManager.CreateNotificationChannel(channel);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
171
XXCpzs.Android/Services/NetworkConnection.cs
Normal file
171
XXCpzs.Android/Services/NetworkConnection.cs
Normal file
@@ -0,0 +1,171 @@
|
||||
using Android.Content.PM;
|
||||
using Android.OS;
|
||||
using System;
|
||||
using Xamarin.Forms;
|
||||
using XXCpzs.Droid.Services;
|
||||
using XXCpzs.Services;
|
||||
using XXCpzs.Droid;
|
||||
using Android.Net;
|
||||
using Android.Locations;
|
||||
using Android.Content;
|
||||
using Android.Telephony;
|
||||
using Xamarin.Essentials;
|
||||
|
||||
[assembly: Dependency(typeof(NetworkConnection))] //的作用是将该类依赖于程序集,以便在可移植平台底下实例化接口。
|
||||
|
||||
namespace XXCpzs.Droid
|
||||
{
|
||||
public class NetworkConnection : INetworkConnection
|
||||
{
|
||||
//没有网络
|
||||
public const int NETWORKTYPE_INVALID = 0;
|
||||
//wap网络
|
||||
public const int NETWORKTYPE_WAP = 1;
|
||||
//2G网络
|
||||
public const int NETWORKTYPE_2G = 2;
|
||||
//3G和3G以上网络,或统称为快速网络
|
||||
public const int NETWORKTYPE_3G = 3;
|
||||
//wifi网络
|
||||
public const int NETWORKTYPE_WIFI = 4;
|
||||
public bool IsConnected { get; set; }
|
||||
//判断是否有网络连接
|
||||
public void CheckNetworkConnection()
|
||||
{
|
||||
var current = Connectivity.NetworkAccess;
|
||||
|
||||
if (current == NetworkAccess.Internet)
|
||||
{
|
||||
IsConnected = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
IsConnected = false;
|
||||
}
|
||||
}
|
||||
//检查启用了网络位置提供商报告
|
||||
public bool isNetLocEnabled()
|
||||
{
|
||||
LocationManager locMgr = MainActivity.mContext.GetSystemService(Context.LocationService) as LocationManager;
|
||||
return locMgr.IsProviderEnabled(LocationManager.NetworkProvider);
|
||||
}
|
||||
//判断是否有WIFI连接
|
||||
public bool isWifiConnected(Context context)
|
||||
{
|
||||
if (context != null)
|
||||
{
|
||||
ConnectivityManager mConnectivityManager = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);
|
||||
NetworkInfo mWiFiNetworkInfo = mConnectivityManager.GetNetworkInfo(ConnectivityType.Wifi);
|
||||
//NetworkInfo mWiFiNetworkInfo = mConnectivityManager.GetNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
||||
if (mWiFiNetworkInfo != null)
|
||||
{
|
||||
return mWiFiNetworkInfo.IsAvailable;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
//判断Mobile网络是否可用
|
||||
public bool isMobileConnected(Context context)
|
||||
{
|
||||
if (context != null)
|
||||
{
|
||||
ConnectivityManager mConnectivityManager = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);
|
||||
NetworkInfo mMobileNetworkInfo = mConnectivityManager.GetNetworkInfo(ConnectivityType.Mobile);
|
||||
//NetworkInfo mMobileNetworkInfo = mConnectivityManager.GetNetworkInfo(ConnectivityManager.TYPE_MOBILE);
|
||||
if (mMobileNetworkInfo != null)
|
||||
{
|
||||
return mMobileNetworkInfo.IsAvailable;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
//检查飞行模式 - 我们要关闭飞机模式
|
||||
//public bool confirmAirplaneModeOff(Context context)
|
||||
//{
|
||||
//int airplaneSetting = Settings.System.GetInt(context.ContentResolver, Settings.System.AirplaneModeOn, 0);
|
||||
//int airplaneSetting = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0);
|
||||
//return airplaneSetting == 0;
|
||||
//}
|
||||
//确定网络位置提供程序是否真的可用
|
||||
// public bool isNetlocUsable(Context context)
|
||||
// {
|
||||
// return isNetLocEnabled(context) && confirmAirplaneModeOff(context) && isWifiConnected(context) && isMobileConnected(context);
|
||||
// }
|
||||
//判断是否是FastMobileNetWork,将3G或者3G以上的网络称为快速网络
|
||||
private static bool isFastMobileNetwork(Context context)
|
||||
{
|
||||
TelephonyManager telephonyManager = (TelephonyManager)context.GetSystemService(Context.TelephonyService);
|
||||
switch (telephonyManager.NetworkType)
|
||||
{
|
||||
case NetworkType.OneXrtt:
|
||||
return false; // ~ 50-100 kbps
|
||||
case NetworkType.Cdma:
|
||||
return false; // ~ 14-64 kbps
|
||||
case NetworkType.Edge:
|
||||
return false; // ~ 50-100 kbps
|
||||
case NetworkType.Evdo0:
|
||||
return true; // ~ 400-1000 kbps
|
||||
case NetworkType.EvdoA:
|
||||
return true; // ~ 600-1400 kbps
|
||||
case NetworkType.Gprs:
|
||||
return false; // ~ 100 kbps
|
||||
case NetworkType.Hsdpa:
|
||||
return true; // ~ 2-14 Mbps
|
||||
case NetworkType.Hspa:
|
||||
return true; // ~ 700-1700 kbps
|
||||
case NetworkType.Hsupa:
|
||||
return true; // ~ 1-23 Mbps
|
||||
case NetworkType.Umts:
|
||||
return true; // ~ 400-7000 kbps
|
||||
case NetworkType.Ehrpd:
|
||||
return true; // ~ 1-2 Mbps
|
||||
case NetworkType.EvdoB:
|
||||
return true; // ~ 5 Mbps
|
||||
case NetworkType.Hspap:
|
||||
return true; // ~ 10-20 Mbps
|
||||
case NetworkType.Iden:
|
||||
return false; // ~25 kbps
|
||||
case NetworkType.Lte:
|
||||
return true; // ~ 10+ Mbps
|
||||
case NetworkType.Unknown:
|
||||
return false;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//获取网络状态,wifi,wap,2g,3g.
|
||||
/// <summary>
|
||||
/// context 上下文
|
||||
/// return int 网络状态 {@link #NETWORKTYPE_2G},{@link #NETWORKTYPE_3G}
|
||||
/// {@link #NETWORKTYPE_INVALID},{@link #NETWORKTYPE_WAP}* <p>{@link #NETWORKTYPE_WIFI}
|
||||
/// </summary>
|
||||
public static int getNetWorkType(Context context)
|
||||
{
|
||||
int mNetWorkType = 0;
|
||||
ConnectivityManager manager = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);
|
||||
NetworkInfo networkInfo = manager.ActiveNetworkInfo;
|
||||
|
||||
|
||||
if (networkInfo != null && networkInfo.IsConnected)
|
||||
{
|
||||
String type = networkInfo.TypeName;
|
||||
|
||||
|
||||
if (type.Equals("WIFI".ToUpper()))
|
||||
{
|
||||
mNetWorkType = NETWORKTYPE_WIFI;
|
||||
}
|
||||
else if (type.Equals("MOBILE".ToUpper()))
|
||||
{
|
||||
String proxyHost = Proxy.DefaultHost;
|
||||
//String proxyHost = android.net.Proxy.getDefaultHost();
|
||||
//mNetWorkType = TextUtils.IsEmpty(proxyHost) ? (isFastMobileNetwork(context) ? NETWORKTYPE_3G : NETWORKTYPE_2G) : NETWORKTYPE_WAP;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
mNetWorkType = NETWORKTYPE_INVALID;
|
||||
}
|
||||
return mNetWorkType;
|
||||
}
|
||||
}
|
||||
}
|
||||
18
XXCpzs.Android/Services/NotifyService.cs
Normal file
18
XXCpzs.Android/Services/NotifyService.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
using Android.App;
|
||||
using Android.Content;
|
||||
using Android.OS;
|
||||
using Android.Runtime;
|
||||
using Android.Views;
|
||||
using Android.Widget;
|
||||
|
||||
namespace XXCpzs.Droid.Services
|
||||
{
|
||||
class NotifyService
|
||||
{
|
||||
}
|
||||
}
|
||||
83
XXCpzs.Android/Services/TextToSpeechService.cs
Normal file
83
XXCpzs.Android/Services/TextToSpeechService.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Android.Speech.Tts;
|
||||
using XXCpzs.Services;
|
||||
|
||||
namespace XXCpzs.Droid.Services
|
||||
{
|
||||
public class TextToSpeechService : Java.Lang.Object, ITextToSpeechService, TextToSpeech.IOnInitListener,
|
||||
#pragma warning disable CS0618
|
||||
TextToSpeech.IOnUtteranceCompletedListener
|
||||
#pragma warning restore CS0618
|
||||
{
|
||||
TextToSpeech speaker;
|
||||
TaskCompletionSource<bool> tcsInitialize;
|
||||
TaskCompletionSource<bool> tcsUtterance;
|
||||
|
||||
Task<bool> Initialize()
|
||||
{
|
||||
if (tcsInitialize != null)
|
||||
return tcsInitialize.Task;
|
||||
|
||||
tcsInitialize = new TaskCompletionSource<bool>();
|
||||
try
|
||||
{
|
||||
speaker = new TextToSpeech(MainActivity.Instance, this);
|
||||
#pragma warning disable CS0618
|
||||
speaker.SetOnUtteranceCompletedListener(this);
|
||||
#pragma warning restore CS0618
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
tcsInitialize.TrySetException(ex);
|
||||
}
|
||||
return tcsInitialize.Task;
|
||||
}
|
||||
|
||||
public async Task SpeakAsync(string text)
|
||||
{
|
||||
await Initialize();
|
||||
|
||||
if (tcsUtterance?.Task != null)
|
||||
await tcsUtterance.Task;
|
||||
|
||||
tcsUtterance = new TaskCompletionSource<bool>();
|
||||
|
||||
// Set the utterance id so the completed listener fires
|
||||
var parameters = new Dictionary<string, string>();
|
||||
parameters.Add(TextToSpeech.Engine.KeyParamUtteranceId, new Guid().ToString());
|
||||
|
||||
// Use an obsolete overload so it works on older API levels
|
||||
#pragma warning disable CS0618
|
||||
speaker.Speak(text, QueueMode.Flush, parameters);
|
||||
#pragma warning restore CS0618
|
||||
await tcsUtterance.Task;
|
||||
}
|
||||
|
||||
public void OnInit(OperationResult status)
|
||||
{
|
||||
if (status.Equals(OperationResult.Success))
|
||||
tcsInitialize.TrySetResult(true);
|
||||
else
|
||||
tcsInitialize.TrySetException(new ArgumentException("Failed to initialize TTS engine."));
|
||||
}
|
||||
|
||||
public void OnUtteranceCompleted(string utteranceId)
|
||||
{
|
||||
tcsUtterance.TrySetResult(true);
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
speaker?.Stop();
|
||||
speaker?.Shutdown();
|
||||
speaker = null;
|
||||
}
|
||||
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user