Application类,当应用程序启动的时候,系统将会对这个类初始化,可以定制一个Application类,管理程序全局状态信息,如Context
定制Application
1 package com.example.contexttest; 2 3 import android.app.Application; 4 import android.content.Context; 5 public class MyApplication extends Application { 6 private static Context context; 7 8 @Override 9 public void onCreate() { 10 context = getApplicationContext(); 11 } 12 13 public static Context getContext(){ 14 return context; 15 } 16 17 }
使用全局Context
1 package com.example.contexttest; 2 3 import java.io.BufferedReader; 4 import java.io.InputStream; 5 import java.io.InputStreamReader; 6 import java.net.HttpURLConnection; 7 import java.net.URL; 8 9 import android.widget.Toast; 10 11 public class HttpUtil { 12 public static void sendHttp(final String address, 13 final HttpListener listener) { 14 // 在这里我们判断一下网络是否可用,如果不可用,则弹出提示 15 // 问题来了,Toast需要传递Context,在哪里去找一个Context?——通过自定义application 16 if(!isNetWorkAvailable()){ 17 Toast.makeText(MyApplication.getContext(), "network is unavailable", Toast.LENGTH_SHORT).show(); 18 } 19 20 new Thread(new Runnable() { 21 HttpURLConnection connection = null; 22 23 @Override 24 public void run() { 25 try { 26 // 获取HttpURLConnection对象 27 URL url = new URL(address); 28 connection = (HttpURLConnection) url.openConnection(); 29 30 // 设置请求方式和延迟 31 connection.setRequestMethod("GET"); 32 connection.setConnectTimeout(8000); 33 connection.setReadTimeout(8000); 34 connection.setDoInput(true); 35 connection.setDoOutput(true); 36 37 // 请求数据,疯狂的读 38 StringBuilder response = new StringBuilder(); 39 InputStream in = connection.getInputStream(); 40 BufferedReader bufr = new BufferedReader(new InputStreamReader(in)); 41 String line = null; 42 while((line=bufr.readLine())!=null){ 43 response.append(line); 44 } 45 46 // 读完之后通过监听器操作数据 47 if(listener!=null){ 48 listener.onFinish(response.toString()); 49 } 50 51 52 } catch (Exception e) { 53 listener.onError(e); 54 } finally { 55 if (connection != null) { 56 connection.disconnect(); 57 } 58 } 59 } 60 }).start(); 61 } 62 }
intent除了可以传递常见的数据类型如int,boolean等等,但是不能直接传递对象,要传递对象,通常有两种做法
序列化很简单,只需要让类实现Serializable接口就可以了,并且这个接口一个方法都没有!!!仅仅相当于是添加一个标记一样!
1 package com.example.intenttest; 2 3 import java.io.Serializable; 4 5 public class People implements Serializable { 6 private String Name; 7 8 public People() { 9 } 10 11 public People(String name) { 12 super(); 13 Name = name; 14 } 15 16 public String getName() { 17 return Name; 18 } 19 20 }
传入:
1 Intent intent = new Intent(MainActivity.this,SecondActivity.class); 2 People people = new People("秋香"); 3 intent.putExtra("people", people); 4 startActivity(intent);
取出:
People people = (People) getIntent().getSerializableExtra("people");
textView.setText("Serializable:"+people.getName()+"\n");
Serializable方式会将整个对象序列化,效率上会比parcelable稍低
1 package com.example.intenttest; 2 3 import android.os.Parcel; 4 import android.os.Parcelable; 5 6 public class Dog implements Parcelable { 7 private String name; 8 private int age; 9 10 public Dog() {} 11 public Dog(String name, int age) { 12 super(); 13 this.name = name; 14 this.age = age; 15 } 16 17 public String getName() { 18 return name; 19 } 20 21 public int getAge() { 22 return age; 23 } 24 25 /** 26 * 返回0即可 27 */ 28 @Override 29 public int describeContents() { 30 // TODO Auto-generated method stub 31 return 0; 32 } 33 34 /** 35 * 将字段一一写出 36 */ 37 @Override 38 public void writeToParcel(Parcel dest, int flags) { 39 dest.writeString(name); 40 dest.writeInt(age); 41 42 } 43 44 /** 45 * Parcelable方式必须提供一个CREATOR常量,传入泛型类名 46 */ 47 public static final Parcelable.Creator<Dog> CREATOR = new Creator<Dog>() { 48 49 /** 50 * 指定数组大小 51 */ 52 @Override 53 public Dog[] newArray(int size) { 54 // TODO Auto-generated method stub 55 return new Dog[size]; 56 } 57 58 /** 59 * 按照写入的顺序一一读取 60 */ 61 @Override 62 public Dog createFromParcel(Parcel source) { 63 // TODO Auto-generated method stub 64 Dog dog = new Dog(); 65 dog.name = source.readString(); 66 dog.age = source.readInt(); 67 return dog; 68 } 69 }; 70 }
存入:
1 Intent intent = new Intent(MainActivity.this,SecondActivity.class); 2 Dog dog = new Dog("旺财", 8); 3 intent.putExtra("dog", dog); 4 startActivity(intent);
取出:
1 Dog dog = getIntent().getParcelableExtra("dog"); 2 textView.setText("Parcelable:"+dog.getName()+"::"+dog.getAge()+"\n");
Parcelable稍微复杂一点,不过效率稍微高一点,更推荐使用
原文:http://www.cnblogs.com/erhai/p/4953720.html