android studio常用數(shù)據(jù)傳輸
全局變量
//AppContext? 是工程入口類? 也就是新建個(gè)變量 添加對(duì)應(yīng)的設(shè)置獲取方法而已
public class AppContext extends Application {
? ? /**
? ? ?* 全局變量 調(diào)用方法
? ? ?* AppContext appState = ((AppContext)getApplicationContext());
? ? ?* String state = appState.getState();
? ? ?* appState.setState("88888888888888");
? ? ?*/
? ? private String myState;
? ? public String getState(){
? ? ? ? return myState;
? ? }
? ? public void setState(String s){
? ? ? ? myState = s;
? ? ? }
? ? @Override
? ? public void onCreate() {
? ? ? ? super.onCreate();
? ? }
}
-------------------------------分割線-------------------------------------
Activity 跳轉(zhuǎn)傳值
Intent intent=new Intent(MainActivity.this,Activity2.class);
intent.putExtra("data1", "這里塞你要傳遞的值" );
intent.putExtra("data2", "這里塞你要傳遞的值" );//你可以多傳幾個(gè) 類型也有很多
startActivity(intent);
接收方 activity 寫法?
String data=getIntent().getStringExtra("data1");
String data=getIntent().getStringExtra("data2");
-------------------------------分割線-------------------------------------
Activity 跳轉(zhuǎn)傳輸list? ??
序列化里面的所有類 都要? implements Serializable? 內(nèi)部的也要寫上
//例 這里是用到的bean
? ? public static class SchedulingDataBean implements Serializable {
? ? ? ? private String ProjectID;
? ? ? ? private List<ScheduleEveryDayListBean> ScheduleEveryDayList;
? ? ? ? public String getProjectID() {
? ? ? ? ? ? return ProjectID;
? ? ? ? }
? ? ? ? public void setProjectID(String ProjectID) {
? ? ? ? ? ? this.ProjectID = ProjectID;
? ? ? ? }
? ? ? ? public List<ScheduleEveryDayListBean> getScheduleEveryDayList() {
? ? ? ? ? ? return ScheduleEveryDayList;
? ? ? ? }
? ? ? ? public void setScheduleEveryDayList(List<ScheduleEveryDayListBean> ScheduleEveryDayList) {
? ? ? ? ? ? this.ScheduleEveryDayList = ScheduleEveryDayList;
? ? ? ? }
? ? ? ? public static class ScheduleEveryDayListBean implements Serializable {
? ? ? ? ? ? private String Is_Can_Edit="";
? ? ? ? ? ? public String getIs_Can_Edit() {
? ? ? ? ? ? ? ? return Is_Can_Edit;
? ? ? ? ? ? }
? ? ? ? ? ? public void setIs_Can_Edit(String is_Can_Edit) {
? ? ? ? ? ? ? ? Is_Can_Edit = is_Can_Edit;
? ? ? ? ? ? }
? ? ? ? }
? ? }
傳遞方
SchedulingDataBean bean = new SchedulingDataBean();
List<SchedulingDataBean.ScheduleEveryDayListBean> listBeans = new ArrayList<>();
SchedulingDataBean.ScheduleEveryDayListBean scBean = new SchedulingDataBean.ScheduleEveryDayListBean();
bean.setProjectID("需要傳的內(nèi)容");
scBean.setIs_Can_Edit("子類需要傳遞的東西");
listBeans.add(scBean);
bean.setScheduleEveryDayList(listBeans);
Intent intent = new Intent(DataInsidePagesActivity.this, ComplaintActivity.class);
intent.putExtra("SchedulingDataBeanList", (Serializable) bean);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
接受方
String data = getIntent().getSerializableExtra("SchedulingDataBeanList");
DataInsidePagesBean? schedulingDataBean = gson.fromJson(data, SchedulingDataBean.class);//轉(zhuǎn)一下?
-------------------------------分割線-------------------------------------
輕量級(jí)存儲(chǔ)? 就是把數(shù)據(jù)保存在系統(tǒng)里面? 但是APP被卸載了 這些數(shù)據(jù)也就沒有了,就算普通的數(shù)據(jù)緩存把,不過手機(jī)重啟還是有的
SharedPreferences preferences = getSharedPreferences("user", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("dataString", data);
editor.commit();
讀取
String name = preferences.getString("dataString", "沒有內(nèi)容");// 第二個(gè)參數(shù)是 沒有數(shù)據(jù)的時(shí)候返回的內(nèi)容
-------------------------------分割線-------------------------------------
存儲(chǔ)數(shù)組
就是把數(shù)據(jù)轉(zhuǎn)成String 來存 取出來的時(shí)候 轉(zhuǎn)一下就好
List<String> StringData = new ArrayList<>();
for (int i = 0; i < 50; i++) {
? ? StringData.add(i + "");
}
String data = JsonUtil.toJson(StringData);
SharedPreferences preferences = getSharedPreferences("user", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("dataList", data);
editor.commit();
讀取數(shù)組
String name = preferences.getString("dataList", "沒有內(nèi)容");// 第二個(gè)參數(shù)是 沒有數(shù)據(jù)的時(shí)候返回的內(nèi)容
-------------------------------分割線-------------------------------------
圖片緩存? 這個(gè)比較特殊?
//圖片存儲(chǔ)
? ? private void saveBitmapToSharedPreferences(Bitmap bitmap){
//? ? ? ? Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
? ? ? ? //第一步:將Bitmap壓縮至字節(jié)數(shù)組輸出流ByteArrayOutputStream
? ? ? ? ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
? ? ? ? bitmap.compress(Bitmap.CompressFormat.PNG, 80, byteArrayOutputStream);
? ? ? ? //第二步:利用Base64將字節(jié)數(shù)組輸出流中的數(shù)據(jù)轉(zhuǎn)換成字符串String
? ? ? ? byte[] byteArray=byteArrayOutputStream.toByteArray();
? ? ? ? String imageString=new String(Base64.encodeToString(byteArray, Base64.DEFAULT));
? ? ? ? //第三步:將String保持至SharedPreferences
? ? ? ? SharedPreferences sharedPreferences=getSharedPreferences("testSP", Context.MODE_PRIVATE);
? ? ? ? SharedPreferences.Editor editor=sharedPreferences.edit();
? ? ? ? editor.putString("image", imageString);
? ? ? ? editor.commit();
? ? }
//? ? 拿 圖片緩存
? ?private Bitmap?getBitmapFromSharedPreferences(){
? ? ? ? SharedPreferences sharedPreferences= getSharedPreferences("testSP", Context.MODE_PRIVATE);
? ? ? ? //第一步:取出字符串形式的Bitmap
? ? ? ? String imageString=sharedPreferences.getString("image", "");
? ? ? ? //第二步:利用Base64將字符串轉(zhuǎn)換為ByteArrayInputStream
? ? ? ? byte[] byteArray=Base64.decode(imageString, Base64.DEFAULT);
? ? ? ? ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(byteArray);
? ? ? ? //第三步:利用ByteArrayInputStream生成Bitmap
? ? ? ? Bitmap bitmap=BitmapFactory.decodeStream(byteArrayInputStream);
? ? ? ? if (bitmap == null) { // 設(shè)置拿不到的一個(gè)替代圖 這玩意容易報(bào)錯(cuò)
? ? ? ? ?bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.portrait);
? ? ? ???????}
????????return?bitmap ;
? ? }
-------------------------------分割線-------------------------------------
附帶一個(gè) 工具類吧?
導(dǎo)包? ? ? compile 'com.google.code.gson:gson:2.7'?
public class JsonUtil {
? ? private static Gson gson = null;
? ? static {
? ? ? ? GsonBuilder builder = new GsonBuilder();
? ? ? ? builder.registerTypeAdapter(JsonObject.class, new JsonDeserializer<Object>() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public Object deserialize(JsonElement jsonElement, Type type,
? ? ? ? ? ? ? ? ? ? JsonDeserializationContext jsonDeserializationContext)
? ? ? ? ? ? ? ? ? ? throws JsonParseException {
? ? ? ? ? ? ? ? JsonObject jsonObject = jsonElement.getAsJsonObject();
? ? ? ? ? ? ? ? return jsonObject;
? ? ? ? ? ? }
? ? ? ? });
? ? ? ??
? ? ? ? gson = builder.disableHtmlEscaping().create();
? ? }
? ? public static String toJson(Object object) {
? ? ? ? if (object == null) {
? ? ? ? ? ? return null;
? ? ? ? }
? ? ? ? return gson.toJson(object);
? ? }
? ? public static <T> T fromJson(String content, Class<T> clazz) {
? ? ? ? if (StringUtil.isEmpty(content) || clazz == null) {
? ? ? ? ? ? return null;
? ? ? ? }
? ? ? ? try {
? ? ? ? ? ? return gson.fromJson(content, clazz);
? ? ? ? } catch (JsonSyntaxException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? return null;
? ? ? ? }
? ? }
? ? public static <T> T fromJson(String content, TypeToken<T> token) {
? ? ? ? if (StringUtil.isEmpty(content) || token == null) {
? ? ? ? ? ? return null;
? ? ? ? }
? ? ? ? try {
? ? ? ? ? ? return gson.fromJson(content, token.getType());
? ? ? ? } catch (JsonSyntaxException e) {
? ? ? ? ? ? return null;
? ? ? ? }
? ? }
? ??
? ? /**
? ? ?* 把json轉(zhuǎn)成對(duì)應(yīng)的類型。適合用于自定義數(shù)據(jù)類型,如ArrayList<Foo>等
? ? ?* @param content json
? ? ?* @param type 自定義類型的token。使用方法如下
? ? ?*? ? ? ? ? ? ? Type listType = new TypeToken<ArrayList<Foo>>(){}.getType();
? ? ?* @param <T>
? ? ?* @return 對(duì)應(yīng)類型的對(duì)象
? ? ?*/
? ? public static <T>T fromJson(String content , Type type){
? ? ? ? if(!StringUtil.isEmpty(content) && type != null){
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? return gson.fromJson(content,type);
? ? ? ? ? ? } catch (JsonSyntaxException e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return null;
? ? }
? ? public static Map<String, Object> toMap(Object obj) {
? ? ? ? JsonElement element = gson.toJsonTree(obj);
? ? ? ? return gson.fromJson(element, Map.class);
? ? }
? ? public static <T> T fromObject(Object obj, Class<T> clazz) {
? ? ? ? JsonElement element = gson.toJsonTree(obj);
? ? ? ? return gson.fromJson(element, clazz);
? ? }
? ? public static <T> T fromObject(Object obj, TypeToken<T> token) {
? ? ? ? JsonElement element = gson.toJsonTree(obj);
? ? ? ? return gson.fromJson(element, token.getType());
? ? }
? ? public static Map<String, Object> getMap(Map<String, Object> map, String key) {
? ? ? ? if (map == null || key == null) {
? ? ? ? ? ? return null;
? ? ? ? }
? ? ? ? Object value = map.get(key);
? ? ? ? if (value instanceof Map) {
? ? ? ? ? ? return (Map) value;
? ? ? ? }
? ? ? ? return null;
? ? }
? ? public static Long getLong(Map<String, Object> map, String key) {
? ? ? ? if (map == null || key == null) {
? ? ? ? ? ? return null;
? ? ? ? }
? ? ? ? Object value = map.get(key);
? ? ? ? if (value == null) {
? ? ? ? ? ? return null;
? ? ? ? }
? ? ? ? if (value instanceof Number) {
? ? ? ? ? ? return ((Number) value).longValue();
? ? ? ? }
? ? ? ? try {
? ? ? ? ? ? return Long.parseLong(value.toString());
? ? ? ? } catch (NumberFormatException e) {
? ? ? ? ? ? return null;
? ? ? ? }
? ? }
? ? public static List<Long> getLongList(Map<String, Object> map, String key) {
? ? ? ? if (map == null || key == null) {
? ? ? ? ? ? return Collections.EMPTY_LIST;
? ? ? ? }
? ? ? ? Object value = map.get(key);
? ? ? ? if (value == null) {
? ? ? ? ? ? return Collections.EMPTY_LIST;
? ? ? ? }
? ? ? ? if (value instanceof List) {
? ? ? ? ? ? List<Object> list = (List) value;
? ? ? ? ? ? List<Long> longValues = new ArrayList<Long>();
? ? ? ? ? ? for (Object i : list) {
? ? ? ? ? ? ? ? if (i instanceof Number) {
? ? ? ? ? ? ? ? ? ? longValues.add(((Number) i).longValue());
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? return longValues;
? ? ? ? }
? ? ? ? return Collections.EMPTY_LIST;
? ? }
? ? /**
? ? ?* 從json中搜索,根據(jù)鍵的名字,返回值。
? ? ?* @param json
? ? ?* @param name json中的鍵名
? ? ?* @return Object
? ? ?*/
? ? public static Object findObject(String json , String name){
? ? ? ? Object object = null;
? ? ? ? if(StringUtil.isEmpty(json) || StringUtil.isEmpty(name)){
? ? ? ? ? ? return null;
? ? ? ? }
? ? ? ? try {
? ? ? ? ? ? JSONObject jsonobject = new JSONObject(json);
? ? ? ? ? ? if(!jsonobject.has(name)){
? ? ? ? ? ? ? ? return null;
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? object = jsonobject.get(name);
? ? ? ? ? ? }
? ? ? ? } catch (JSONException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? return object;
? ? }
}