A、软件安装代码实现,Action为Intent.ACTION_VIEW
//安装程序的apk文件路径
String fileName = Environment.getExternalStorageDirectory() + apkName;
//创建URI
Uri uri = Uri.fromFile(new File(fileName));
//创建Intent意图
Intent intent = new Intent(Intent.ACTION_VIEW);
//设置Uri和类型
intent.setDataAndType(Uri, application/vnd.android.package-archive");
//执行意图进行安装
startActivity(intent);
B、软件卸载代码实现,注意这里Action是Intent.ACTION_DELETE
//通过程序的报名创建URI
Uri packageURI = Uri.parse("package: poet.android.app.id");
//创建Intent意图
Intent intent = new Intent(Intent.ACTION_DELETE);
//执行卸载程序
startActivity(intent);
android应用后台安装,静默安装的代码实现方法
http://hi.baidu.com/6936995/blog/item/eca4436387a80d7b0d33fa31.html
首先,你要有linux环境或其他可以mmm交叉编译的环境,因为这个用到的是隐藏api,SDK中没有的,ecilipse里面会提示包不存在,但交叉编译可以,如果你不知道mmm编译,请return。
代码如下:
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.IPackageInstallObserver;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class InstallActivity extends Activity {
/** Called when the activity is first created. */
private Button btn2=null;
private final int INSTALL_COMPLETE = 1;
final static int SUCCEEDED = 1;
final static int FAILED = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button) findViewById(R.id.btn2);
btn2.setOnClickListener(listener2);
}
private OnClickListener listener2=new OnClickListener(){
public void onClick(View v) {
String fileName = Environment.getExternalStorageDirectory() + "/baidu/"+"UC.apk";
Uri uri = Uri.fromFile(new File(fileName));
int installFlags = 0;
PackageManager pm = getPackageManager();
try {
PackageInfo pi = pm.getPackageInfo("com.uc.browser",
PackageManager.GET_UNINSTALLED_PACKAGES);
if(pi != null) {
installFlags |= PackageManager.INSTALL_REPLACE_EXISTING;
}
} catch (NameNotFoundException e) {
}
// String installerPackageName = getIntent().getStringExtra(
// Intent.EXTRA_INSTALLER_PACKAGE_NAME);
PackageInstallObserver observer = new PackageInstallObserver();
pm.installPackage(uri, observer, installFlags, "com.uc.browser");
}
};
class PackageInstallObserver extends IPackageInstallObserver.Stub {
public void packageInstalled(String packageName, int returnCode) {
Message msg = mHandler.obtainMessage(INSTALL_COMPLETE);
msg.arg1 = returnCode;
mHandler.sendMessage(msg);
}
};
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case INSTALL_COMPLETE:
// Update the status text
// Show the ok button
int centerTextLabel;
Drawable centerTextDrawable = null;
if(msg.arg1 == SUCCEEDED) {
} else {}
if (centerTextDrawable != null) {}
break;
default:
break;
}
}
};
}
重点是要实现一个这个类PackageInstallObserver,另外要注意import android.content.pm.……的那些包一定要导入。android.mk文件如下
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := user
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_PACKAGE_NAME := InstallActivity
LOCAL_CERTIFICATE := platform
include $(BUILD_PACKAGE)
重点是LOCAL_CERTIFICATE := platform要对
另外编译的时候有可能会报R.java类重复错误,把你工程下gen文件夹删除即可,不会影响编译,传说中会在别的地方自动生成^……
作为一个新手,研究了四天无果,网上苦苦追寻无果,最后还是再前辈的带领下看系统安装源代码得来。看来要学好android还是要会看源代码。卷毛大帝。
这个在2.1上编译通过可以用,后台安装可以实现,后台卸载也可以实现,可以和安卓市场一样了。希望大家不要用这个功能搞图谋不轨的事情。
现在把工程和apk传上来大家看看,这种方法看来也不是适用于每一个系统,android系统太不统一了,只能这样了。
http://115.com/file/bhrs7fr4#
工程和apk.zip
这个需要源码编译环境,就是要下载一个android系统的源码
有时候我们需要软件实现静默安装,但是Android并未提供相应的API,然而我们知道命令行安装android的时候是不会提示用户的,所有要实现这个功能,我们就可以从执行命令行的方式实现。android提供的安装命令是
- pm install package
- ps : pm install /sdcard/android.apk
但是执行这个命令需要前提条件,需要是系统级应用或者具有ROOT权限。我们先介绍通过ROOT权限的方式执行。
1,通过获取ROOT权限静默安装APK
看代码:
- new Thread() {
- public void run() {
- Process process = null;
- OutputStream out = null;
- InputStream in = null;
- try {
-
- process = Runtime.getRuntime().exec("su");
- out = process.getOutputStream();
-
- out.write(("pm install -r " + currentTempFilePath + "\n").getBytes());
- in = process.getInputStream();
- int len = 0;
- byte[] bs = new byte[256];
- while (-1 != (len = in.read(bs))) {
- String state = new String(bs, 0, len);
- if (state.equals("Success\n")) {
-
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (out != null) {
- out.flush();
- out.close();
- }
- if (in != null) {
- in.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }.start();
当然也可以通过NDK实现,代码就不放了。
第二种方式,同样是通过pm命令实现,不用请求ROOT,但是需要系统的签名。这里附上模拟器的的签名,用这个签名的APK安装在模拟器上可以实现不请求ROOT而静默安装的效果。
?
-
- android:sharedUserId="android.uid.system"
- new Thread() {
- public void run() {
- Process process = null;
- InputStream in = null;
- try {
-
- process = Runtime.getRuntime().
- exec("pm install -r " + currentTempFilePath + "\n");
- in = process.getInputStream();
- int len = 0;
- byte[] bs = new byte[256];
- while (-1 != (len = in.read(bs))) {
- String state = new String(bs, 0, len);
- if (state.equals("Success\n")) {
-
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (in != null) {
- in.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }.start();
from : http://www.myeducs.cn/sys/Android/android-jingmoanzhuangdeshixian-yi.htm
通常情况下,android是没有提供静默方式的上层接口,我们需要在android源代码下来调用这个隐藏的接口来完成静默安装。
最重要的就是参考android系统目录下的packages/apps/PackageInstaller,
当中有两个文件 PackageInstallerActivity.java,InstallAppProgress.java ,前者就是我们通常看到的带有提示对话框的安装应用程序,后者是点确定安装后调用的intent。
现提供一个静默安装的关键类,该类在android2.2下成功编译, 其中通过循环调用接口instatllBatch则可实现批量安装
当然最后的应用程序别忘记添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.DELETE_PACKAGES" />
<uses-permission android:name="android.permission.CLEAR_APP_CACHE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CLEAR_APP_USER_DATA" />
package com.android.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageParser;
import android.net.Uri;
import android.util.Log;
import android.util.DisplayMetrics;
import android.content.pm.IPackageInstallObserver;
import android.content.pm.IPackageDeleteObserver;
import android.os.FileUtils;
import android.os.Handler;
import android.os.Message;
public class PackageInstaller {
private File mTmpFile;
private final int INSTALL_COMPLETE = 1;
final static int SUCCEEDED = 1;
final static int FAILED = 0;
private final static String TAG = "PackInstaller";
private Context mContext;
private ApplicationInfo mAppInfo;
public PackageInstaller(Context context) {
mContext = context;
}
public void install(String path,String packageName){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path)),
"application/vnd.android.package-archive");
mContext.startActivity(intent);
}
public void instatllBatch(String path) {
Log.i(TAG, "path=" + path);
int installFlags = 0;
Uri mPackageURI = Uri.fromFile(new File(path));
PackageParser.Package mPkgInfo = getPackageInfo(mPackageURI);
mAppInfo = mPkgInfo.applicationInfo;
String packageName = mAppInfo.packageName;
Log.i(TAG, "====install packageName ="+packageName);
PackageManager pm = mContext.getPackageManager();
try {
PackageInfo pi = pm.getPackageInfo(packageName,
PackageManager.GET_UNINSTALLED_PACKAGES);
if (pi != null) {
installFlags |= PackageManager.INSTALL_REPLACE_EXISTING;
}
} catch (NameNotFoundException e) {
}
if ((installFlags & PackageManager.INSTALL_REPLACE_EXISTING) != 0) {
Log.w(TAG, "Replacing package:" + packageName);
}
PackageInstallObserver observer = new PackageInstallObserver();
pm.installPackage(mPackageURI, observer, installFlags,
packageName);
}
private class PackageInstallObserver extends IPackageInstallObserver.Stub {
public void packageInstalled(String packageName, int returnCode) {
// Message msg = mHandler.obtainMessage(INSTALL_COMPLETE);
// msg.arg1 = returnCode;
// mHandler.sendMessage(msg);
Log.i(TAG, "====INSTALL_COMPLETE");
}
}
private class PackageDeleteObserver extends IPackageDeleteObserver.Stub {
public void packageDeleted(boolean succeeded) {
// Message msg = mHandler.obtainMessage(UNINSTALL_COMPLETE);
// msg.arg1 = succeeded?SUCCEEDED:FAILED;
// mHandler.sendMessage(msg);
Log.i(TAG, "====UNINSTALL_COMPLETE");
}
}
public void uninstall(String packageName){
Uri packageURI = Uri.parse("package:" + packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,
packageURI);
mContext.startActivity(uninstallIntent);
}
public void uninstallBatch(String packageName) {
PackageDeleteObserver observer = new PackageDeleteObserver();
mContext.getPackageManager().deletePackage(packageName, observer, 0);
}
/*
* Utility method to get package information for a given packageURI
*/
public PackageParser.Package getPackageInfo(Uri packageURI) {
final String archiveFilePath = packageURI.getPath();
PackageParser packageParser = new PackageParser(archiveFilePath);
File sourceFile = new File(archiveFilePath);
DisplayMetrics metrics = new DisplayMetrics();
metrics.setToDefaults();
PackageParser.Package pkg = packageParser.parsePackage(sourceFile,
archiveFilePath, metrics, 0);
// Nuke the parser reference.
packageParser = null;
return pkg;
}
/*
* Utility method to get application information for a given packageURI
*/
public ApplicationInfo getApplicationInfo(Uri packageURI) {
final String archiveFilePath = packageURI.getPath();
PackageParser packageParser = new PackageParser(archiveFilePath);
File sourceFile = new File(archiveFilePath);
DisplayMetrics metrics = new DisplayMetrics();
metrics.setToDefaults();
PackageParser.Package pkg = packageParser.parsePackage(sourceFile, archiveFilePath, metrics, 0);
if (pkg == null) {
return null;
}
return pkg.applicationInfo;
}
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case INSTALL_COMPLETE:
if(msg.arg1 == SUCCEEDED) {
} else {}
break;
default:
break;
}
}
};
}