RouterUtil.java 2.54 KB
package com.chudiangameplay.android.util;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.chudiangameplay.android.MyApplication;
import com.chudiangameplay.android.bean.RouterParamBean;
import com.chudiangameplay.android.tool.CommToast;
import com.chudiangameplay.android.tool.Log;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

/**
 * Created by Administrator on 2018/11/27.
 */

public class RouterUtil {

    public static void startActivity(Context mContext, String param) {
        //
        Log.i("param:" + param);
        try {
            RouterParamBean pushBean = new Gson().fromJson(param, RouterParamBean.class);

            String arr[] = pushBean.androidRoute.split("\\?");
            //链接
            String className = arr[0].replace("/khpw/", "");
            //参数
            Bundle bundle = new Bundle();
            if (arr.length > 1) {
                String[] arrParam = arr[1].split("&");   // title=11
                for (int i = 0; i < arrParam.length; i++) {
                    if (!TextUtils.isEmpty(arrParam[i])) {
                        String[] keyvalue = arrParam[i].split("=");
                        String key = keyvalue[0];
                        String value = keyvalue[1];
                        bundle.putString(key, value);
                    }
                }
//                String paramText = arr[1].replace("params=", "");
//                try {
//                    JSONObject jsonObject = new JSONObject(paramText);
////                    jsonObject.keys()
//                    //迭代器用于while循环
//                    Iterator iter = jsonObject.keys();
//                    while (iter.hasNext()) {
//                        String key = (String) iter.next();
//                        String value = jsonObject.getString(key);
//                        bundle.putString(key, value);
//                    }
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
            }

            String fullText = MyApplication.context.getPackageName() + ".ui.activity." + className;
            Intent intent = new Intent();
            intent.setClassName(MyApplication.context.getPackageName(), fullText);
            intent.putExtras(bundle);
            mContext.startActivity(intent);
        } catch (Exception e) {
            CommToast.showToast(MyApplication.context, "配置错误");
        }
    }
}