zoukankan      html  css  js  c++  java
  • Xamarin 小试牛刀 通知栏消息通知和按钮(基于Java代码人肉转换)

    本示例基于网友现有安卓项目人肉翻译,在Xamarin中替换和修改了很多方法的命名,比如某些属性需要去掉getName的get前缀, 有些方法名称需要使用Pascal命名法替换Java的Camel 命名规范

    另外在内部类的使用方式上也有一些区别,但是整体上来说,大部分的方法名称都与Java 原版Android一致,所以如果有现有的Android 项目需要转换到Xamarin 还是很容易的.此处给Xamarin 66个赞

     参考Java版本:http://blog.csdn.net/wxdjaqgs/article/details/44561101

    博主提供了源码下载

    我所做的只是将需要的图片文件贴过来.然后开始翻译代码

    由于IOS品台的打包流程还没有去研究,所以本次试验没有使用ios 测试.以后有时间补上

    贴代码: 

    using System;
    using Android.App;
    using Android.Content;
    using Android.Widget;
    using Android.OS;
    
    namespace App3
    {
    
        [Activity(Label = "App3", MainLauncher = true, Icon = "@drawable/icon")]
        public class MainActivity : Activity
        {
            public static String ACTION_BTN = "com.example.notification.btn.login";
    
            public static String INTENT_NAME = "btnid";
    
            public const int INTENT_BTN_LOGIN = 1;
    
            static NotificationBroadcastReceiver mReceiver;
    
            public static NotificationManager NotifyManager;
    
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
                // Set our view from the "main" layout resource
                SetContentView(Resource.Layout.Main);
                Button button = (Button)FindViewById(Resource.Id.btn_notification);
                button.Click += (sender, e) => { notification(); };
    
            }
    
    
            private void intiReceiver()
            {
                mReceiver = new NotificationBroadcastReceiver();
                IntentFilter intentFilter = new IntentFilter();
                intentFilter.AddAction(ACTION_BTN);
                ApplicationContext.RegisterReceiver(mReceiver, intentFilter);
            }
    
            public void unregeisterReceiver()
            {
                if (mReceiver != null)
                {
                    ApplicationContext.UnregisterReceiver(mReceiver);
                    mReceiver = null;
                }
            }
    
    
            private void notification()
            {
                unregeisterReceiver();
                intiReceiver();
    
                RemoteViews remoteViews = new RemoteViews(PackageName, Resource.Layout.notification);
                remoteViews.SetTextViewText(Resource.Id.tv_up, "首都机场精品无线");
                remoteViews.SetTextViewText(Resource.Id.tv_down, "已免费接入");
    
                Intent intent = new Intent(ACTION_BTN);
                intent.PutExtra(INTENT_NAME, INTENT_BTN_LOGIN);
                PendingIntent intentpi = PendingIntent.GetBroadcast(this, 1, intent, PendingIntentFlags.UpdateCurrent);
                remoteViews.SetOnClickPendingIntent(Resource.Id.btn_login, intentpi);
    
                Intent intent2 = new Intent();
                intent2.SetClass(this, typeof(MainActivity));
                intent2.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);
                PendingIntent intentContent = PendingIntent.GetActivity(this, 0, intent2, PendingIntentFlags.UpdateCurrent);
    
                Notification.Builder builder = new Notification.Builder(this);
    
                builder.SetOngoing(false);
                builder.SetAutoCancel(false);
                builder.SetContent(remoteViews);
                builder.SetTicker("正在使用首都机场无线");
                builder.SetSmallIcon(Resource.Drawable.id_airport);
    
                Notification notification = builder.Build();
                notification.Defaults = NotificationDefaults.Sound;
                notification.Flags = NotificationFlags.NoClear;
                notification.ContentIntent = intentContent;
                NotifyManager = (NotificationManager)GetSystemService(Context.NotificationService);
    
                NotificationManager notificationManager = NotifyManager;
                notificationManager.Notify(0, notification);
            }
    
    
            class NotificationBroadcastReceiver : BroadcastReceiver
            {
                public override void OnReceive(Context context, Intent intent)
                {
    
                    String action = intent.Action;
                    if (action.Equals(ACTION_BTN))
                    {
                        int btn_id = intent.GetIntExtra(INTENT_NAME, 0);
                        switch (btn_id)
                        {
                            case INTENT_BTN_LOGIN:
                                Toast.MakeText(context, "从通知栏点登录", ToastLength.Short).Show();
    
                                if (mReceiver != null)
                                {
                                    context.ApplicationContext.UnregisterReceiver(mReceiver);
                                    mReceiver = null;
                                }
                                NotificationManager notificationManager = NotifyManager;
                                notificationManager.Cancel(0);
                                break;
                        }
    
    
                    }
                }
            }
    
        }
    
    
    
    }
  • 相关阅读:
    设置屏幕分辨率的函数 回复 "董勇" 的问题
    Delphi 的内存操作函数(5): 复制内存
    汉字与区位码(1) 转换函数
    汉字与多字节编码的转换 回复 "不知道" 的问题
    一个可以显示多边形的 TMyShape 类 回复 "董勇" 的问题
    Delphi 的内存操作函数(6): 跨进程的内存分配
    Delphi 中的 IfThen 函数 回复 "深挖洞、广积粮" 的问题
    Byte 数组转字符串 回复 "不知道" 问题
    汉字与区位码(2) 分析
    获取各种编码的识别符
  • 原文地址:https://www.cnblogs.com/Qbit/p/Xamarin_Notification.html
Copyright © 2011-2022 走看看