using System;
|
using Android.App;
|
using Android.Content;
|
using Android.Database;
|
using Android.Media;
|
using Android.Net.Wifi;
|
using Android.OS;
|
using Android.Util;
|
using Android.Widget;
|
using Java.Util;
|
using Android.Content.PM;
|
using Android.Locations;
|
using Android.Runtime;
|
using Android.Views;
|
using Com.Hdl.ON;
|
//using ICSharpCode.SharpZipLib.Zip;
|
using Android.Net;
|
using Android;
|
using System.Collections.Generic;
|
using System.Xml;
|
using Org.XmlPull.V1;
|
using Android.Support.V4.App;
|
using Android.Provider;
|
|
namespace Shared
|
{
|
//[Activity(MainLauncher =true)]
|
//public class BaseActivity1 : Android.App.Activity
|
//{
|
// public static Action<BaseActivity1, Android.App.Application> OnCreateActoin;
|
// protected override void OnCreate (Android.OS.Bundle savedInstanceState)
|
// {
|
// base.OnCreate (savedInstanceState);
|
// var mainFrameLayout = new Android.Widget.FrameLayout (this) { KeepScreenOn = true };
|
// SetContentView (mainFrameLayout);
|
// if (OnCreateActoin != null) {
|
// OnCreateActoin (this, this.Application);
|
// }
|
// }
|
|
|
//}
|
//新的添加,可以解决切换语言后登陆问题。ConfigurationChanges= ConfigChanges.ScreenSize|ConfigChanges.Orientation| ConfigChanges.Keyboard| ConfigChanges.KeyboardHidden| ConfigChanges.Locale,
|
[Activity (MainLauncher = true, WindowSoftInputMode = Android.Views.SoftInput.AdjustResize, LaunchMode = LaunchMode.SingleInstance, ConfigurationChanges = ConfigChanges.LayoutDirection | ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.Keyboard | ConfigChanges.KeyboardHidden | ConfigChanges.Locale, Theme = "@style/MyTheme", ScreenOrientation = ScreenOrientation.Portrait)]
|
public class BaseActivity : Android.App.Activity, ICommon, ViewTreeObserver.IOnGlobalLayoutListener
|
{
|
enum RequestCode
|
{
|
ActionLocationSourceSettings = 100,
|
}
|
WifiManager.MulticastLock multicasetLock;
|
public static Action<BaseActivity> MonitorInfoChangedAction;
|
public static Action<BaseActivity, Android.App.Application> OnCreateActoin;
|
public static Action<BaseActivity> RefreshUIAction;
|
public static Action<BaseActivity> OnResumeAction;
|
/// <summary>
|
/// 网络状态变化
|
/// </summary>
|
public static Action<int> NetworkStateChanged;
|
/// <summary>
|
/// Activity 销掉时事件
|
/// </summary>
|
public static Action<BaseActivity> OnDestroyAction;
|
Android.Widget.FrameLayout mainFrameLayout;
|
DisplayMetrics metric = new DisplayMetrics ();
|
/// <summary>
|
/// Y轴坐标
|
/// </summary>
|
public static int TopMargin;
|
public static bool IsHideVirualButtons;
|
protected override void OnCreate (Android.OS.Bundle savedInstanceState)
|
{
|
try {
|
Shared.HDLUtils.WriteLine($"Activity 线程ID{System.Threading.Thread.CurrentThread.ManagedThreadId}");
|
if (IsHideVirualButtons) {
|
MyJar.Common.HideVirtualButtons (this);
|
}
|
Shared.Application.Activity = this;
|
if (Shared.Application.IsPad) {
|
this.RequestedOrientation = Android.Content.PM.ScreenOrientation.Landscape;
|
}
|
base.OnCreate (savedInstanceState);
|
|
if (IsHideVirualButtons) {
|
WindowManager.DefaultDisplay.GetRealMetrics (metric);
|
} else {
|
WindowManager.DefaultDisplay.GetMetrics (metric);
|
}
|
//获取设备的宽、高度
|
Shared.Application.CurrentWidth = metric.WidthPixels; //宽度(PX)
|
//考虑到5.7寸屏往上偏移,加高父控件的高度
|
Shared.Application.CurrentHeight = metric.HeightPixels - TopMargin; //高度(PX
|
|
//父控件
|
var contentView = new Android.Widget.FrameLayout (this) ;
|
mainFrameLayout = new Android.Widget.FrameLayout(this) { KeepScreenOn = KeepScreenON } ;
|
contentView.AddView (mainFrameLayout, new Android.Widget.FrameLayout.LayoutParams (Shared.Application.CurrentWidth, Shared.Application.CurrentHeight) { TopMargin = TopMargin });
|
SetContentView(contentView);
|
//界面变化实现的接口
|
Window.DecorView.ViewTreeObserver.AddOnGlobalLayoutListener (this);
|
|
multicasetLock = WifiManager.FromContext (this).CreateMulticastLock ("WIFI");
|
multicasetLock.Acquire ();
|
initLanguage ();
|
|
|
//启动本地音乐的后台服务
|
StartService (new Intent (this, typeof (Other.MusicService)));
|
SettingsContentObserver.RegisterVolumeChangeReceiver ();
|
//注册广播
|
registerReceiver ();
|
com.hdl.on.Volume.Init ();
|
OnCreateActoin?.Invoke (this, this.Application);
|
|
//启动GPS后台服务
|
StartGPSService();
|
//StartService(new Intent(this, typeof(GPSLocationService)));
|
|
} catch { }
|
}
|
public static bool KeepScreenON = true;
|
|
public void StartGPSService()
|
{
|
try
|
{
|
StartService(new Intent(this, typeof(GPSLocationService)));
|
}
|
catch { }
|
}
|
|
public void StopGpsService()
|
{
|
try
|
{
|
StopService(new Intent(this, typeof(GPSLocationService)));
|
}
|
catch { }
|
}
|
|
|
|
#region 图库的权限
|
Action<bool> imageAction;
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="action">Action.</param>
|
public void SetImagePermission(Action<bool> action)
|
{
|
imageAction = action;
|
//如果还没有权限,得请求权限
|
if (BuildVersionCodes.M <= Build.VERSION.SdkInt && (CheckSelfPermission(Manifest.Permission.WriteExternalStorage) == Permission.Denied || CheckSelfPermission(Manifest.Permission.ReadExternalStorage) == Permission.Denied))
|
{
|
RequestPermissions(new string[] { Manifest.Permission.WriteExternalStorage,Manifest.Permission.ReadExternalStorage }, 255);
|
}
|
else
|
{
|
//有权限直接回调出去
|
if (imageAction != null)
|
{
|
imageAction(true);
|
imageAction = null;
|
}
|
}
|
}
|
#endregion
|
#region 摄像头功能
|
Action<bool> cameraAction;
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="action">Action.</param>
|
public void SetCamera(Action<bool> action)
|
{
|
cameraAction = action;
|
//如果还没有权限,得请求权限
|
if (BuildVersionCodes.M <= Build.VERSION.SdkInt && CheckSelfPermission(Manifest.Permission.Camera) == Permission.Denied)
|
{
|
RequestPermissions(new string[] { Manifest.Permission.Camera }, 255);
|
}
|
else
|
{
|
//有权限直接回调出去
|
if (cameraAction != null)
|
{
|
cameraAction(true);
|
cameraAction = null;
|
}
|
}
|
}
|
#endregion
|
#region 获取手机状态信息权限
|
Action<bool> readPhoneStateAction;
|
/// <summary>
|
/// 萤石摄像头那块代码比较特殊,得在调用这个启动他需要的权限
|
/// </summary>
|
/// <param name="action">Action.</param>
|
public void SetPhoneState(Action<bool> action)
|
{
|
readPhoneStateAction = action;
|
//如果还没有权限,得请求权限
|
if (BuildVersionCodes.M <= Build.VERSION.SdkInt && (CheckSelfPermission(Manifest.Permission.ReadPhoneState) == Permission.Denied || CheckSelfPermission(Manifest.Permission.CallPhone) == Permission.Denied || CheckSelfPermission(Manifest.Permission.ReadCallLog) == Permission.Denied || CheckSelfPermission(Manifest.Permission.WriteCallLog) == Permission.Denied || CheckSelfPermission(Manifest.Permission.AddVoicemail) == Permission.Denied || CheckSelfPermission(Manifest.Permission.UseSip) == Permission.Denied || CheckSelfPermission(Manifest.Permission.ProcessOutgoingCalls) == Permission.Denied)) {
|
RequestPermissions(new string[] { Manifest.Permission.ReadPhoneState, Manifest.Permission.CallPhone, Manifest.Permission.ReadCallLog, Manifest.Permission.WriteCallLog, Manifest.Permission.AddVoicemail, Manifest.Permission.UseSip, Manifest.Permission.ProcessOutgoingCalls }, 255);
|
} else {
|
//有权限直接回调出去
|
if (readPhoneStateAction != null) {
|
readPhoneStateAction(true);
|
readPhoneStateAction = null;
|
}
|
}
|
}
|
#endregion
|
#region 读写联系人信息
|
Action<bool> readWriteContactAction;
|
public void ReadWriteContact(Action<bool> action)
|
{
|
readWriteContactAction = action;
|
if (BuildVersionCodes.M <= Android.OS.Build.VERSION.SdkInt && (CheckSelfPermission(Manifest.Permission.ReadContacts) == Permission.Denied || CheckSelfPermission(Manifest.Permission.WriteContacts) == Permission.Denied || CheckSelfPermission(Manifest.Permission.GetAccounts) == Permission.Denied))
|
{
|
RequestPermissions(new string[] { Android.Manifest.Permission.ReadContacts, Android.Manifest.Permission.WriteContacts, Manifest.Permission.GetAccounts }, 255);
|
}
|
else
|
{
|
//有权限直接回调出去
|
readWriteContactAction?.Invoke(true);
|
readWriteContactAction = null;
|
}
|
}
|
#endregion
|
#region 可视对讲的权限
|
Action<bool> audioRecordAction;
|
public void AudioRecord(Action<bool> action)
|
{
|
audioRecordAction = action;
|
if (BuildVersionCodes.M <= Android.OS.Build.VERSION.SdkInt && CheckSelfPermission(Manifest.Permission.RecordAudio) == Permission.Denied)
|
{
|
RequestPermissions(new string[] { Manifest.Permission.RecordAudio}, 255);
|
}
|
else
|
{
|
//有权限直接回调出去
|
if (audioRecordAction != null)
|
{
|
audioRecordAction(true);
|
audioRecordAction = null;
|
}
|
}
|
}
|
#endregion
|
|
|
#region 通用权限请求
|
Dictionary<int, Action<bool>> permissionLists = new Dictionary<int, Action<bool>> { };
|
int maxCode = 10000;
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="action">Action.</param>
|
public void SetPermission(Action<bool> action,string permission)
|
{
|
//如果还没有权限,得请求权限
|
if (CheckSelfPermission(permission) == Permission.Denied)
|
{
|
var code = maxCode++;
|
permissionLists[code] = action;//根据请求的Code来确认请求的权限
|
RequestPermissions(new string[] { permission }, code);
|
}
|
else
|
{
|
//有权限直接回调出去
|
action?.Invoke(true);
|
}
|
}
|
#endregion
|
#region GPS的权限
|
Action<bool> gpsLocationAction;
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="action">Action.</param>
|
public void SetGPSLocationPermission(Action<bool> action)
|
{
|
gpsLocationAction = action;
|
//如果还没有权限,得请求权限
|
if (BuildVersionCodes.M <= Build.VERSION.SdkInt && (CheckSelfPermission(Manifest.Permission.AccessFineLocation) == Permission.Denied || CheckSelfPermission(Manifest.Permission.AccessCoarseLocation) == Permission.Denied))
|
{
|
RequestPermissions(new string[] { Manifest.Permission.AccessFineLocation, Manifest.Permission.AccessCoarseLocation }, 255);
|
}
|
else
|
{
|
//有权限直接回调出去
|
gpsLocationAction?.Invoke(true);
|
gpsLocationAction = null;
|
|
}
|
}
|
#endregion
|
|
#region 注册广播事件
|
MyBroadcastReceive myBroadcastReceive = new MyBroadcastReceive { };
|
void registerReceiver ()
|
{
|
var intentFilter = new IntentFilter { };
|
//监听电话状态变化
|
intentFilter.AddAction ("android.intent.action.PHONE_STATE");
|
intentFilter.AddAction ("android.intent.action.NEW_OUTGOING_CALL");
|
//监听网络变化
|
intentFilter.AddAction ("android.net.conn.CONNECTIVITY_CHANGE");
|
intentFilter.AddAction ("android.intent.category.DEFAULT");
|
//intentFilter.AddCategory
|
RegisterReceiver (myBroadcastReceive, intentFilter);
|
|
int beforeValue = 10;
|
MyBroadcastReceive.ActionIdle = () => {
|
var audioManager = (AudioManager)GetSystemService (AudioService);
|
int nowValue = (int)(audioManager.GetStreamVolume (Stream.Music) * 100.0f / audioManager.GetStreamMaxVolume (Stream.Music));
|
if (com.hdl.on.Volume.VolumeChange != null && beforeValue != nowValue) {
|
com.hdl.on.Volume.VolumeChange (beforeValue);
|
}
|
};
|
|
MyBroadcastReceive.ActioRinging = () => {
|
var audioManager = (AudioManager)GetSystemService (AudioService);
|
beforeValue = (int)(audioManager.GetStreamVolume (Stream.Music) * 100.0f / audioManager.GetStreamMaxVolume (Stream.Music));
|
};
|
}
|
#endregion
|
Action<bool> contactsAction;
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="action">Action.</param>
|
public void SetContactsPermission(Action<bool> action)
|
{
|
contactsAction = action;
|
//如果还没有权限,得请求权限
|
if (BuildVersionCodes.M <= Build.VERSION.SdkInt && CheckSelfPermission(Manifest.Permission.ReadContacts) == Permission.Denied)
|
{
|
RequestPermissions(new string[] { Manifest.Permission.ReadContacts }, 254);
|
}
|
else
|
{
|
//有权限直接回调出去
|
contactsAction?.Invoke(true);
|
contactsAction = null;
|
}
|
}
|
#region 获取通讯录
|
//**版本判断。当手机系统大于 23 时,才有必要去判断权限是否获取**
|
|
#endregion
|
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
|
{
|
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
|
//根据Code来确认权限
|
if (permissionLists.ContainsKey(requestCode))
|
{
|
var action = permissionLists[requestCode];
|
permissionLists.Remove(requestCode);
|
if (0 < grantResults.Length && grantResults[0] == Permission.Granted)
|
{
|
action?.Invoke(true);
|
}
|
else {
|
action?.Invoke(false);
|
}
|
}
|
//之前的写法,后面可以通过上面的方式处理,然后去掉这块代码
|
for (int i = 0; i < permissions.Length; i++)
|
{
|
switch (permissions[i])
|
{
|
case Manifest.Permission.AccessFineLocation:
|
if (grantResults[i] == Permission.Granted)
|
{
|
//有权限回调出去
|
gpsLocationAction?.Invoke(true);
|
gpsLocationAction = null;
|
}
|
break;
|
case Android.Manifest.Permission.ReadPhoneState:
|
if (readPhoneStateAction != null)
|
{
|
if (grantResults[i] == Permission.Granted)
|
{
|
readPhoneStateAction(true);
|
}
|
else
|
{
|
readPhoneStateAction(false);
|
}
|
readPhoneStateAction = null;
|
}
|
break;
|
case Manifest.Permission.ReadContacts:
|
if (requestCode == 254) {
|
if (grantResults[i] == Permission.Granted)
|
{
|
contactsAction?.Invoke(true);
|
}
|
else
|
{
|
contactsAction?.Invoke(false);
|
}
|
contactsAction = null;
|
}
|
if (requestCode == 255)
|
{
|
if (grantResults[i] == Permission.Granted)
|
{
|
readWriteContactAction?.Invoke(true);
|
}
|
else
|
{
|
readWriteContactAction?.Invoke(false);
|
}
|
readWriteContactAction = null;
|
}
|
|
break;
|
case Manifest.Permission.RecordAudio:
|
if (audioRecordAction != null)
|
{
|
if (grantResults[i] == Permission.Granted)
|
{
|
audioRecordAction(true);
|
}
|
else
|
{
|
audioRecordAction(false);
|
}
|
audioRecordAction = null;
|
}
|
break;
|
case Manifest.Permission.Camera:
|
if (cameraAction != null)
|
{
|
if (grantResults[i] == Permission.Granted)
|
{
|
cameraAction(true);
|
}
|
else
|
{
|
cameraAction(false);
|
}
|
cameraAction = null;
|
}
|
break;
|
case Manifest.Permission.WriteExternalStorage:
|
if (imageAction != null)
|
{
|
if (grantResults[i] == Permission.Granted)
|
{
|
imageAction(true);
|
}
|
else
|
{
|
imageAction(false);
|
}
|
imageAction = null;
|
}
|
break;
|
|
}
|
}
|
}
|
|
|
protected override void OnResume ()
|
{
|
base.OnResume ();
|
OnResumeAction?.Invoke(this);
|
}
|
|
|
public override void OnWindowFocusChanged (bool hasFocus)
|
{
|
base.OnWindowFocusChanged (hasFocus);
|
if (hasFocus && null == Shared.Application.RootFrameLayout) {
|
initImages (() => {
|
mainFrameLayout.Post (() => {
|
Shared.Application.RootFrameLayout = mainFrameLayout;
|
RefreshUIAction?.Invoke(this);
|
});
|
});
|
}
|
}
|
/// <summary>
|
/// 审核最后时间
|
/// </summary>
|
public static DateTime VerifyDateTime = DateTime.Now;
|
public static Action BackKeyAction;
|
public override bool DispatchKeyEvent (Android.Views.KeyEvent e)
|
{
|
//百度认证要求返回键有效果,加这个是为了审核通过
|
//if (DateTime.Now < VerifyDateTime) {
|
// Shared.HDLUtils.WriteLine ("base VerifyDateTime");
|
// VerifyDateTime = DateTime.MinValue;
|
// //return base.DispatchKeyEvent (e);
|
//}
|
if (e.KeyCode == Android.Views.Keycode.Back && e.Action == Android.Views.KeyEventActions.Down) {
|
BackKeyAction?.Invoke ();
|
// Shared.HDLUtils.WriteLine ("BackKeyAction End!!!");
|
// return true;
|
}
|
return base.DispatchKeyEvent (e);
|
}
|
|
public override bool OnKeyDown([GeneratedEnum] Keycode keyCode, KeyEvent e)
|
{
|
if(keyCode== Keycode.Back){
|
//Shared.HDLUtils.WriteLine ("OnKeyDown1");
|
//不允许结束程序,只是退出到后台
|
if (VerifyDateTime == DateTime.MaxValue)
|
{
|
//Shared.HDLUtils.WriteLine ("OnKeyDown2");
|
VerifyDateTime = DateTime.MinValue;
|
MoveTaskToBack (false);
|
//} else {
|
//Shared.HDLUtils.WriteLine ("OnKeyDown3");
|
//BackKeyAction ();
|
//return true;
|
}
|
return true;
|
}
|
return true;
|
//return base.OnKeyDown(keyCode, e);
|
}
|
|
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
|
{
|
base.OnActivityResult(requestCode, resultCode, data);
|
if (requestCode == 0x30)
|
{
|
if (data == null) {
|
Shared.Contacts.ContactAction?.Invoke(null, null);
|
}
|
else
|
{
|
string phoneNum = null;
|
string contactName = null;
|
// 创建内容解析者
|
var contentResolver = ContentResolver;
|
ICursor cursor = null;
|
if (data.Data != null)
|
{
|
cursor = contentResolver.Query(data.Data, new string[] { "display_name", "data1" }, null, null, null);
|
}
|
while (cursor.MoveToNext())
|
{
|
contactName = cursor.GetString(cursor.GetColumnIndex("display_name"));
|
phoneNum = cursor.GetString(cursor.GetColumnIndex("data1"));
|
}
|
cursor.Close();
|
// 把电话号码中的 - 符号 替换成空格
|
if (phoneNum != null)
|
{
|
phoneNum = phoneNum.Replace("-", " ");
|
// 空格去掉 为什么不直接-替换成"" 因为测试的时候发现还是会有空格 只能这么处理
|
phoneNum = phoneNum.Replace(" ", "");
|
}
|
Shared.Contacts.ContactAction?.Invoke(contactName, phoneNum);
|
}
|
}
|
}
|
|
protected override void OnDestroy ()
|
{
|
base.OnDestroy();
|
|
OnDestroyAction?.Invoke(this);
|
//关闭本地音乐提供的服务
|
StopService (new Intent (this, typeof (Other.MusicService)));
|
//关闭GPS获取定位服务
|
StopGpsService();
|
|
if (multicasetLock != null) {
|
multicasetLock.Release ();
|
}
|
//移除手机状态广播
|
UnregisterReceiver (myBroadcastReceive);
|
|
SettingsContentObserver.UnregisterVolumeChangeReceiver ();
|
|
Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
|
|
//Java.Lang.JavaSystem.Exit(0);//正常退出App
|
}
|
|
void initLanguage ()
|
{
|
Locale l = Locale.Default;
|
string language = l.Language;
|
if ("zh" == language) {
|
Language.CurrentLanguage = "Chinese";
|
} else {
|
Language.CurrentLanguage = "English";
|
}
|
}
|
|
void writeFiles (string path)
|
{
|
//创建根目录
|
System.IO.Directory.CreateDirectory (System.IO.Path.Combine (Shared.Application.RootPath, path));
|
var paths = Assets.List (path);
|
foreach (string fileOrFolderPath in paths) {
|
var tempString = fileOrFolderPath.ToLower ();
|
//系统自带的图
|
if (tempString == "license" || tempString == "images" || tempString == "webkit") {
|
continue;
|
}
|
if (fileOrFolderPath.EndsWith (".zip")) {
|
//decomparessFile (fileOrFolderPath, Shared.IO.FileUtils.RootPath);
|
continue;
|
}
|
if (tempString.EndsWith (".txt") || tempString.EndsWith (".png") || tempString.EndsWith (".jpg")) {
|
System.IO.Stream inputStream = null;
|
try {
|
//根目录下如果直接是文件的就不需要复制
|
if (path != "") {
|
inputStream = this.Assets.Open (path + "/" + fileOrFolderPath);
|
//写当前文件
|
IO.FileUtils.WriteFileByInputStream (path + "/" + fileOrFolderPath, inputStream);
|
}
|
|
} catch { } finally {
|
if (inputStream != null) {
|
inputStream.Close ();
|
}
|
}
|
} else {
|
if (path == "") {
|
writeFiles (fileOrFolderPath);
|
} else {
|
writeFiles (path + "/" + fileOrFolderPath);
|
}
|
}
|
}
|
}
|
|
void initImages (Action action)
|
{
|
var version = PackageManager.GetPackageInfo (Application.PackageName, 0).VersionCode;
|
System.Threading.Tasks.Task.Run (() => {
|
try {
|
var SharedPref = GetSharedPreferences ("PrivateData", Android.Content.FileCreationMode.Private);
|
//判断是否已经初始化图片成功,成功了就不在写
|
if (SharedPref.GetInt ("Version", -1) == version) {
|
return;
|
}
|
|
writeFiles ("");
|
|
//全部初始化后所有的信息后就创建这个文件来标记一下
|
var v = SharedPref.Edit ();
|
v.PutInt ("Version", version);
|
v.Apply ();
|
v.Commit ();
|
} catch { } finally {
|
action ();
|
}
|
});
|
}
|
|
|
public static Action<MonitorData> ControlMonitorSceneAction;
|
public void ControlMonitorScene (MonitorData p0)
|
{
|
ControlMonitorSceneAction?.Invoke(p0);
|
}
|
|
public static Action<string, string> MonitorMessagePushAction;
|
public void MonitorMessagePush (string p0, string p1)
|
{
|
MonitorMessagePushAction?.Invoke(p0, p1);
|
}
|
|
public static Action<MonitorData> MonitorSceneDataAction;
|
public void MonitorSceneData (MonitorData p0)
|
{
|
MonitorSceneDataAction?.Invoke(p0);
|
}
|
|
public void OnGlobalLayout()
|
{
|
var rect = new Android.Graphics.Rect();
|
mainFrameLayout.GetWindowVisibleDisplayFrame(rect);
|
|
var softHeight = metric.HeightPixels - (rect.Bottom - rect.Top);
|
|
var tempHeight = EditText.FocusBottom + softHeight - metric.HeightPixels;
|
//if (0 < tempHeight) {
|
// mainFrameLayout.SetY (-tempHeight - 10);
|
//} else {
|
// mainFrameLayout.SetY (0);
|
//}
|
if (0 < tempHeight)
|
{
|
mainFrameLayout.SetY(TopMargin - tempHeight - 10);
|
}
|
else
|
{
|
mainFrameLayout.SetY(TopMargin);
|
}
|
|
//Shared.HDLUtils.WriteLine($"屏幕高度{mainFrameLayout.Height} 输入法高度{softHeight}");
|
//当前已经是隐藏状态了
|
//if(softHeight<=108){
|
if (IsHideVirualButtons)
|
{
|
MyJar.Common.HideVirtualButtons(this);
|
}
|
//}
|
}
|
|
public void MoveToFront() {
|
SystemHelper.SetTopApp(this.BaseContext);
|
}
|
|
///// <summary>
|
///// 解压文件到指定文件夹
|
///// </summary>
|
///// <param name="sourceFile">压缩文件</param>
|
///// <param name="destinationDirectory">目标文件夹,如果为空则解压到当前文件夹下</param>
|
///// <param name="password">密码</param>
|
///// <returns></returns>
|
//public bool decomparessFile (string filePath, string destinationDirectory, string password = null)
|
//{
|
// bool result = false;
|
// var bufferSize = 1024;
|
|
// try {
|
// if (!System.IO.Directory.Exists (destinationDirectory)) {
|
// System.IO.Directory.CreateDirectory (destinationDirectory);
|
// }
|
|
// using (var zipStream = new ZipInputStream (Assets.Open (filePath))) {
|
// zipStream.Password = password;
|
// var zipEntry = zipStream.GetNextEntry ();
|
|
// while (zipEntry != null) {
|
// if (zipEntry.IsDirectory)//如果是文件夹则创建
|
// {
|
// System.IO.Directory.CreateDirectory (System.IO.Path.Combine (destinationDirectory,
|
// System.IO.Path.GetDirectoryName (zipEntry.Name)));
|
// } else {
|
// string fileName = System.IO.Path.GetFileName (zipEntry.Name);
|
// if (!string.IsNullOrEmpty (fileName) && fileName.Trim ().Length > 0) {
|
// var fileItem = new System.IO.FileInfo (System.IO.Path.Combine (destinationDirectory, zipEntry.Name));
|
// using (var writeStream = fileItem.Create ()) {
|
// var buffer = new byte [bufferSize];
|
// int readLength = 0;
|
|
// do {
|
// readLength = zipStream.Read (buffer, 0, bufferSize);
|
// writeStream.Write (buffer, 0, readLength);
|
// } while (readLength == bufferSize);
|
|
// writeStream.Flush ();
|
// writeStream.Close ();
|
// }
|
// fileItem.LastWriteTime = zipEntry.DateTime;
|
// }
|
// }
|
// zipEntry = zipStream.GetNextEntry ();//获取下一个文件
|
// }
|
// zipStream.Close ();
|
// }
|
// result = true;
|
// } catch (System.Exception ex) {
|
// Shared.HDLUtils.WriteLine ($"文件解压发生错误:{ex}");
|
// }
|
|
// return result;
|
//}
|
}
|
|
/// <summary>
|
/// 监听音量的变化
|
/// </summary>
|
class SettingsContentObserver : ContentObserver
|
{
|
static SettingsContentObserver settingsContentObserver;
|
static Context context;
|
public SettingsContentObserver(Context c, Handler handler) : base(handler)
|
{
|
context = c;
|
}
|
public override void OnChange(bool selfChange)
|
{
|
base.OnChange(selfChange);
|
if(MyBroadcastReceive.IsCalling){
|
return;
|
}
|
var audioManager = (AudioManager)context.GetSystemService(Context.AudioService);
|
float currentVolume = audioManager.GetStreamVolume(Stream.Music) * 100.0f / audioManager.GetStreamMaxVolume(Stream.Music);
|
if (com.hdl.on.Volume.VolumeChange != null)
|
{
|
Shared.HDLUtils.WriteLine ($"媒体音量变化Volume:{currentVolume}");
|
com.hdl.on.Volume.VolumeChange((int)currentVolume);
|
}
|
}
|
|
public static void RegisterVolumeChangeReceiver()
|
{
|
settingsContentObserver = new SettingsContentObserver(Shared.Application.Activity, new Handler());
|
context.ContentResolver.RegisterContentObserver(Android.Provider.Settings.System.ContentUri, true, settingsContentObserver);
|
}
|
public static void UnregisterVolumeChangeReceiver()
|
{
|
context.ContentResolver.UnregisterContentObserver(settingsContentObserver);
|
}
|
}
|
|
internal static class SystemHelper
|
{
|
/**
|
* 判断本地是否已经安装好了指定的应用程序包
|
*
|
* @param packageNameTarget :待判断的 App 包名,如 微博 com.sina.weibo
|
* @return 已安装时返回 true,不存在时返回 false
|
*/
|
public static bool AppIsExist(Context context, string packageNameTarget)
|
{
|
return true;
|
//if ("" != packageNameTarget.Trim())
|
//{
|
// PackageManager packageManager = context.PackageManager;
|
// var packageInfoList = packageManager.GetInstalledPackages(PackageInfoFlags.MatchUninstalledPackages);
|
// foreach (PackageInfo packageInfo in packageInfoList)
|
// {
|
// var packageNameSource = packageInfo.PackageName;
|
// if (packageNameSource == (packageNameTarget))
|
// {
|
// return true;
|
// }
|
// }
|
//}
|
//return false;
|
}
|
|
/**
|
* 将本应用置顶到最前端
|
* 当本应用位于后台时,则将它切换到最前端
|
*
|
* @param context
|
*/
|
public static void SetTopApp(Context context)
|
{
|
if (!isRunningForeground(context))
|
{
|
/**获取ActivityManager*/
|
var activityManager = (ActivityManager)context.GetSystemService(Activity.ActivityService);
|
|
/**获得当前运行的task(任务)*/
|
var taskInfoList = activityManager.GetRunningTasks(100);
|
foreach (ActivityManager.RunningTaskInfo taskInfo in taskInfoList)
|
{
|
/**找到本应用的 task,并将它切换到前台*/
|
if (taskInfo.TopActivity.PackageName == (context.PackageName))
|
{
|
activityManager.MoveTaskToFront(taskInfo.Id, 0);
|
break;
|
}
|
}
|
}
|
}
|
|
/**
|
* 判断本应用是否已经位于最前端
|
*
|
* @param context
|
* @return 本应用已经位于最前端时,返回 true;否则返回 false
|
*/
|
static bool isRunningForeground(Context context)
|
{
|
var activityManager = (ActivityManager)context.GetSystemService(Activity.ActivityService);
|
var appProcessInfoList = activityManager.RunningAppProcesses;
|
/**枚举进程*/
|
foreach (ActivityManager.RunningAppProcessInfo appProcessInfo in appProcessInfoList)
|
{
|
if (appProcessInfo.Importance == ActivityManager.RunningAppProcessInfo.ImportanceForeground)
|
{
|
if (appProcessInfo.ProcessName == (context.ApplicationInfo.ProcessName))
|
{
|
return true;
|
}
|
}
|
}
|
return false;
|
}
|
}
|
}
|