命令行打开方式:
1、首先你要打开android模拟器 (下面命令行打开的4步骤我是引用百度上的)
1).找到SDK的tools文件夹,我的在D:\android-sdk-windows\tools;
2).如果没有创建AVD的话,可以用命令android list targets查看各版本对应的id;
然后android create avd --target 5 --name Android2.2;//我这里5对应的是android2.2
3).用命令android list avd查看自己以创建的AVD
4).emulator -debug avd_config -avd Android2.2就可以打开AVD了,就是有点慢
或者在eclipse上直接打开一个android程序。
2、然后输入 adb install xxx.apk,在模拟器上点击对应应用即可(安装apk后的应用程序名不知道的话得仔细找哦,肯定在模拟器上的)。
注:xxx.apk包含路径名,在命令行你只要直接把apk文件拖至windows命令窗口就可以加载完整路径了。
代码打开方式:
需要将apk拷贝至shared_prefs文件夹下
(Eclipse下工具栏window-->show view-->other-->Android-->File Explorer,
也许打开的File Explorer为空白,这个你肯定忘了先运行下面代码构成的工程。出现目录后找到data/data/工程包名/shared_prefs,ok)
public class APKTest extends Activity
{
private SharedPreferences metafer = null;
ApplicationInfo mAppInfo = null;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//apk安装或卸载路径
String installPath = "/data/data/com.hyz/shared_prefs/matchmusic.apk";
//新建shared_prefs文件夹
mkShared_prefs();
//安装apk
installApk(installPath);
//卸载apk
dumpApk(installPath);
}
public void dumpApk(String path)
{
ApplicationInfo mAppInfo = null;
PackageManager pm = getApplicationContext().getPackageManager();
PackageInfo info = pm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);
if(info != null)
{
mAppInfo = info.applicationInfo;
}
Uri uri = Uri.fromParts("package", mAppInfo.packageName, null);
Intent it = new Intent(Intent.ACTION_DELETE, uri);
startActivity(it);
}
public void installApk(String path)
{
Intent ret = new Intent();
ret.setDataAndType(Uri.fromFile(new File(path)),"application/vnd.android.package-archive");
ret.setAction(Intent.ACTION_VIEW);
startActivity(ret);
}
public void mkShared_prefs()
{
if (metafer == null)
{
// metafer = getSharedPreferences("Vdmc", 0);
metafer = PreferenceManager.getDefaultSharedPreferences(this);
}
SharedPreferences.Editor editor = metafer.edit();
//editor.putString("IMSI", "");
editor.commit();
}
}