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。