Xamarin.Forsm for Android 系统功能
Xamarin.Forsm for Android 系统功能
- 前述
- pm 命令
- Context
- 命令执行
- 检查应用是否安装
- 设置自动开关机
- 开机自启动
前述
初次涉猎Android程序,使用 Xamarin 编写,这里不讨论 Xamarin 的好坏,重点是解决我们的问题。调用系统功能必不可少的便是 adb 命令 和 Android 共享库,来浅试下。
pm 命令
pm工具为包管理(package manager)的简称。
可以使用 pm 工具来执行应用的安装和查询应用包的信息、系统权限、控制应用。
pm 工具是 Android 开发与测试过程中必不可少的工具。
命令格式:pm <command>
- 命令行下输入
adb shell pm
即可获得关于 pm 的用法帮助
usage: pm list packages [-f] [-d] [-e] [-s] [-3] [-i] [-u] [--user USER_ID] [FILTER]pm list permission-groupspm list permissions [-g] [-f] [-d] [-u] [GROUP]pm list instrumentation [-f] [TARGET-PACKAGE]pm list featurespm list librariespm list userspm path PACKAGEpm dump PACKAGEpm install [-lrtsfd] [-i PACKAGE] [PATH]pm install-create [-lrtsfdp] [-i PACKAGE] [-S BYTES]pm install-write [-S BYTES] SESSION_ID SPLIT_NAME [PATH]pm install-commit SESSION_IDpm install-abandon SESSION_IDpm uninstall [-k] [--user USER_ID] PACKAGEpm set-installer PACKAGE INSTALLERpm clear [--user USER_ID] PACKAGEpm enable [--user USER_ID] PACKAGE_OR_COMPONENTpm disable [--user USER_ID] PACKAGE_OR_COMPONENTpm disable-user [--user USER_ID] PACKAGE_OR_COMPONENTpm disable-until-used [--user USER_ID] PACKAGE_OR_COMPONENTpm hide [--user USER_ID] PACKAGE_OR_COMPONENTpm unhide [--user USER_ID] PACKAGE_OR_COMPONENTpm grant PACKAGE PERMISSIONpm revoke PACKAGE PERMISSIONpm set-install-location [0/auto] [1/internal] [2/external]pm get-install-locationpm set-permission-enforced PERMISSION [true|false]pm trim-caches DESIRED_FREE_SPACEpm create-user [--profileOf USER_ID] [--managed] USER_NAMEpm remove-user USER_IDpm get-max-userspm list packages: prints all packages, optionally onlythose whose package name contains the text in FILTER. Options:-f: see their associated file.-d: filter to only show disbled packages.-e: filter to only show enabled packages.-s: filter to only show system packages.-3: filter to only show third party packages.-i: see the installer for the packages.-u: also include uninstalled packages.pm list permission-groups: prints all known permission groups.pm list permissions: prints all known permissions, optionally onlythose in GROUP. Options:-g: organize by group.-f: print all information.-s: short summary.-d: only list dangerous permissions.-u: list only the permissions users will see.pm list instrumentation: use to list all test packages; optionallysupply <TARGET-PACKAGE> to list the test packages for a particularapplication. Options:-f: list the .apk file for the test package.pm list features: prints all features of the system.pm list users: prints all users on the system.pm path: print the path to the .apk of the given PACKAGE.pm dump: print system state associated with the given PACKAGE.pm install: install a single legacy package
pm install-create: create an install session-l: forward lock application-r: replace existing application-t: allow test packages-i: specify the installer package name-s: install application on sdcard-f: install application on internal flash-d: allow version code downgrade-p: partial application install-S: size in bytes of entire sessionpm install-write: write a package into existing session; path maybe '-' to read from stdin-S: size in bytes of package, required for stdinpm install-commit: perform install of fully staged session
pm install-abandon: abandon sessionpm set-installer: set installer package namepm uninstall: removes a package from the system. Options:-k: keep the data and cache directories around after package removal.pm clear: deletes all data associated with a package.pm enable, disable, disable-user, disable-until-used: these commandschange the enabled state of a given package or component (writtenas "package/class").pm grant, revoke: these commands either grant or revoke permissionsto applications. Only optional permissions the application hasdeclared can be granted or revoked.pm get-install-location: returns the current install location.0 [auto]: Let system decide the best location1 [internal]: Install on internal device storage2 [external]: Install on external mediapm set-install-location: changes the default install location.NOTE: this is only intended for debugging; using this can causeapplications to break and other undersireable behavior.0 [auto]: Let system decide the best location1 [internal]: Install on internal device storage2 [external]: Install on external mediapm trim-caches: trim cache files to reach the given free space.pm create-user: create a new user with the given USER_NAME,printing the new user identifier of the user.pm remove-user: remove the user with the given USER_IDENTIFIER,deleting all data associated with that user
Context
Context 提供了关于应用环境全局信息的接口。它是一个抽象类,它的执行被Android系统所提供。它允许获取以应用为特征的资源和类型,是一个统领一些资源(应用程序环境变量等)的上下文。
也就是说Context描述一个应用程序环境的信息(即上下文);是一个抽象类,Android提供了该抽象类的具体实现类;通过它我们可以获取应用程序的资源和类。
下面的代码中使用到了 _context 对象这里交代下
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{internal static MainActivity Instance { get; private set; }
}// 在各自的实现类中定义然后使用
private readonly Context _context = MainActivity.Instance;
命令执行
- 执行命令前需要获取
su
权限
using Java.IO;private bool ExecuteCommand(string commandStr)
{try{using Process p = Runtime.GetRuntime().Exec("su");BufferedReader ins = new BufferedReader(new InputStreamReader(p.InputStream));BufferedReader ie = new BufferedReader(new InputStreamReader(p.ErrorStream));BufferedWriter w = new BufferedWriter(new OutputStreamWriter(p.OutputStream));w.Write(commandStr);w.Flush();w.Close();ins.Close();ie.Close();int res = p.WaitFor();if (res is 0)return true;}catch (Throwable ex){return false;}return false;
}
检查应用是否安装
检查 APP 是否安装,PackageManager 实例来获取当前系统安装的所有 APP 通过packageName 进行判断。
public bool CheckAppInstalled(string packageName)
{var packageInfo = _context.PackageManager.GetInstalledPackages(PackageInfoFlags.Activities).Where(pm => pm.PackageName == packageName).FirstOrDefault();if (packageInfo != null)return true;return false;
}
设置自动开关机
自动开关机,就属于 Android 共享库实现了
Android Intent
,具体怎么用自己搜很多,这里把开关机时间的设定作为例子。
- 需要指定共享库的操作函数,用常量存下
/// <summary>
/// 开机 Intent
/// </summary>
private static readonly string SETPOWERON = "android.intent.action.setpoweron";
/// <summary>
/// 关机 Intent
/// </summary>
private static readonly string SETPOWEROFF = "android.intent.action.setpoweroff";
- 代码实现,需要注意的是时间,它是个
long
类型需要进行转换
private static readonly string SETPOWERON = "android.intent.action.setpoweron";
private static readonly string SETPOWEROFF = "android.intent.action.setpoweroff";
private static readonly string POWERON = "poweron";
private static readonly string POWEROFF = "poweroff";
private static readonly string ENABLE = "enable";public bool PowerOffPlan(bool enable, long planTime)
{try{Intent intent = new Intent(SETPOWEROFF);intent.PutExtra(ENABLE, enable);if (enable && planTime > 0){Date dateTime = new Date(planTime);Calendar calendar = Calendar.GetInstance(Locale.SimplifiedChinese);calendar.Time = dateTime;calendar.Set(CalendarField.Second, 0);int[] time = new int[]{calendar.Get(CalendarField.Year),calendar.Get(CalendarField.Month),calendar.Get(CalendarField.DayOfMonth),calendar.Get(CalendarField.HourOfDay),calendar.Get(CalendarField.Minute),calendar.Get(CalendarField.Second),};intent.PutExtra(POWEROFF, time);}_context.SendBroadcast(intent);return true;}catch (Throwable ex){Toast.MakeText(_context, $"Exception:{ex.Message}", ToastLength.Short).Show();}return false;
}public bool PowerOnPlan(bool enable, long planTime)
{try{Intent intent = new Intent(SETPOWERON);intent.PutExtra(ENABLE, enable);if (enable && planTime > 0){Date dateTime = new Date(planTime);Calendar calendar = Calendar.GetInstance(Locale.SimplifiedChinese);calendar.Time = dateTime;calendar.Set(CalendarField.Second, 0);int[] time = new int[]{calendar.Get(CalendarField.Year),calendar.Get(CalendarField.Month),calendar.Get(CalendarField.DayOfMonth),calendar.Get(CalendarField.HourOfDay),calendar.Get(CalendarField.Minute),calendar.Get(CalendarField.Second),};intent.PutExtra(POWERON, time);}_context.SendBroadcast(intent);return true;}catch (Throwable ex){Toast.MakeText(_context, $"Exception:{ex.Message}", ToastLength.Short).Show();}return false;
}
开机自启动
通过监听系统的开机广播来实现,拉起app。
首先,需要在清单文件AndroidManifest.xml申请监听开机广播权限
android.intent.action.BOOT_COMPLETED
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
然后在清单文件 AndroidManifest.xml 声明该广播接收者,也就是下面实现监听广播处理自己的业务逻辑(拉起自己app)的类
BootReceiver
<receiver android:name=".base.BootReceiver"><intent-filter><action android:name="android.intent.action.BOOT_COMPLETED" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter>
</receiver>
最后实现一个BroadcastReceiver,该广播接收者监听
"android.intent.action.BOOT_COMPLETED"
广播,当接收到该广播时,打开该应用的启动页面。
[BroadcastReceiver(Enabled = true)]
[IntentFilter(new[] { Intent.ActionBootCompleted, Intent.ActionReboot })]
public class BootReceiver : BroadcastReceiver
{public override void OnReceive(Context context, Intent intent){try{if (intent.Action != null && intent.Action is Intent.ActionBootCompleted){Intent _intent = new Intent();_intent.SetClass(context, typeof(MainActivity));_intent.SetFlags(ActivityFlags.NewTask);context.StartService(_intent);}}catch (System.Exception ex){Toast.MakeText(context, ex.Message, ToastLength.Short).Show();}}
}
这种开机自启的方式不好的地方在于,需要在安装之后手动启动一下,才能收听广播。如果有好的方案,请不吝赐教
Xamarin.Forsm for Android 系统功能相关推荐
- Xamarin无法调试Android项目
Xamarin无法调试Android项目 项目可以正常编译,生成APK,也可以通过右键菜单部署.但是一旦开启调试,就报错.错误信息如下: 没有为此解决方案配置选中要生成的项目 出现这种问题是因为配置管 ...
- Xamarin如何生成Android项目的APK
Xamarin如何生成Android项目的APK 首先需要选择Release模式生成项目.然后从"生成"菜单中选择Export Android Package命令,就可以导出APK ...
- Xamarin.Form的Android SDK工具下载安装
Xamarin.Form的Android SDK工具下载安装 本节将讲解如何下载Xamarin.Form的Android SDK工具,并使用其中的工具管理Android SDK,如何创建模拟器等内容. ...
- Xamarin.Forms教程Android SDK工具下载安装
Xamarin.Form的Android SDK工具下载安装 本节将讲解如何下载Xamarin.Form的Android SDK工具,并使用其中的工具管理Android SDK,如何创建模拟器等内容. ...
- 从零开始学Xamarin.Forms(四) Android 准备步骤(添加第三方Xamarin.Forms.Labs库)
从零开始学Xamarin.Forms(四) Android 准备步骤(添加第三方Xamarin.Forms.Labs库) 原文:从零开始学Xamarin.Forms(四) Android 准备步骤(添 ...
- Xamarin Mono For Android
Xamarin Mono For Android 4.6.07004 完整离线安装破解版(C#开发Android.IOS工具) Xamarin是由Miguel de Icaza成立的一家新的独立公司, ...
- Xamarin Mono For Android 4.10.01068 完整离线安装破解版(C#开发Android、IOS工具)
Xamarin是由Miguel de Icaza成立的一家新的独立公司,目的是给Mono一个继续奋斗的机会.Mono for Android (原名:MonoDroid)可以让开发人员使用 Micro ...
- Xamarin Mono For Android 4.6.07004 完整离线安装破解版(C#开发Android、IOS工具)
Xamarin是由Miguel de Icaza成立的一家新的独立公司,目的是给Mono一个继续奋斗的机会.Mono for Android (原名:MonoDroid)可以让开发人员使用 Micro ...
- xamarin和mysql_Xamarin.Android 使用 SQLiteOpenHelper 进行数据库操作
一.前言 在手机中进行网络连接不仅是耗时也是耗电的,而耗电却是致命的.所以我们就需要数据库帮助我们存储离线数据,以便在用户未使用网络的情况下也可以能够使用应用的部分功能,而在需要网络连接的功能上采用提 ...
最新文章
- Dubbo基础专题——第一章(带你认识Dubbo)
- perl开发环境配置(Database,SOCKET,CISCO)j(ReShip)
- Laravel: 基础篇
- bulk_create 批量插入数据
- 大话设计模式(十 会修电脑不会修收音机?——聊设计模式原则)
- jre环境变量配置_详解java环境变量意思-用于解决环境搭建问题
- QUIC实战(四) 设置应用开机自启动
- SQL server management 查询所有触发器
- matlab频分复用,基于MATLAB的频分复用系统的仿真_.doc
- Numpy 排序(sorting)、查询(searching)、计数(counting)
- IIS 7.0 中的 HTTP 状态代码
- Cobbler实现自动化安装操作系统
- 关于虚拟主机软件配置的问题
- window.open实现post方式复杂参数传递
- IDEA 2020 配置Emmylua插件仍无法调试Lua代码问题
- 51单片机AD模块PCF8591 1路AD采样+数码管显示+Proteus仿真
- 阅读《资本论》简单思考和笔记
- 粒子群在小车机械手臂的应用
- unity 5.x android发布注意事项
- vue项目接入eslint、prettier、husky+lint-staged