Android外部唤醒APP跳转指定页面

原理

通过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) {
        }

        //resume逻辑会将LightSchemeActivity也算进来,当程序没启动时从三方跳转会打开本Activity因此要排除
        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)) {
            //如果没有跳转的页面,默认打开应用之前的栈顶activity,如果有则打开主页面,因为我们的跳转逻辑写在主页面里
            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) {
        //此为google推荐的方法,并且不需要添加新的权限。
        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();
    }


}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值