我想保存一个Intent对象,该对象具有与之关联的属性集,然后检索它以便我只需要编写
startActivity(intentObject);
这样做的最佳做法是什么? 我已经阅读了有关Google Gson和对象序列化/ parcelable实现的内容,但无法对其进行处理。
示例场景:
活动1
Intent abc = new Intent ();
abc.setClass(); //set class here
abc.setExtra(); //set some extras
其他一些活动
//code to retrieve abc Intent object
PS。 - 我通过意图了解活动之间的沟通,但这不是两个活动之间的沟通。
编辑1
请记住,其他用户正在设置意图,因此我并不确切知道与之相关的属性。将它转换为Json String或Serializing / make it Parcelable也不是我想要的。有什么想法吗?
编辑2
我到目前为止尝试的是使用Jackson library将该Intentobject转换为Json,然后通过解析该json来设置新的intent。我担心的是丢失一些可能无法转换为Json的重要属性。
谢谢,
山塔努
答案 0 :(得分:0)
正如您的评论所说,是的,您可以使Intent对象全局可访问,但这并不是正确的方法。你不知道何时可以再次调用它。虽然它只是等待"在那里,它使用内存,如果应用程序关闭,它就是一个临时解决方案。
我不会称之为最好的"方式,但它的工作原理:
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putString("class_name", SomeOtherActivity.class.getSimpleName());
// add your other properties here
editor.apply();
// then continue with your work, maybe start the new activity
}
});
然后从其他地方,你可以这样做:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext();
String className = preferences.getString("class_name", "");
// get your other properties here
if(!className.isEmpty()) {
try {
Intent intent = new Intent(this, Class.forName(className));
// add the retreived properties here
startActivity(intent);
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
答案 1 :(得分:0)
经过多天的研究,我知道虽然有许多将Intent转换为字节流的方法,但是没有适当的方法来存储和检索具有与之相关的外来(对我们)属性的intent对象。 以下是我自己的逻辑。我首先使用Jackson将intentobcject转换为json字符串,然后我将解析json并将属性设置为新的intent对象。
对于json的意图:
public String intentToJSON(Context con,Intent intent) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
String getClassName = null;
getClassName = intent.getComponent().getClassName();
String getContextName = null;
getContextName = con.getClass().getName() + ".this";
HashMap<String, String> makeInsideJsonArray = new HashMap<String, String>();
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("className",getClassName);
hashMap.put("context",getContextName);
Bundle bundle = intent.getExtras();
if (bundle != null) {
Set<String> keys = bundle.keySet();
Iterator<String> it = keys.iterator();
Log.d("SappsnoopDog",""+keys.size());
while (it.hasNext()) {
String key = it.next();
Log.d("Sapptagdog","TYPE " + bundle.get(key).toString());
Log.d("NERVE",""+bundle.get(key).getClass().getAnnotations());
String type = bundle.get(key).getClass().getSimpleName();
Log.d("SappDogTAG",key + " OF TYPE " + type);
switch (type) {
case "String":
makeInsideJsonArray.put(key,type+ LibraryDatabase.JSONSEPERATOR +bundle.get(key).toString());
Log.d("SappDogTAG","bool array");
break;
case "String[]":
makeInsideJsonArray.put(key,type+LibraryDatabase.JSONSEPERATOR+bundle.get(key).toString().replace(" ",""));
Log.d("SappDogTAG","bool array");
break;
case "Integer":
makeInsideJsonArray.put(key,type+LibraryDatabase.JSONSEPERATOR+bundle.get(key).toString());
Log.d("SappDogTAG","bool array");
break;
case "Double":
makeInsideJsonArray.put(key,type+LibraryDatabase.JSONSEPERATOR+bundle.get(key).toString());
Log.d("SappDogTAG","bool array");
break;
case "double[]":
double[] newDouble = (double[]) bundle.get(key);
String fromDouble = Arrays.toString(newDouble);
makeInsideJsonArray.put(key,type+LibraryDatabase.JSONSEPERATOR+fromDouble.replace(" ",""));
Log.d("SappDogTAG","bool array");
break;
case "int[]":
int[] newArray = (int[]) bundle.get(key);
String fromArray = Arrays.toString(newArray);
makeInsideJsonArray.put(key,type+LibraryDatabase.JSONSEPERATOR+fromArray.replace(" ",""));
Log.d("SappDogTAG","bool array");
break;
case "Boolean":
makeInsideJsonArray.put(key,type+LibraryDatabase.JSONSEPERATOR+bundle.get(key).toString());
Log.d("SappDogTAG","bool array");
break;
case "boolean[]":
boolean[] newBool = (boolean[]) bundle.get(key);
String fromBool = Arrays.toString(newBool);
makeInsideJsonArray.put(key,type+LibraryDatabase.JSONSEPERATOR+fromBool.replace(" ",""));
Log.d("SappDogTAG","bool array");
break;
case "Char":
makeInsideJsonArray.put(key,type+LibraryDatabase.JSONSEPERATOR+bundle.get(key).toString());
Log.d("SappDogTAG","bool array");
break;
case "char[]":
char[] newChar = (char[]) bundle.get(key);
String fromChar = Arrays.toString(newChar);
makeInsideJsonArray.put(key,type+LibraryDatabase.JSONSEPERATOR+fromChar.replace(" ",""));
Log.d("SappDogTAG","bool array");
break;
case "CharSequence":
makeInsideJsonArray.put(key,type+LibraryDatabase.JSONSEPERATOR+bundle.get(key).toString());
Log.d("SappDogTAG","bool array");
break;
case "charsequence[]":
makeInsideJsonArray.put(key,type+LibraryDatabase.JSONSEPERATOR+bundle.get(key).toString().replace(" ",""));
Log.d("SappDogTAG","bool array");
break;
case "Byte":
makeInsideJsonArray.put(key,type+LibraryDatabase.JSONSEPERATOR+bundle.get(key).toString());
Log.d("SappDogTAG","bool array");
break;
case "byte[]":
byte[] newByte = (byte[]) bundle.get(key);
String fromByte = Arrays.toString(newByte);
makeInsideJsonArray.put(key,type+LibraryDatabase.JSONSEPERATOR+fromByte.replace(" ",""));
Log.d("SappDogTAG","bool array");
break;
case "Float":
makeInsideJsonArray.put(key,type+LibraryDatabase.JSONSEPERATOR+bundle.get(key).toString());
Log.d("SappDogTAG","bool array");
break;
case "float[]":
float[] newFloat = (float[]) bundle.get(key);
String fromFloat = Arrays.toString(newFloat);
makeInsideJsonArray.put(key,type+LibraryDatabase.JSONSEPERATOR+fromFloat.replace(" ",""));
Log.d("SappDogTAG","bool array");
break;
case "Short":
makeInsideJsonArray.put(key,type+LibraryDatabase.JSONSEPERATOR+bundle.get(key).toString());
Log.d("SappDogTAG","bool array");
break;
case "short[]":
short[] newShort = (short[]) bundle.get(key);
String fromShort = Arrays.toString(newShort);
fromShort = fromShort.replace(" ","");
makeInsideJsonArray.put(key,type+LibraryDatabase.JSONSEPERATOR+fromShort.replace(" ",""));
Log.d("SappDogTAG","bool array");
break;
case "Long":
makeInsideJsonArray.put(key,type+LibraryDatabase.JSONSEPERATOR+bundle.get(key).toString());
Log.d("SappDogTAG","bool array");
break;
case "long[]":
long[] newLong = (long[]) bundle.get(key);
String fromLong = Arrays.toString(newLong);
makeInsideJsonArray.put(key,type+LibraryDatabase.JSONSEPERATOR+bundle.get(key).toString().replace(" ",""));
Log.d("SappDogTAG","bool array");
break;
case "ArrayList":
ArrayList<Object> obj = (ArrayList<Object>) bundle.get(key);
Object[] objArr = obj.toArray();
if(objArr[0] instanceof Integer){
ArrayList<Integer> newIntegerArray = bundle.getIntegerArrayList(key);
makeInsideJsonArray.put(key,type+"Integer"+LibraryDatabase.JSONSEPERATOR+newIntegerArray.toString().replace(" ",""));
}else if(objArr[0] instanceof String){
ArrayList<String> newStringArray = bundle.getStringArrayList(key);
makeInsideJsonArray.put(key,type+"String"+LibraryDatabase.JSONSEPERATOR+newStringArray.toString().replace(" ",""));
}
break;
default:
// whatever
}
hashMap.put(key, bundle.get(key).toString());
}
}
String passArray = mapper.writeValueAsString(makeInsideJsonArray);
hashMap.put("intentExtras",passArray);
Log.d("GOGTAD",""+passArray);
String intentString = mapper.writeValueAsString(intent);
Log.d("IntentString", "" + mapper.writeValueAsString(hashMap));
StringBuilder a1S = new StringBuilder(mapper.writeValueAsString(hashMap));
a1S.deleteCharAt(mapper.writeValueAsString(hashMap).length()-1);
a1S.append(",");
String s1t = a1S.toString();
StringBuilder sb = new StringBuilder(intentString);
sb.deleteCharAt(0);
String retrString = sb.toString();
StringBuilder newS = new StringBuilder();
newS.append(s1t);
newS.append(retrString);
Log.d("Insnsns",newS.toString());
return newS.toString();
}
和json的意图:
public Intent jsonToINTENT(String JSONString) throws JSONException {
JSONObject jsonObject = new JSONObject(JSONString.toString());
String toArray = jsonObject.get("intentExtras").toString();
String contextName = "com.hanuor.sapphiredemo";
String className = jsonObject.get("className").toString();
Log.d("Insass",className.toString());
Intent setIntent = new Intent();
setIntent.setClassName(contextName, className);
HashMap<String, String> extrasHash = new HashMap<String, String>();
JSONObject issueObj = new JSONObject(toArray);
for (int i = 0; i < issueObj.length(); i++) {
extrasHash.put(issueObj.names().getString(i), issueObj.get(issueObj.names().getString(i)).toString());
}
Iterator it = extrasHash.entrySet().iterator();
while (it.hasNext()) {
//add conditions and checks here
Map.Entry pair = (Map.Entry) it.next();
String currentKey = (String) pair.getKey();
Log.d("HAHA",""+currentKey);
String[] getValuethroughSplit = pair.getValue().toString().split(LibraryDatabase.JSONSEPERATOR);
String dataType = getValuethroughSplit[0];
String value = (String) getValuethroughSplit[2];
Log.d("Insamareen",getValuethroughSplit.length + " " +dataType+ " " +value.toString());
switch (dataType) {
case "String":
setIntent.putExtra(currentKey,(String) value);
break;
case "String[]":
String comp1 = value.substring(1,value.length()-1);
String[] comp2 = comp1.split(",");
setIntent.putExtra(currentKey,comp2);
break;
case "Integer":
setIntent.putExtra(currentKey,Integer.parseInt(value));
break;
case "Double":
setIntent.putExtra(currentKey,Double.parseDouble(value));
break;
case "double[]":
String compDouble1 = value.substring(1,value.length()-1);
String[] compDoub2 = compDouble1.split(",");
double[] db = new double[compDoub2.length];
for(int i = 0; i<compDoub2.length; i++){
db[i] = Double.parseDouble(compDoub2[i]);
}
setIntent.putExtra(currentKey,db);
break;
case "int[]":
String compInt1 = value.substring(1,value.length()-1);
String[] compInt2 = compInt1.split(",");
int[] intVal = new int[compInt2.length];
for(int i = 0; i<compInt2.length; i++){
intVal[i] = Integer.parseInt(compInt2[i]);
}
Log.d("Hankey",intVal.toString());
setIntent.putExtra(currentKey,intVal);
break;
case "Boolean":
setIntent.putExtra(currentKey,Boolean.valueOf(value));
break;
case "boolean[]":
String compB1 = value.substring(1,value.length()-1);
String[] compB2 = compB1.split(",");
boolean[] BVal = new boolean[compB2.length];
for(int i = 0; i<compB2.length; i++){
BVal[i] =Boolean.parseBoolean(compB2[i]);
}
setIntent.putExtra(currentKey, value);
break;
case "Char":
setIntent.putExtra(currentKey,value);
break;
case "char[]":
String ch1 = value.substring(1,value.length()-1);
String[] ch2 = ch1.split(",");
String newS = null;
for(int i = 0; i<ch2.length; i++){
newS = newS + ch2[i];
}
setIntent.putExtra(currentKey,newS.toCharArray());
break;
case "CharSequence":
setIntent.putExtra(currentKey,(CharSequence) value);
break;
case "Charsequence[]":
setIntent.putExtra(currentKey,value.toString());
break;
case "Byte":
setIntent.putExtra(currentKey,Byte.valueOf(value));
break;
case "byte[]":
String by = value.substring(1,value.length()-1);
String[] by2 = by.split(",");
byte[] by3 = new byte[by2.length];
for(int i = 0; i<by2.length; i++){
by3[i] =Byte.parseByte(by2[i]);
}
setIntent.putExtra(currentKey,by3);
break;
case "Float":
setIntent.putExtra(currentKey,Float.valueOf(value));
break;
case "float[]":
String fl = value.substring(1,value.length()-1);
String[] fl2 = fl.split(",");
float[] fl3 = new float[fl2.length];
for(int i = 0; i<fl2.length; i++){
fl3[i] =Float.parseFloat(fl2[i]);
}
setIntent.putExtra(currentKey,fl3);
break;
case "Short":
setIntent.putExtra(currentKey,Short.valueOf(value));
break;
case "short[]":
String sh = value.substring(1,value.length()-1);
String[] sh2 = sh.split(",");
short[] sh3 = new short[sh2.length];
for(int i = 0; i<sh2.length; i++){
sh3[i] =Short.parseShort(sh2[i]);
}
setIntent.putExtra(currentKey,sh3);
break;
case "Long":
setIntent.putExtra(currentKey,Long.valueOf(value));
break;
case "long[]":
String ll = value.substring(1,value.length()-1);
String[] ll2 = ll.split(",");
long[] ll3 = new long[ll2.length];
for(int i = 0; i<ll2.length; i++){
ll3[i] =Long.parseLong(ll2[i]);
}
setIntent.putExtra(currentKey,ll3);
break;
case "ArrayListString":
Log.d("Hankey",currentKey+" ");
String arrL = value.substring(1,value.length()-1);
String[] arrl2 = arrL.split(",");
ArrayList<String> arrStr = new ArrayList<String>();
for (int i = 0; i < arrl2.length; i++) {
arrStr.add(arrl2[i]);
}
setIntent.putStringArrayListExtra(currentKey, arrStr);
break;
case "ArrayListInteger":
String arL = value.substring(1,value.length()-1);
String[] arl2 = arL.split(",");
ArrayList<Integer> arrInt = new ArrayList<Integer>();
for(int i = 0; i<arl2.length; i++){
arrInt.add(Integer.parseInt(arl2[i]));
}
setIntent.putIntegerArrayListExtra(currentKey,arrInt);
break;
default:
// whatever
}
}
return setIntent;
}
在解析Json时还有很多东西要实现。我这样做只是为了设置相关的值。