Android Study之跳轉(zhuǎn)自啟動管理頁
前言
話說,最近項(xiàng)目要求軟件自啟動,腦子一想,靜態(tài)注冊廣播,監(jiān)聽用戶開機(jī)不就得了么。按照思路編寫好代碼,卻發(fā)現(xiàn)怎么都監(jiān)聽不到這個開機(jī)權(quán)限。LZ很是郁悶。經(jīng)過幾天的咨詢度娘和各種腦洞大開的測試后,發(fā)現(xiàn)有個東東叫做自啟動管理,經(jīng)過簡單測試后發(fā)現(xiàn),用戶如果給定軟件自啟動權(quán)限后,我們只需要靜態(tài)注冊開機(jī)廣播就可以監(jiān)聽到用戶開機(jī),并可以針對這一情況做相關(guān)操作。
問題延伸
但是問題又來了,那么我們怎么知道用戶是否給定軟件自啟動的權(quán)限呢?找了好久,沒找到可以解決的辦法,倒是有以下幾個辦法可以曲線救國,讓我們一塊來看看吧。
1. 靜態(tài)注冊廣播,本地保存標(biāo)識。 主要監(jiān)聽用戶開機(jī)和關(guān)機(jī)權(quán)限,如果監(jiān)聽到開機(jī)權(quán)限,本地保存一個標(biāo)識,證明用戶已給定這個權(quán)限;而監(jiān)聽到關(guān)機(jī)的同時將這個標(biāo)識置為空或者其他一個標(biāo)識,表明認(rèn)為用戶取消自啟動權(quán)限。但是這個有個問題我一直想不明白的是,加入一開始用戶設(shè)置了自啟動,但是通過其他手段又禁止自啟動,這時候怎么辦?有點(diǎn)惡心。。。
而關(guān)于廣播,有的人又提出監(jiān)聽系統(tǒng)打電話,發(fā)短信,鬧鐘,等等。。。一系列監(jiān)聽,感覺有點(diǎn)頭大。。。
2. 彈框提示用戶
用戶***登陸的時候,通過引導(dǎo)用戶去給本軟件設(shè)置自啟動權(quán)限。
。。。 。。。
還有一些其他的辦法,個人感覺不靠譜,就不一一細(xì)說了,感興趣的同志們可以自己找找~
找了好久,惡心的不要不要的,***索性直接給用戶提示吧,通過引導(dǎo),讓用戶設(shè)置自啟動。那么,讓我們先看一下效果圖吧~小的們,上圖~
由于Android旗下各大手機(jī)廠商種類很多,我們***步需要獲取到用戶當(dāng)前使用的手機(jī)型號,然后根據(jù)相應(yīng)的手機(jī)型號,去跳轉(zhuǎn)不同的自啟動界面,從而引導(dǎo)用戶設(shè)置我們的軟件自啟動權(quán)限。
介紹實(shí)現(xiàn)關(guān)鍵類
1.獲取手機(jī)型號(Build)
所屬包: android.os.Build
作用(含義): 從系統(tǒng)屬性中提取設(shè)備硬件和版本信息
靜態(tài)屬性:
1.1 BOARD 主板:The name of the underlying board, like goldfish.
1.2 BOOTLOADER 系統(tǒng)啟動程序版本號:The system bootloader version number.
1.3 BRAND 系統(tǒng)定制商:The consumer-visible brand with which the product/hardware will be associated, if any.
1.4 CPU_ABI cpu指令集:The name of the instruction set (CPU type + ABI convention) of native code.
1.5 CPU_ABI2 cpu指令集2:The name of the second instruction set (CPU type + ABI convention) of native code.
1.6 DEVICE 設(shè)備參數(shù):The name of the industrial design.
1.7 DISPLAY 顯示屏參數(shù):A build ID string meant for displaying to the user
1.8 FINGERPRINT 唯一識別碼:A string that uniquely identifies this build. Do not attempt to parse this value.
1.9 HARDWARE 硬件名稱:The name of the hardware (from the kernel command line or /proc).
1.10 HOST
1.11 ID 修訂版本列表:Either a changelist number, or a label like M4-rc20.
1.12 MANUFACTURER 硬件制造商:The manufacturer of the product/hardware.(我們目前只需要關(guān)注這個靜態(tài)屬性即可)
1.13 MODEL 版本即最終用戶可見的名稱:The end-user-visible name for the end product.
1.14 PRODUCT 整個產(chǎn)品的名稱:The name of the overall product.
1.15 RADIO 無線電固件版本:The radio firmware version number. 在API14后已過時。使用 getRadioVersion()代替。
1.16 SERIAL 硬件序列號:A hardware serial number, if available. Alphanumeric only, case-insensitive.
1.17 TAGS 描述build的標(biāo)簽,如未簽名,debug等等。:Comma-separated tags describing the build, like unsigned,debug.
1.18 TIME
1.19 TYPE build的類型:The type of build, like user or eng.
1.20 USER
2.打開其他應(yīng)用程序中的Activity或服務(wù)(ComponentName)
所屬包: android.content.ComponentName
構(gòu)造方法使用方式如下:
2.1 傳遞當(dāng)前上下文和將要跳轉(zhuǎn)的類名;
2.2 傳遞一個String包名和String類名;
2.3 傳遞一個Parcel數(shù)據(jù)容器。
需要關(guān)注的方法:unflattenFromString("傳遞將要跳轉(zhuǎn)的地址,格式為包名/跳轉(zhuǎn)Activity Name")
當(dāng)然以上還包括一些未介紹的方法屬性,大家有興趣可以看一下~
基本了解之后,讓我們一起開啟愉快的編碼之路吧~
創(chuàng)建MobileInfoUtils工具類
此工具類作用是獲取用戶手機(jī)型號,以及通過不同手機(jī)型號跳轉(zhuǎn)相應(yīng)管理界面。
同時也為大家通過幾種不同方式去實(shí)現(xiàn)跳轉(zhuǎn),大家可以仔細(xì)查看代碼~
- package cn.hle.skipselfstartmanager.util;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.net.Uri;import android.os.Build;import android.provider.Settings;import android.util.Log;/**
- * Mobile Info Utils
- * create by heliquan at 2017年3月23日
- */public class MobileInfoUtils { /**
- * Get Mobile Type
- *
- * @return
- */
- private static String getMobileType() { return Build.MANUFACTURER;
- } /**
- * GoTo Open Self Setting Layout
- * Compatible Mainstream Models 兼容市面主流機(jī)型
- *
- * @param context
- */
- public static void jumpStartInterface(Context context) {
- Intent intent = new Intent(); try {
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- Log.e("HLQ_Struggle", "******************當(dāng)前手機(jī)型號為:" + getMobileType());
- ComponentName componentName = null; if (getMobileType().equals("Xiaomi")) { // 紅米Note4測試通過
- componentName = new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity");
- } else if (getMobileType().equals("Letv")) { // 樂視2測試通過
- intent.setAction("com.letv.android.permissionautoboot");
- } else if (getMobileType().equals("samsung")) { // 三星Note5測試通過
- componentName = new ComponentName("com.samsung.android.sm_cn", "com.samsung.android.sm.ui.ram.AutoRunActivity");
- } else if (getMobileType().equals("HUAWEI")) { // 華為測試通過
- componentName = new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity");
- } else if (getMobileType().equals("vivo")) { // VIVO測試通過
- componentName = ComponentName.unflattenFromString("com.iqoo.secure/.safeguard.PurviewTabActivity");
- } else if (getMobileType().equals("Meizu")) { //萬惡的魅族
- // 通過測試,發(fā)現(xiàn)魅族是真惡心,也是夠了,之前版本還能查看到關(guān)于設(shè)置自啟動這一界面,系統(tǒng)更新之后,完全找不到了,心里默默F(xiàn)uck!
- // 針對魅族,我們只能通過魅族內(nèi)置手機(jī)管家去設(shè)置自啟動,所以我在這里直接跳轉(zhuǎn)到魅族內(nèi)置手機(jī)管家界面,具體結(jié)果請看圖
- componentName = ComponentName.unflattenFromString("com.meizu.safe/.permission.PermissionMainActivity");
- } else if (getMobileType().equals("OPPO")) { // OPPO R8205測試通過
- componentName = ComponentName.unflattenFromString("com.oppo.safe/.permission.startup.StartupAppListActivity");
- } else if (getMobileType().equals("ulong")) { // 360手機(jī) 未測試
- componentName = new ComponentName("com.yulong.android.coolsafe", ".ui.activity.autorun.AutoRunListActivity");
- } else { // 以上只是市面上主流機(jī)型,由于公司你懂的,所以很不容易才湊齊以上設(shè)備
- // 針對于其他設(shè)備,我們只能調(diào)整當(dāng)前系統(tǒng)app查看詳情界面
- // 在此根據(jù)用戶手機(jī)當(dāng)前版本跳轉(zhuǎn)系統(tǒng)設(shè)置界面
- if (Build.VERSION.SDK_INT >= 9) {
- intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
- intent.setData(Uri.fromParts("package", context.getPackageName(), null));
- } else if (Build.VERSION.SDK_INT <= 8) {
- intent.setAction(Intent.ACTION_VIEW);
- intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
- intent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
- }
- }
- intent.setComponent(componentName);
- context.startActivity(intent);
- } catch (Exception e) {//拋出異常就直接打開設(shè)置頁面
- intent = new Intent(Settings.ACTION_SETTINGS);
- context.startActivity(intent);
- }
- }
- }
寫完之后,大家可能會有疑問,LZ你是通過什么方式得知的具體包名呢?
哈,有些是Android小伙伴友情贊助,有些是通過adb命令獲取,再次為大家介紹下通過adb獲取跳轉(zhuǎn)包名。
通過adb獲取跳轉(zhuǎn)包名路徑
adb為我們提供了一個可以打印出當(dāng)前系統(tǒng)所有service信息,在后面可加上具體的服務(wù)名的牛掰命令,那就是如下:
adb shell dumpsys
在此為大家拓展一些常用的基于dumpsys命令:
獲取設(shè)備電池信息:adb shell dumpsys battery
獲取cpu信息:adb shell dumpsys cpuinfo
獲取內(nèi)存信息:adb shell dumpsys meminfo
要獲取具體應(yīng)用的內(nèi)存信息,可加上包名 adb shell dumpsys meminfo PACKAGE_NAME
獲取Activity信息:adb shell dumpsys activity
獲取package信息:adb shell dumpsys package
加上-h可以獲取幫助信息
獲取某個包的信息:adb shell dumpsys package PACKAGE_NAME
獲取通知信息:adb shell dumpsys notification
獲取wifi信息:adb shell dumpsys wifi
可以獲取到當(dāng)前連接的wifi名、搜索到的wifi列表、wifi強(qiáng)度等
獲取電源管理信息:adb shell dumpsys power
可以獲取到是否處于鎖屏狀態(tài):mWakefulness=Asleep或者mScreenOn=false
獲取電話信息:adb shell dumpsys telephony.registry
可以獲取到電話狀態(tài),例如mCallState值為0,表示待機(jī)狀態(tài)、1表示來電未接聽狀態(tài)、2表示電話占線狀態(tài)
mCallForwarding=false #是否啟用呼叫轉(zhuǎn)移
mDataConnectionState=2 #0:無數(shù)據(jù)連接 1:正在創(chuàng)建數(shù)據(jù)連接 2:已連接mDataConnectionPossible=true #是否有數(shù)據(jù)連接mDataConnectionApn= #APN名稱等
而我們將通過以下命令獲取當(dāng)前在棧頂Activity包名:
adb shell dumpsys activity top
看命令行字面的意思也就是獲取當(dāng)前在棧頂Activity信息(就是咱能看到的界面的具體信息)。
那么接下來拿LZ紅米Note4為大家仔細(xì)講解,我們?nèi)绾瓮ㄟ^命令行獲取位置界面的包名路徑。
使用adb獲取位置界面包名路徑詳情
1.連接手機(jī),手動打開系統(tǒng)自啟動管理界面,之后打開Android Studio,點(diǎn)擊下方Terminal,輸入以上命令,查看信息,如下圖:
我們看到打印出n條信息,但是對我們有用的只是紅色區(qū)域內(nèi),大家這次懂了吧~
在相應(yīng)的Activity調(diào)用,引導(dǎo)用戶設(shè)置自啟動
在此,簡單寫為點(diǎn)擊按鈕,彈框,確認(rèn)跳轉(zhuǎn)自啟動管理界面。兄弟們有具體需求在具體考慮實(shí)現(xiàn)吧~
- package cn.hle.skipselfstartmanager;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.os.Bundle;import android.view.View;import cn.hle.skipselfstartmanager.util.MobileInfoUtils;public class MainActivity extends Activity { @Override
- protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- } public void gotoSelfStartingManager(View view){
- jumpStartInterface();
- } /**
- * Jump Start Interface
- * 提示是否跳轉(zhuǎn)設(shè)置自啟動界面
- */
- private void jumpStartInterface() { try {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setMessage(R.string.app_user_auto_start);
- builder.setPositiveButton("立即設(shè)置", new DialogInterface.OnClickListener() { @Override
- public void onClick(DialogInterface dialog, int which) {
- MobileInfoUtils.jumpStartInterface(MainActivity.this);
- }
- });
- builder.setNegativeButton("暫時不設(shè)置", new DialogInterface.OnClickListener() { @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- });
- builder.setCancelable(false);
- builder.create().show();
- } catch (Exception e) {
- }
- }
- }
附上三星Note5以及魅族4運(yùn)行跳轉(zhuǎn)的結(jié)果工具類中只有360手機(jī)未測試,其他的測試通過。
三星Note5點(diǎn)擊后跳轉(zhuǎn)如下:
2.魅族4點(diǎn)擊后跳轉(zhuǎn)如下:
demo地址
查看Demo源碼地址:https://github.com/HLQ-Struggle/SkipSelfStartManager
===============這是一條神奇的分割線===================
w3812127大兄弟指出在oppo r9s 自啟動項(xiàng)的包位置發(fā)生變化,并給出了更新后的coding,感謝~在此做一下補(bǔ)充。
("OPPO")) { // OPPO R8205測試通過
componentName =ComponentName.unflattenFromString("com.oppo.safe/.permission.startup.StartupAppListActivity");
Intent intentOppo = new Intent();
intentOppo.setClassName("com.oppo.safe/.permission.startup", "StartupAppListActivity");
if (context.getPackageManager().resolveActivity(intentOppo, 0) == null) {
componentName =ComponentName.unflattenFromString("com.coloros.safecenter/.startupapp.StartupAppListActivity");
}
結(jié)束語
努力了,還不夠,不放棄,才為真。愿大家干的順心,擼的開心,玩的舒心~我們一起努力!