您的位置:首页 > 移动开发 > Android开发

Android不安装APK,也能调用其程序

2014-09-12 10:38 274 查看
Android不安装APK,也能调用其程序。

看过很多博客,依然写不出好程序。

首先需要建立一个基本程序,称为主程序A。

主要代码:

public class MainActivity extends Activity {

String TAG = "A-B";
//Shared storage cannot protect your application from code injection attacks.
//共享存储无法保护您的应用程序的代码注入攻击。
//如果SD卡无法访问
//需要将文件复制到/data/data/<packagename>中
String rootPath = Environment.getExternalStorageDirectory().getPath();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button btn = new Button(this);
btn.setText("run");
setContentView(btn);
rootPath = getFilesDir().getPath();
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Bundle paramBundle = new Bundle();
paramBundle.putBoolean("KEY_START_FROM_OTHER_ACTIVITY", true);
String dexpath = rootPath+File.separator+"B.apk";
String dexoutputpath = rootPath;
LoadAPK(paramBundle, dexpath, dexoutputpath);
}
});
}

public void LoadAPK(Bundle paramBundle, String dexpath, String dexoutputpath) {
ClassLoader localClassLoader = ClassLoader.getSystemClassLoader();
DexClassLoader localDexClassLoader = new DexClassLoader(dexpath,
dexoutputpath, null, localClassLoader);
try {
PackageInfo plocalObject = getPackageManager()
.getPackageArchiveInfo(dexpath, 1);

if ((plocalObject.activities != null)
&& (plocalObject.activities.length > 0)) {
String activityname = plocalObject.activities[0].name;
Log.d(TAG, "activityname = " + activityname);

Class localClass = localDexClassLoader.loadClass(activityname);
Constructor localConstructor = localClass
.getConstructor(new Class[] {});
Object instance = localConstructor.newInstance(new Object[] {});
Log.d(TAG, "instance = " + instance);

Method localMethodSetActivity = localClass.getDeclaredMethod(
"setActivity", new Class[] { Activity.class });
localMethodSetActivity.setAccessible(true);
localMethodSetActivity.invoke(instance, new Object[] { this });

Method methodonCreate = localClass.getDeclaredMethod(
"onCreate", new Class[] { Bundle.class });
methodonCreate.setAccessible(true);
methodonCreate.invoke(instance, new Object[] { paramBundle });
}
return;
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

接下来是子程序B:

public class MainActivity extends Activity {

private static final String TAG = "B";
private Activity otherActivity;

@Override
public void onCreate(Bundle savedInstanceState) {
boolean b = false;
TextView textView;
if (savedInstanceState != null) {
b = savedInstanceState.getBoolean("KEY_START_FROM_OTHER_ACTIVITY", false);
if (b) {
textView = new TextView(otherActivity);
textView.setText("AAA");
this.otherActivity.setContentView(textView);
}
}
if (!b) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
otherActivity = this;
textView = new TextView(otherActivity);
textView.setText("BBB");
setContentView(textView);
}
}

public void setActivity(Activity paramActivity) {
Log.d(TAG, "setActivity..." + paramActivity);
this.otherActivity = paramActivity;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: