1.1 背景和目的
XX项目,需要实现点击模型,启动应用(海报、广告、网页、下载等)。
本文档定义json协议,实现多种方式启动应用,支持多种传参方式。
1.2 范围
本文适合全部应用开发人员和系统整合人员。
2 启动应用方式/参数
Intent构造的两种方式:指定Action名,和指定组件名。
Intent启动的两种方式:启动组件,和发送广播。
Intent传参的三种方式:Extra传参,Bundle传参,和Data传参。
其中,两种构造方式可并用,三种传参方式也可并用。
2.1 Intent构造的两种方式
2.1.1 指定Action名
Intent intent = new Intent(); intent.setAction(action); |
2.1.2 指定组件名
Intent intent = new Intent(); intent.setClassName(packageName, activityName); |
2.2 Intent启动的两种方式
2.2.1 启动Activity
context.startActivity(intent); |
2.2.2 发送广播
context.sendBroadcast(intent); |
2.3 Intent传参的三种方式
2.3.1 Extra传参
intent.putExtra(“name”, “Hello”); intent.putExtra(“age”, 10); |
2.3.2 Bundle传参
Bundle bundle = new Bundle(); bundle.putFloat(“pie”, 3.14f); bundle.putBoolean(“isChild”, true); intent.putExtras(bundle); |
2.3.3 Data传参
intent.setData(Uri.parse(“http://www.csdn.com”)); |
3 Json协议
3.1 样例
{ "behavior":"activity", "action":"", "packagename":"com.example.packagenameactivityname", "activityname":"com.example.packagenameactivityname.ParamsActivity", "extramap":[ { "key":"name", "value":"Hello Json", "type":"str" }, { "key":"age", "value":"15", "type":"int" } ], "bundlemap":[ { "key":"pie", "value":"3.1415926", "type":"float" }, { "key":"isExit", "value":"false", "type":"boolean" } ], "uri":"https://www.cnbeta.com" } |
3.2 字段名解释
1)behavior: activity,或者broadcast
2)action:Action名
3)packagename :包名
4)activityname:活动名
5)extramap: extra参数
6)bundlemap: bundlemap参数
7)uri:Data参数
3.3 参数类型解释
3.3.1 Extra参数对应关系
putExtra(String name, double[] value) | { "key":"name", "value":[value0, value1, value2,…], "type":"double[]" } |
putExtra(String name, int value) | { "key":"name", "value":value, "type":"int" } |
putExtra(String name, CharSequence value) | { "key":"name", "value":value, "type":" CharSequence" } |
putExtra(String name, char value) | { "key":"name", "value":value, "type":"char" } |
putExtra(String name, Bundle value) | { "key":"name", "value":[ { "key":"pie", "value":"3.1415926", "type":"float" }, { "key":"isExit", "value":"false", "type":"boolean" } ], "type":"Bundle" } |
putExtra(String name, Parcelable[] value) | 暂不支持 |
putExtra(String name, Serializable value) | 暂不支持 |
putExtra(String name, int[] value) | { "key":"name", "value":[value0, value1, value2,…], "type":"int[]" } |
putExtra(String name, float value) |
|
putExtra(String name, byte[] value) | { "key":"name", "value":[value0, value1, value2,…], "type":"byte[]" } |
putExtra(String name, long[] value) | { "key":"name", "value":[value0, value1, value2,…], "type":"long[]" } |
putExtra(String name, Parcelable value) | 暂不支持 |
putExtra(String name, float[] value) | { "key":"name", "value":[value0, value1, value2,…], "type":"float[]" } |
putExtra(String name, long value) | { "key":"name", "value":value, "type":"long" } |
putExtra(String name, String[] value) | { "key":"name", "value":[value0, value1, value2,…], "type":"String[]" } |
putExtra(String name, boolean value) | { "key":"name", "value":value, "type":"boolean" } |
putExtra(String name, boolean[] value) | { "key":"name", "value":[value0, value1, value2,…], "type":"boolean[]" } |
putExtra(String name, short value) | { "key":"name", "value":value, "type":"short" } |
putExtra(String name, double value) | { "key":"name", "value":value, "type":"double" } |
putExtra(String name, short[] value) | { "key":"name", "value":[value0, value1, value2,…], "type":"short[]" } |
putExtra(String name, String value) | { "key":"name", "value":value, "type":"String" } |
putExtra(String name, byte value) | { "key":"name", "value":value, "type":"byte" } |
putExtra(String name, char[] value) | { "key":"name", "value":[value0, value1, value2,…], "type":"char[]" } |
putExtra(String name, CharSequence[] value) | { "key":"name", "value":[value0, value1, value2,…], "type":"CharSequence[]" } |
putExtras(Intent src) | 暂不支持 |
putExtras(Bundle extras) | "bundlemap":[ { "key":"pie", "value":"3.1415926", "type":"float" }, { "key":"isExit", "value":"false", "type":"boolean" } ] |
putIntegerArrayListExtra(String name, ArrayList<Integer> value) | { "key":"name", "value":[value0, value1, value2,…], "type":"ArrayList<Integer>" } |
putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value) | 暂不支持 |
putStringArrayListExtra(String name, ArrayList<String> value) | { "key":"name", "value":[value0, value1, value2,…], "type":"ArrayList<String>" } |
putCharSequenceArrayListExtra(String name, ArrayList<CharSequence> value) | { "key":"name", "value":[value0, value1, value2,…], "type":"ArrayList<CharSequence>" } |
3.3.2 Bundle参数对应关系
putAll(Bundle map) | 暂不支持 |
putBoolean(String key, boolean value) | { "key":"name", "value":value, "type":"boolean" } |
putBooleanArray(String key, boolean[] value) | { "key":"name", "value":[value0, value1, value2,…], "type":"boolean[]" } |
putBundle(String key, Bundle value) | { "key":"name", "value":[ { "key":"pie", "value":"3.1415926", "type":"float" }, { "key":"isExit", "value":"false", "type":"boolean" } ], "type":"Bundle" } |
putByte(String key, byte value) | { "key":"name", "value":value, "type":"byte" } |
putByteArray(String key, byte[] value) | { "key":"name", "value":[value0, value1, value2,…], "type":"byte[]" } |
putChar(String key, char value) | { "key":"name", "value":value, "type":"char" } |
putCharArray(String key, char[] value) | { "key":"name", "value":[value0, value1, value2,…], "type":"char[]" } |
putCharSequence(String key, CharSequence value) | { "key":"name", "value":value, "type":"CharSequence" } |
putCharSequenceArray(String key, CharSequence[] value) | { "key":"name", "value":[value0, value1, value2,…], "type":"CharSequence[]" } |
putCharSequenceArrayList(String key, ArrayList<CharSequence> value) | { "key":"name", "value":[value0, value1, value2,…], "type":"ArrayList<CharSequence>" } |
putDouble(String key, double value) | { "key":"name", "value":value, "type":"doudle" } |
putDoubleArray(String key, double[] value) | { "key":"name", "value":[value0, value1, value2,…], "type":"double[]" } |
putFloat(String key, float value) | { "key":"name", "value":value, "type":"float" } |
putFloatArray(String key, float[] value) | { "key":"name", "value":[value0, value1, value2,…], "type":"float[]" } |
putInt(String key, int value) | { "key":"name", "value":value, "type":"int" } |
putIntArray(String key, int[] value) | { "key":"name", "value":[value0, value1, value2,…], "type":"int[]" } |
putIntegerArrayList(String key, ArrayList<Integer> value) | { "key":"name", "value":[value0, value1, value2,…], "type":"ArrayList<Integer>" } |
putLong(String key, long value) | { "key":"name", "value":value, "type":"long" } |
putLongArray(String key, long[] value) | { "key":"name", "value":[value0, value1, value2,…], "type":"long[]" } |
putParcelable(String key, Parcelable value) | 暂不支持 |
putParcelableArray(String key, Parcelable[] value) | 暂不支持 |
putParcelableArrayList(String key, ArrayList<? extends Parcelable> value) | 暂不支持 |
putSerializable(String key, Serializable value) | 暂不支持 |
putShort(String key, short value) | { "key":"name", "value":value, "type":"short" } |
putShortArray(String key, short[] value) | { "key":"name", "value":[value0, value1, value2,…], "type":"short[]" } |
putSparseParcelableArray(String key, SparseArray<? extends Parcelable> value) | 暂不支持 |
putString(String key, String value) | { "key":"name", "value":value, "type":"String" } |
putStringArray(String key, String[] value) | { "key":"name", "value":[value0, value1, value2,…], "type":"String[]" } |
putStringArrayList(String key, ArrayList<String> value) | { "key":"name", "value":[value0, value1, value2,…], "type":"ArrayList<String>" } |