• 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;
                        }
    
    
                    }
                }
            }
    
        }
    
    
    
    }
  • 相关阅读:
    linux下socket编程-TCP
    python正则表达式练习篇2
    python正则表达式练习篇
    python正则表达式基础篇
    Jmeter应用初步介绍
    数据库清除重复数据
    Nginx 获取真实 IP 方案
    Mac 实用工具bash-comletion介绍安装
    MySQL的binlog数据如何查看
    Mybatlis SQL 注入与防范
  • 原文地址:https://www.cnblogs.com/Qbit/p/Xamarin_Notification.html
Copyright © 2020-2023  润新知