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); } } }