web页面点击按钮唤起App跳转到指定页面以及返回键的处理

时间:2024-03-29 18:32:04

1.在需要跳转的指定页面的AndroidManifest中配置

<activity
    android:name=".activity.goods.GoodsDetail"
    android:configChanges="keyboardHidden|orientation|screenSize"
//点击返回时跳转的activity
    android:parentActivityName=".MainActivity"
    android:screenOrientation="portrait">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".MainActivity" />
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data
            android:host="my.com"
            android:scheme="m" />
    </intent-filter>
</activity>

2.在指定页面的接值

Intent i_getvalue = getIntent();
String action = i_getvalue.getAction();

if (Intent.ACTION_VIEW.equals(action)) {
    Uri uri = i_getvalue.getData();
    if (uri != null) {
        itemId = uri.getQueryParameter("id");
    }
} else {
    itemId = getIntent().getStringExtra("id");
}

3.指定页面返回键的处理

@Override
public void onBackPressed() {
    if (isLaunchedActivity(context, MainActivity.class)) {
        Intent upIntent = NavUtils.getParentActivityIntent(this);
        if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
            TaskStackBuilder.create(this)
                    .addNextIntentWithParentStack(upIntent)
                    .startActivities();
        } else {
            upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            NavUtils.navigateUpTo(this, upIntent);
        }
    } else {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }
    super.onBackPressed();
}

/**
 * 判断目标activity是否启动
 *
 * @param context
 * @param clazz   传入ACT_main
 * @return
 */
public boolean isLaunchedActivity(@NonNull Context context, Class<?> clazz) {
    Intent intent = new Intent(context, clazz);
    ComponentName cmpName = intent.resolveActivity(context.getPackageManager());
    boolean flag = false;
    if (cmpName != null) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> taskInfoList = am.getRunningTasks(10);
        for (ActivityManager.RunningTaskInfo taskInfo : taskInfoList) {
            if (taskInfo.baseActivity.equals(cmpName)) {
                flag = true;
                break;
            }
        }
    }
    return flag;
}

 

HTML中的代码:

web页面点击按钮唤起App跳转到指定页面以及返回键的处理

结束