-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyApplication.java
More file actions
89 lines (78 loc) · 3.48 KB
/
MyApplication.java
File metadata and controls
89 lines (78 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.example.androidtest;
import android.app.Application;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import com.apkplug.service.ApkplugCloudAgent;
import org.apkplug.Bundle.BundleControl;
import org.apkplug.Bundle.installCallback;
import org.apkplug.app.FrameworkFactory;
import org.apkplug.app.FrameworkInstance;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.ServiceReference;
/**
* Created by linyun on 14-6-4.
*/
public class MyApplication extends Application {
private FrameworkInstance mFramework;
@Override
public void onCreate() {
super.onCreate();
try {
initApkPlug();
} catch (Exception e) {
e.printStackTrace();
}
}
private void initApkPlug() throws Exception {
mFramework = FrameworkFactory.getInstance().start(null, this);
BundleContext bundleContext = mFramework.getSystemBundleContext();
ApkplugCloudAgent.init(bundleContext);
ServiceReference serviceReference = bundleContext.getServiceReference(BundleControl.class.getName());
if (serviceReference != null) {
BundleControl bundleControl = (BundleControl) bundleContext.getService(serviceReference);
bundleControl.install(bundleContext, "file:/data/data/com.example.androidtest/files/test.apk", new installCallback() {
@Override
public void callback(int status, Bundle bundle) {
String text;
if (status == installCallback.stutas5 || status == installCallback.stutas7) {
text = String.format("插件安装 %s:%d", bundle.getName(), status);
onSuccess(bundle);
Log.d("install callback", text);
Toast.makeText(MyApplication.this, text, Toast.LENGTH_SHORT).show();
} else {
text = String.format("插件安装失败:%s", bundle.getName());
Log.d("install callback", text);
Toast.makeText(MyApplication.this, text, Toast.LENGTH_SHORT).show();
try {
bundle.uninstall();
Toast.makeText(MyApplication.this, "uninstall success", Toast.LENGTH_SHORT).show();
} catch (BundleException e) {
e.printStackTrace();
}
}
}
private void onSuccess(Bundle bundle) {
if (bundle.getState() != bundle.ACTIVE) {
try {
bundle.start();
} catch (Exception e) {
e.printStackTrace();
}
}
if (bundle.getBundleActivity() != null) {
Intent i = new Intent();
i.setClassName(MyApplication.this, bundle.getBundleActivity().split(",")[0]);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MyApplication.this.startActivity(i);
} else {
Toast.makeText(MyApplication.this, "该插件没有配置BundleActivity",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
}