androidapk安裝代碼
❶ Android怎樣用代碼實現安裝apk到/system/app
不用代碼,如果你是6.0以下的系統(不包括6.0)那就把這個安裝包(不要安裝)從date/app移動到system/app,修改許可權和其它app一樣,6.0及以上系統就復雜一些,前面跟6.0以下的方法一樣,不過要在system/app下面創建跟這個app一樣的名字一樣(拼音 第一個字母大寫)保存並退出,重啟,雙清,就可以了。
❷ 如何在Android7.0系統下通過Intent安裝apk
Android系統升級到7.0之後,安全性提高了不少,過去我們通常是使用這樣的代碼進行apk的安裝操作。
「`
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(apkFile), 「application/vnd.android.package-archive」);
context.startActivity(intent);
「`
但是在Android7.0的系統上,運行這段代碼,會報如下錯誤。
Caused by: android.os.FileUriExposedException
原因是,安卓官方為了提高私有文件的安全性,面向 Android 7.0 或更高版本的應用私有目錄被限制訪問(0700)。此設置可防止私有文件的元數據泄漏,如它們的大小或存在性.
傳遞軟體包網域外的 file:// URI 可能給接收器留下無法訪問的路徑。因此,嘗試傳遞 file:// URI 會觸發 FileUriExposedException。分享私有文件內容的推薦方法是使用 FileProvider。
1.定義一個FileProvider
<manifest>
...
<application>
...
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mydomain.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
...
</provider>
...
</application>
</manifest>
2.添加可用許可權的文件目錄
在res目錄下,增加xml文件夾,並新建一個名為 file_paths.xml 的文件。文件內容格式如下:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="name1" path="test1" />
...
</paths>
標簽下面必須包含至少包含以下標簽中的一個或者多個。
files-path
<files-path name="name1" path="test1" />
表示Context.getFilesDir()目錄或者其子目錄。
示例 : /data/data/com.chen.gradle/files/test1
cache-path
<cache-path name="name2" path="test2" />
表示Context.getCacheDir()目錄或者其子目錄。
示例 : /data/data/com.chen.gradle/cache/test2
external-path
<external-path name="name3" path="test3" />
表示Environment.getExternalStorageDirectory()目錄或者其子目錄。
示例 : /storage/emulated/0/test3
external-files-path
<external-files-path name="name4" path="test4" />
表示Context.getExternalFilesDir(null)目錄或者其子目錄。
示例 : /storage/emulated/0/Android/data/com.chen.gradle/files/test4
external-cache-path
<external-cache-path name="name5" path="test5" />
表示Context.getExternalCacheDir()目錄或者其子目錄。
示例 : /storage/emulated/0/Android/data/com.chen.gradle/cache/test5
3.增加到provider
通過<meta-data>標簽將上面的filepath添加到provider當中。
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mydomain.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
4.通過provider生成Uri
File imagePath = new File(Context.getFilesDir(), "test1");
File newFile = new File(imagePath, "default_image.jpg");
Uri contentUri = FileProvider.getUriForFile(getContext(), "com.mydomain.fileprovider", newFile);
5.賦予臨時許可權給Uri
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
最終安裝apk的代碼變成這樣:
public static void installApk(Context context, String apkPath) {
if (context == null || TextUtils.isEmpty(apkPath)) {
return;
}
File file = new File(apkPath);
Intent intent = new Intent(Intent.ACTION_VIEW);
//判讀版本是否在7.0以上
if (Build.VERSION.SDK_INT >= 24) {
//provider authorities
Uri apkUri = FileProvider.getUriForFile(context, "com.mydomain.fileprovider", file);
//Granting Temporary Permissions to a URI
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
}
context.startActivity(intent);
}
❸ 如何自己動手命令行安裝安卓apk文件
1、用數據線連接手機和電腦
2在電腦上打開命令行工具,進入apk文件所在目錄,比如根目錄下的game文件夾,
cd /game回車,然後輸入安裝命令adb install hello.apk回車就安裝到手機上了
❹ android怎麼實現apk的靜默安裝
Android上的靜默安裝似乎是個很誘人的功能,好多人都問這個問題。今天分享下實現靜默安裝的兩種方法,但當看完這篇文章後,仍會讓一些人失望滴。
Android把所有的Permission依據其潛在風險(屬性名為protectionLevel )劃分為四個等級,即"normal "、 "dangerous "、 "signature "、 "signatureOrSystem "。 INSTALL_PACKAGES屬於後兩者。讓我們看一下官方文檔對後兩類的描述吧。
"signature ": A permission that the system grants only if the requesting application is signed with the same certificate as the application that declared the permission. If the certificates match, the system automatically grants the permission without notifying the user or asking for the user's explicit approval.
"signatureOrSystem ": A permission that the system grants only to applications that are in the Android system image or that are signed with the same certificates as those in the system image. Please avoid using this option, as thesignature protection level should be sufficient for most needs and works regardless of exactly where applications are installed. The "signatureOrSystem " permission is used for certain special situations where multiple vendors have applications built into a system image and need to share specific features explicitly because they are being built together.
所以,這兒介紹的兩種方法各自需要的苛刻條件如下:
1.內置到ROM。即APK包的安裝位置是/system/app下。
2.使用APK的目標安裝系統同樣的簽名。
好了,先不管這些苛刻的條件,下面講下如何編寫直接安裝APK的代碼,這兒使用pm install <apk_path>命令,而不是繁雜的未公開的PackageManager.install()方法。
String[] args = { "pm", "install", "-r", apkAbsolutePath };
String result = "";
ProcessBuilder processBuilder = new ProcessBuilder(args);
Process process = null;
InputStream errIs = null;
InputStream inIs = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int read = -1;
process = processBuilder.start();
errIs = process.getErrorStream();
while ((read = errIs.read()) != -1) {
baos.write(read);
}
baos.write('/n');
inIs = process.getInputStream();
while ((read = inIs.read()) != -1) {
baos.write(read);
}
byte[] data = baos.toByteArray();
result = new String(data);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (errIs != null) {
errIs.close();
}
if (inIs != null) {
inIs.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if (process != null) {
process.destroy();
}
}
return result;
}
代碼執行後,如果安裝成功的話獲取到的result值是「 pkg: /data/local/tmp/Calculator.apk /nSuccess」,如果是失敗的話,則沒有結尾的「Success」。
安裝代碼有了,現在開始介紹第一種方法,將你自己的APK內置到ROM中。前提是,你這手機已經刷機過並且保留了recovery-windows.bat/recover-linux.sh 文件。
針對HTC-Legend的具體操作步驟為:
1.USB連接你的設備然後在命令行輸入 "adb reboot recovery" ,機子重啟,啟動後將顯示一個紅色的三角形和箭頭圖標
2 .(在PC下)進入到你的刷機文件夾然後運行 './recover-linux.sh' ,屏幕將顯示綠色的菜單
3 .如果得到的結果是 "error:device not found" ,運行 "./adb-linux kill-server" 後再一次運行 './recovery-linux.sh' 直到顯示綠色菜單.
4 .執行 "adb shell mount /dev/block/mtdblock3 /system" ,至此,可對/system進行寫操作。
5.在PC上運行命令:adb push <your_apk_path> /system/<your_apk_name>。至此,內置成功。
第二種方法,需要先打一個未簽名的APK包,然後用系統簽名對其進行簽名。這個方面的東西在我之前的一篇博文已說明,這兒就不重復了。[Android]使用platform密鑰來給apk文件簽名的命令
由於HTC-Legend是「原裝」的,所以靜默安裝倒是順利。但對於一些MOTO或樂Phone的手機,一般上是不支持的。
以上這兩種方法都在AndroidManifest中聲明android.permission.INSTALL_PACKAGES,有一點比較奇怪的是執行「 int result = checkCallingOrSelfPermission(Intent.ACTION_PACKAGE_INSTALL) 」,result的值為android.content.pm.PackageManager.PERMISSION_DENIED而不是PERMISSION_GRANTED。
❺ 怎樣查看 Android APP 源代碼
需要把反編譯的apk存放到apktools同級文件夾目錄下,然後運行要查看的安裝包,具體操作如下:
1、首先把反編譯的apk存放到apktools同級文件夾目錄下,如下圖所示。
❻ Android 代碼,關於pm指令安裝apk。
你看你的加號被寫到字元串里了
❼ android如何在代碼中安裝apk
/**
* 安裝APK文件
*/
private void installApk()
{
File apkfile = new File(文件路徑,應用名稱);
if (!apkfile.exists())
{
return;
}
// 通過Intent安裝APK文件
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive");
mContext.startActivity(i);
}
❽ 怎樣查看 Android APP 源代碼
需要把反編譯的apk存放到apktools同級文件夾目錄下,然後運行要查看的安裝包,具體操作如下:
1、首先把反編譯的apk存放到apktools同級文件夾目錄下,如下圖所示。
❾ Android APK安裝流程(4)--APK載入
上面 主要分析到APK的過程,這里我們開始分析APK的載入過程。直接看之前流程進行到下一步的 processPendingInstall() 方法:
installPackagesLI() 可以支持單包和多包載入,載入主要分為4個階段:
執行完2-2的 scanPackageTrackLI() 之後Pms的兩大核心數據結構都已經准備好了,一個是代表掃描結果的final ArrayMap<String, PackageParser.Package> mPackages = new ArrayMap<>();中的PackageParser.Package,另外一個是mSettings.mPackages的PackageSetting 數據結構,這兩個結構PackageParser.Package代表掃描結果,為靜態數據,掃描完成後就不會發生變化。PackageSetting用於存儲安裝應用的動態數據,如許可權授予情況等。PackageParser.Package由於是靜態數據,掃描apk就可以獲取。PackageSetting生成之後會被記錄到文件中,以後每次系統啟動都會重新載入。
❿ 如何看一個android的.apk應用安裝器的源代碼。
朋友,你好,這個需要用到反編譯,才可以看到源碼哦。
APK反編譯方法如下:
一、更改apk文件的後綴名,如:LianyunHelper3.0.11.apk改成LianyunHelper3.0.11.zip
二、用zip解壓縮LianyunHelper3.0.11.zip文件
三、從解壓縮的文件夾中取出classes.dex文件並放到dex2jar.bat所在目錄
四、運行cmd命令,進入dex2jar.bat所在的目錄,輸入dex2jar.bat classes.dex即可生成classes.dex.dex2jar.jar文件
五、用jd-gui工具打開classes.dex.dex2jar.jar文件,即可看到源碼
六、將AndroidManifest.xml文件放到AXMLPrinter2.jar所在目錄,運行cmd命令,進入 AXMLPrinter2.jar所在目錄,輸入java -jar AXMLPrinter2.jar AndroidManifest.xml > AndroidManifest.txt。