原理
通过scheme协议来唤醒APP。
一.定义一个能响应外部链接的Activity——H5OpenAppActivity,H5OpenAppActivity获取外部唤醒该activity的URL,通过intent传给其他activity。再由H5OpenAppActivity来判断app是否在后台运行,还是从未打开,还是刚刚下载。
1.刚刚下载时,由H5OpenAppActivity跳SplashActivity,GuideActivity,主activity。SplashActivity和GuideActivity主要是把外部唤醒H5OpenAppActivity的URL传递给主activity。
2.已经下载过,打开过但是后台程序已经杀死,由H5OpenAppActivity跳SplashActivity,主activity。
3.该app在后台挂着,由H5OpenAppActivity跳SplashActivity,主activity。
注意:如果没有跳转的页面,默认打开应用之前的栈顶activity,如果有则打开主页面,因为我们的跳转逻辑写在主页面里
二.为什么都要跳主activity呢? 因为我们除了新起一个H5OpenAppActivity用来相应外部唤起的链接,还定义了一个EventJumpActivity用来跳转到想唤起的页面。
两个新创的activity
1.android:scheme=“openapp"是唤起的协议 2. android:host=”${applicationId}"是app名字
<!--start H5唤起APP-->
<activity
android:name="com.hundsun.main.openapp.H5OpenAppActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"
android:noHistory="true">
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="${applicationId}"
android:scheme="openapp" />
</intent-filter>
</activity>
<activity
android:name="com.hundsun.main.openapp.EventJumpActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"/>
<!--end H5唤起APP-->
H5OpenAppActivity源码
public class H5OpenAppActivity extends FragmentActivity {
@Override
protected void onResume() {
Intent i_getvalue = getIntent();
if (i_getvalue != null) {
String action = i_getvalue.getAction();
if (Intent.ACTION_VIEW.equals(action)) {
Uri uri = i_getvalue.getData();
if ((i_getvalue.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
startMainActivity(null);
} else {
if (uri != null) {
startMainActivity(uri);
}
}
}
}
super.onResume();
finish();
}
private void startMainActivity(Uri uri) {
boolean hasOpenMainPage = false;
try {
if (HsActivityManager.getInstance().getTopActivity() != null && HybridCore.getInstance().getPageManager().getPageCount() > 0) {
hasOpenMainPage = true;
}
} catch (NullPointerException e) {
}
if (hasOpenMainPage && isTopActivity(H5OpenAppActivity.this)) {
resumeCurrentActivity(uri);
} else {
Intent startintent = getPackageManager().getLaunchIntentForPackage(getPackageName());
startintent.putExtra(IntentKeys.H5_OPEN_APP_URI, uri);
startintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startintent);
}
}
private void resumeCurrentActivity(Uri uri) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> task_info = manager.getRunningTasks(30);
String className = "";
String packgeName = getPackageName();
for (int i = 0; i < task_info.size(); i++) {
if (packgeName.equals(task_info.get(i).topActivity.getPackageName())) {
manager.moveTaskToFront(task_info.get(i).id, ActivityManager.MOVE_TASK_WITH_HOME);
className = task_info.get(i).topActivity.getClassName();
}
}
String url = "";
try {
if (uri != null) {
url = uri.getQuery();
}
} catch (Exception e) {
}
if (!TextUtils.isEmpty(url)) {
TemplateItem page = HybridCore.getInstance().getTemplateParser().getTemplate("main");
className = page.getClassname();
}
if (!TextUtils.isEmpty(className)) {
Intent intentgo = getIntent();
Bundle bundle = intentgo.getExtras();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putBoolean("isFromNotifacation", true);
intentgo.putExtras(bundle);
intentgo.setAction(Intent.ACTION_MAIN);
intentgo.addCategory(Intent.CATEGORY_LAUNCHER);
try {
intentgo.setComponent(new ComponentName(this, Class.forName(className)));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
intentgo.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
| Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Intent targetInaget = new Intent(this, EventJumpActivity.class);
targetInaget.putExtra(IntentKeys.H5_OPEN_APP_URI,uri);
Intent[] intents = new Intent[]{intentgo, targetInaget};
startActivities(intents);
}
}
public static boolean isTopActivity(Context mContext) {
String packageName = mContext.getPackageName();
ActivityManager am = (ActivityManager) mContext.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> list = am.getRunningAppProcesses();
if (list.size() == 0) {
return false;
}
for (ActivityManager.RunningAppProcessInfo process : list) {
if (process.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND &&
process.processName.equals(packageName)) {
return true;
}
}
return false;
}
}
EventJumpActivity
public class EventJumpActivity extends FragmentActivity {
private void processIntent(Intent data) {
Uri uri = data.getParcelableExtra(IntentKeys.H5_OPEN_APP_URI);
try {
if ("openapp".equals(uri.getScheme())) {
}
} catch (Exception e) {
}
}
@Override
protected void onResume() {
super.onResume();
processIntent(getIntent());
finish();
}
}
|