Skip to content

Commit 758ed3c

Browse files
committed
Shared Preferences App
Between App Activity sharing of the data
1 parent bf136d1 commit 758ed3c

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

sharedperferences/MainActivity.java

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.lenovo.sharedperferences;
2+
3+
import android.content.Context;
4+
import android.content.SharedPreferences;
5+
import android.icu.util.Freezable;
6+
import android.support.v7.app.AppCompatActivity;
7+
import android.os.Bundle;
8+
import android.util.Log;
9+
10+
import java.io.IOException;
11+
import java.util.ArrayList;
12+
13+
public class MainActivity extends AppCompatActivity {
14+
15+
@Override
16+
protected void onCreate(Bundle savedInstanceState) {
17+
super.onCreate(savedInstanceState);
18+
setContentView(R.layout.activity_main);
19+
SharedPreferences sharedPreferences = this.getSharedPreferences("com.lenovo.sharedperferences", Context.MODE_PRIVATE);
20+
//sharedPreferences.edit().putString("username","ashlesh").apply();
21+
//String username= sharedPreferences.getString("username","");
22+
23+
ArrayList<String> friends= new ArrayList<String>();
24+
friends.add("Monica");
25+
friends.add("Chandler");
26+
friends.add("Rachel");
27+
friends.add("Ross");
28+
friends.add("Joey");
29+
friends.add("Pheobe");
30+
try {
31+
sharedPreferences.edit().putString("friends",ObjectSerializer.serialize(friends)).apply();
32+
} catch (IOException e) {
33+
e.printStackTrace();
34+
}
35+
//Log.i("Username",username);
36+
ArrayList<String> newfriends = new ArrayList<>();
37+
try {
38+
newfriends=(ArrayList<String>) ObjectSerializer.deserialize(sharedPreferences.getString("friends",ObjectSerializer.serialize(new ArrayList<String>())));
39+
} catch (IOException e) {
40+
e.printStackTrace();
41+
}
42+
43+
Log.i("firends",newfriends.toString());
44+
}
45+
}
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.lenovo.sharedperferences;
2+
3+
import android.util.Log;
4+
5+
import java.io.ByteArrayInputStream;
6+
import java.io.ByteArrayOutputStream;
7+
import java.io.IOException;
8+
import java.io.ObjectInputStream;
9+
import java.io.ObjectOutputStream;
10+
import java.io.Serializable;
11+
12+
13+
public class ObjectSerializer {
14+
15+
16+
public static String serialize(Serializable obj) throws IOException {
17+
if (obj == null) return "";
18+
try {
19+
ByteArrayOutputStream serialObj = new ByteArrayOutputStream();
20+
ObjectOutputStream objStream = new ObjectOutputStream(serialObj);
21+
objStream.writeObject(obj);
22+
objStream.close();
23+
return encodeBytes(serialObj.toByteArray());
24+
} catch (Exception e) {
25+
throw new RuntimeException(e);
26+
}
27+
}
28+
29+
public static Object deserialize(String str) throws IOException {
30+
if (str == null || str.length() == 0) return null;
31+
try {
32+
ByteArrayInputStream serialObj = new ByteArrayInputStream(decodeBytes(str));
33+
ObjectInputStream objStream = new ObjectInputStream(serialObj);
34+
return objStream.readObject();
35+
} catch (Exception e) {
36+
throw new RuntimeException(e);
37+
}
38+
}
39+
40+
public static String encodeBytes(byte[] bytes) {
41+
StringBuffer strBuf = new StringBuffer();
42+
43+
for (int i = 0; i < bytes.length; i++) {
44+
strBuf.append((char) (((bytes[i] >> 4) & 0xF) + ((int) 'a')));
45+
strBuf.append((char) (((bytes[i]) & 0xF) + ((int) 'a')));
46+
}
47+
48+
return strBuf.toString();
49+
}
50+
51+
public static byte[] decodeBytes(String str) {
52+
byte[] bytes = new byte[str.length() / 2];
53+
for (int i = 0; i < str.length(); i+=2) {
54+
char c = str.charAt(i);
55+
bytes[i/2] = (byte) ((c - 'a') << 4);
56+
c = str.charAt(i+1);
57+
bytes[i/2] += (c - 'a');
58+
}
59+
return bytes;
60+
}
61+
62+
}

0 commit comments

Comments
 (0)