Xamarin.Android调用webview、处理json同时发送系统通知的代码片段-前端撸码笔记

Xamarin.Android调用webview、处理json同时发送系统通知的代码片段

本段Xamarin.Android代码包含获取json、解析json、调用webview并且包含发送系统通知功能。没啥特别意义,仅供备忘用~using内容有些乱,有些是没用的,有兴趣的同学可以精简。本段代码主要是从当麻许的教程以及博客园博主y-z-f相关内容整理而来。

using Android.App;
using Android.Widget;
using Android.OS;
using System;
using System.IO;
using Newtonsoft.Json;
using System.Threading.Tasks;
using System.Threading;
using Android.Media;
using Android.Content;
using Android.Views;
using Android.Webkit;
using System.Net;
namespace SampleForWebClient
{
[Activity(Label = “残冰的代码笔记”, MainLauncher = true, Icon = “@drawable/Icon”)]

public class Activity1 : Activity
{
WebView webviewMain;
private NotificationManager nMgr; //调用系统提示的
protected override void OnCreate(Bundle bundle)
{

base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
webviewMain = FindViewById<WebView>(Resource.Id.webviewMain);
//啟用Javascript Enable
webviewMain.Settings.JavaScriptEnabled = true;
webviewMain.SetWebViewClient(new CustWebViewClient());
webviewMain.SetWebChromeClient(new CustChromeWebViewClient(this));
//載入網址
webviewMain.LoadUrl(“file:///android_asset/index.php”);
// 請注意這行,如果不加入巢狀Class 會必成呼叫系統讓系統來裁決開啟http 的方式

btnGetData1();

}

 

private void btnGetData1()
{
var webClient = new System.Net.WebClient();
var result = webClient.DownloadString(“http://www.eshefang.com/json.php”);
//透過JSON.net 反序列化為User物件
var user = JsonConvert.DeserializeObject<User>(result);

Toast.MakeText(this, user.name, ToastLength.Long).Show();
nMgr = (NotificationManager)GetSystemService(NotificationService);//调用系统提示的
Notification notify = new Notification(Resource.Drawable.Icon, “带有声音、LED光和震动的通知”);
//设置该通知具有声音、LED光和震动
notify.Defaults = NotificationDefaults.All;

//获取系统默认的通知声音
Android.Net.Uri ringUri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
//设置通知的声音
notify.Sound = ringUri;

//设置一秒的震动
notify.Vibrate = new long[] { 1000 };
PendingIntent pintent = PendingIntent.GetActivity(this, 0, new Intent(this, typeof(Activity1)), 0);

notify.SetLatestEventInfo(this, “E奢坊:”, “您有新的订单”, pintent);

nMgr.Notify(1, notify);

}
//void btnGetData1_Click(object sender, System.EventArgs e)
void btnGetData1_Click(object sender)
{
var webClient = new System.Net.WebClient();
var result = webClient.DownloadString(“http://www.eshefang.com/json.php”);
//透过JSON.net 反序列化为User对象
var user = JsonConvert.DeserializeObject<User>(result);
//印出 id and name
Toast.MakeText(this, user.id + “:” + user.name, ToastLength.Long).Show();
}

 

 

public class CustChromeWebViewClient : WebChromeClient
{
private Context _context;

public CustChromeWebViewClient(Context father)
{
_context = father;

}
public override bool OnJsAlert(WebView view, string url, string message, JsResult result)
{

new AlertDialog.Builder(_context).SetTitle(“E奢坊发来通知:”).SetMessage(message).SetPositiveButton(“确定”, delegate
{
result.Confirm();
}).Create().Show();
return true;

}

}
public override bool OnKeyDown(Android.Views.Keycode keyCode, Android.Views.KeyEvent e)
{
if (keyCode == Keycode.Back && webviewMain.CanGoBack())
{
webviewMain.GoBack();
return true;
}
return base.OnKeyDown(keyCode, e);
}

/// <summary>
/// 巢狀Class 繼承WebViewClient
/// </summary>
private class CustWebViewClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading(WebView view, string url)
{
view.LoadUrl(url);
return true;
}

}
}
}

未经允许不得转载:前端撸码笔记 » Xamarin.Android调用webview、处理json同时发送系统通知的代码片段

上一篇:

下一篇: