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

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


    
    
        

        
        

        
    

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 taskInfoList = am.getRunningTasks(10);
        for (ActivityManager.RunningTaskInfo taskInfo : taskInfoList) {
            if (taskInfo.baseActivity.equals(cmpName)) {
                flag = true;
                break;
            }
        }
    }
    return flag;
}

 

HTML中的代码:

web页面点击按钮唤起App跳转到指定页面以及返回键的处理_第1张图片

结束 

你可能感兴趣的:(web页面点击按钮唤起App跳转到指定页面以及返回键的处理)