這篇文章主要介紹Android中普通應(yīng)用升級(jí)為系統(tǒng)應(yīng)用并獲取系統(tǒng)權(quán)限的操作示例,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
目前創(chuàng)新互聯(lián)已為數(shù)千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站托管運(yùn)營(yíng)、企業(yè)網(wǎng)站設(shè)計(jì)、內(nèi)鄉(xiāng)網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶(hù)導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶(hù)和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
Android是一種基于Linux內(nèi)核的自由及開(kāi)放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國(guó)Google公司和開(kāi)放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開(kāi)發(fā)。
有時(shí)候使用某些api需要使用系統(tǒng)權(quán)限,如調(diào)用PackageInstaller的相關(guān)接口,需要android.permission.INSTALL_PACKAGES權(quán)限,該權(quán)限系統(tǒng)只會(huì)授權(quán)給系統(tǒng)應(yīng)用,此時(shí)可以考慮將我們的應(yīng)用升級(jí)為系統(tǒng)應(yīng)用,升級(jí)為系統(tǒng)應(yīng)用有兩種方法:
1、將apk放到/system/app目錄下,重啟手機(jī)即可,此方法比較粗暴,而且需要修改/system目錄的讀寫(xiě)權(quán)限,因此需要root,而且隨著Android系統(tǒng)版本對(duì)權(quán)限管理越來(lái)越嚴(yán),root和修改讀寫(xiě)權(quán)限更繁瑣和復(fù)雜
2、添加sharedUserId="android.uid.system"配置,同時(shí)使用系統(tǒng)簽名對(duì)apk進(jìn)行重簽名,安裝后即有系統(tǒng)權(quán)限,此方法需要獲取到系統(tǒng)簽名,但通常廠商開(kāi)發(fā)的rom都不會(huì)公開(kāi)簽名,因此如果針對(duì)某些自己集成系統(tǒng)的需求,可以使用此方案。
下面針對(duì)第2種方案進(jìn)行講解。
1、首先需要在menifest文件中添加sharedUserId="android.uid.system"配置,如下
2、添加需要的使用的系統(tǒng)權(quán)限,如
3、使用系統(tǒng)簽名對(duì)apk進(jìn)行簽名,首先需要打未簽名的包,Android Studio環(huán)境下點(diǎn)擊右側(cè)Gradle按鈕,彈出目錄,打開(kāi)Tasks->build->assemble...,其中assemble開(kāi)頭的幾項(xiàng)即是打包任務(wù),雙擊相應(yīng)項(xiàng)即可獲取相應(yīng)的包
接著要獲取系統(tǒng)簽名,找到系統(tǒng)簽名文件“platform.pk8”和“platform.x509.pem”,在系統(tǒng)源碼路徑下
簽名文件路徑:android/build/target/product/security/
準(zhǔn)備好簽名工具:“signapk.jar”
位置:android/prebuilts/sdk/tools/lib
將未簽名的apk、簽名文件和簽名工具放在同一路徑下,開(kāi)啟終端,cd到該目錄,運(yùn)行如下命令即可得到帶系統(tǒng)簽名的apk
java -jar signapk.jar platform.x509.pem platform.pk8 Demo.apk signedDemo.apk
以上的方法相對(duì)還是比較繁瑣,而且不便于調(diào)試,可以將系統(tǒng)簽名打入keystore,使用該keystore即可在Android Studio環(huán)境下生成帶系統(tǒng)簽名的apk
1、首先需要生成keystore文件,生成過(guò)程在此省略,可自行百度
2、下載keytool-importkeypair,https://github.com/getfatday/keytool-importkeypair/
3、該操作需要linux系統(tǒng),并安裝jdk,win10環(huán)境下可以開(kāi)啟“基于Lunix的Windows子系統(tǒng)”,不需要去折騰安裝虛擬機(jī)或者系統(tǒng)。當(dāng)如,如果兄dei你已經(jīng)有Linux系統(tǒng)或者是mac,那么恭喜你,省了很多需要折騰的步驟
4、將keystore、platform.pk8、platform.x509.pem、keytool-importkeypair放在同一目錄下,開(kāi)啟終端,cd到此目錄,運(yùn)行如下命令即可將系統(tǒng)簽名打入keystore
/keytool-importkeypair -k ./demo.keystore -p password -pk8 platform.pk8 -cert platform.x509.pem -alias demoAlias
解釋?zhuān)?P后接keystore密碼,-alias后接key alias
5、運(yùn)行結(jié)束即會(huì)在當(dāng)前目錄下生成帶有系統(tǒng)簽名的keystore,使用此keystore安裝Android Studio打包流程打包即可得到帶系統(tǒng)權(quán)限的apk
補(bǔ)充知識(shí):Android 系統(tǒng)級(jí)應(yīng)用守護(hù)進(jìn)程
我就廢話(huà)不多說(shuō)了,還是直接看代碼吧!
--- a/frameworks/base/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java +++ b/frameworks/base/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java @@ -16,24 +16,51 @@ package com.android.systemui; +import android.app.ActivityManager; +import android.app.ActivityManager.RunningAppProcessInfo; import android.app.Application; import android.app.Instrumentation; import android.content.BroadcastReceiver; import android.content.Context; +import android.content.ComponentName; import android.content.Intent; import android.content.IntentFilter; import android.content.res.Configuration; +import android.database.ContentObserver; +import android.net.Uri; +import android.os.Build; +import android.os.Handler; import android.os.Process; import android.os.PowerManager; import android.os.SystemClock; import android.os.SystemProperties; import android.os.UserHandle; import android.util.Log; - +import java.util.List; +import java.util.Calendar; import com.android.systemui.stackdivider.Divider; import java.util.HashMap; import java.util.Map; +import java.util.Objects; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.PrintWriter; +import java.math.BigInteger; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Random; /** * Application class for SystemUI. @@ -42,6 +69,8 @@ public class SystemUIApplication extends Application { private static final String TAG = "SystemUIService"; private static final boolean DEBUG = false; + + private Context mContext; /** * The classes of the stuff to start. @@ -86,6 +115,8 @@ public class SystemUIApplication extends Application { // application theme in the manifest does only work for activities. Keep this in sync with // the theme set there. setTheme(R.style.systemui_theme); + + mContext = getApplicationContext(); SystemUIFactory.createFromConfig(this); @@ -143,6 +174,7 @@ public class SystemUIApplication extends Application { private static final String SETTING_SYSTEM = "tchip.provider.setting.system"; private static final String SETTING_SECURE = "tchip.provider.setting.secure"; private static final String SETTING_GLOBAL = "tchip.provider.setting.global"; + private static final String BROADCAST_SCREENSHOT = "rk.android.screenshot.action"; private String valueContent = "0"; private MainReceiver mainReceiver; @@ -150,6 +182,10 @@ public class SystemUIApplication extends Application { android.util.Log.i("AZ", log); } + private void LDLog(String log) { + android.util.Log.i("LD", log); + } + public class MainReceiver extends BroadcastReceiver { @Override @@ -225,6 +261,18 @@ public class SystemUIApplication extends Application { intentDown.putExtra("android.intent.extra.KEY_CONFIRM", false); intentDown.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intentDown); + } else if (BROADCAST_SCREENSHOT.equals(action)) { + Intent intentScreenShot = new Intent("android.intent.action.SCREENSHOT"); + intentScreenShot.putExtra("path", intentScreenShot.getExtras().getString("path")); + intentScreenShot.putExtra("name", intentScreenShot.getExtras().getString("name")); + context.sendBroadcast(intentScreenShot); + } else if (BROADCAST_TIMETICK.equals(action)) { + //checkLaidianAppAlive(); + try{ + mHandler.removeCallbacks(laidianAppR); + }catch(Exception e){ + } + mHandler.postDelayed(laidianAppR, 1000); } } } @@ -326,4 +374,82 @@ public class SystemUIApplication extends Application { public SystemUI[] getServices() { return mServices; } + + + Handler mHandler = new Handler(); + private int laidianAppCheckTime = 3000; + Runnable laidianAppR = new Runnable(){ + + @Override + public void run() { + checkAppAlive(pkgName", "clsName"); + mHandler.postDelayed(laidianAppR, laidianAppCheckTime); + } + + }; + + private void checkAppAlive(String pkgName, String clsName){ + long startTime = System.currentTimeMillis(); + if(!isAppAlive(pkgName)){ + LDLog("checkAppAlive.start to start app... "); + try{ + ComponentName componentName = new ComponentName(pkgName, clsName); + Intent intent = new Intent(); + intent.setComponent(componentName); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + mContext.startActivity(intent); + }catch(Exception e){ + + } + }else{ + LDLog("checkAppAlive.app is running... "); + } + LDLog("checkAppAlive.used time : " + (System.currentTimeMillis() - startTime)); + } + + public boolean isAppAlive(String packageName){ + //boolean isAlive = false; + //String command = "ps | grep " + packageName/* + " | busybox awk '{print $2}'"*/; + //String[] result = ExecCmd.getPackagePID(command); + //for(String str :result){ + // if(!"-1".equals(str)){ + // isAlive = true; + // } + //} + //return isAlive; + + // 獲取正在運(yùn)行的進(jìn)程 + /*ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); + ListappProcesses = activityManager.getRunningAppProcesses(); + + for (RunningAppProcessInfo appProcess : appProcesses) { + String[] pkgList = appProcess.pkgList; + for (String pckName : pkgList) { + if(packageName.equals(pckName)){ + return true; + } + + } + }*/ + ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); + List recentTasks = am.getRecentTasks(30, ActivityManager.RECENT_IGNORE_UNAVAILABLE + | ActivityManager.RECENT_INCLUDE_PROFILES); + + for (ActivityManager.RecentTaskInfo recentInfo : recentTasks){ + //if (recentInfo.origActivity != null) { + String pckName = recentInfo.baseIntent.getComponent().getPackageName(); + LDLog("isAppAlive.packageName=" + pckName); + if(packageName.equals(pckName)){ + return true; + } + //} + } + return false; + } + }
以上是“Android中普通應(yīng)用升級(jí)為系統(tǒng)應(yīng)用并獲取系統(tǒng)權(quán)限的操作示例”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!