项目部分总结:
Application
写一个AppApplication继承Application
package com.redcircle.redcircle;
import android.app.Application;
import android.content.Context;
/**
* Created by Administrator on 2015/10/10.
*/
public class AppApplication extends Application {
private static AppApplication mApplication;
@Override
public void onCreate() {
super.onCreate();
//程序初始化
mApplication = this;
DbManager.newInstance().getDb();
}
public static AppApplication getApplication(){
return mApplication;
}
}
BaseActivity
写一个BaseActivity继承AppCompatActivity,实现他的方法,然后所有的Activity都继承BaseActivity。
package com.redcircle.redcircle;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
/**
* Created by Administrator on 2015/10/10.
*/
public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onPostResume() {
super.onPostResume();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
BaseFragment
写一个BaseFragment 继承Fragment,实现他的方法。
package com.redcircle.redcircle;
import android.app.Fragment;
import android.os.Bundle;
/**
* Created by Administrator on 2015/10/10.
*/
public class BaseFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onStop() {
super.onStop();
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
网络连接部分
网络连接部分写一个包Http,所有关于网络连接的类都放在这个包里,
网络连接的类MyHttp
package com.redcircle.redcircle.Http;
import android.app.DownloadManager;
import android.content.Context;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.redcircle.redcircle.AppApplication;
import com.redcircle.redcircle.utils.NetWorkUtils;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Administrator on 2015/10/10.
* 所有的网络连接全部使用此类,此类的作用是统一网络连接的接口
*/
public class MyHttp {
//单例设计模式
private MyHttp(){}
private static MyHttp mMyHttp;
public synchronized static MyHttp newInstance(){
if (mMyHttp == null){
mMyHttp = new MyHttp();
}
return mMyHttp;
}
//此处是观察者模式,监听网络连接
public interface OnHTTPConnectionListener{
//无网络可用
public void NullInternet();
//网络连接失败
public void FailConnection(int errocode);
//成功
public void SuccessConnection(String response);
}
public void addRequest(String url, HashMap<String, String> params,final OnHTTPConnectionListener onHTTPListener){
//无网络连接时调用
if (!NetWorkUtils.isConnection()){
onHTTPListener.NullInternet();
return;
}
RequestQueue queue = Volley.newRequestQueue(AppApplication.getApplication());
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
onHTTPListener.SuccessConnection(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
onHTTPListener.NullInternet();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap hashMap=params;
return hashMap;
}
};
queue.add(stringRequest);
// MyHttp.newInstance(new AppApplication().getmContext()).
}
}
专门存放特定字符串的类NetWorkConfig,比如URL
package com.redcircle.redcircle.Http;
/**
* Created by Administrator on 2015/10/10.
*/
public class NetWorkConfig {
public static final String URL = "http://www.baidu.com";
}
区分各部分网络连接的方法,比如登录、注册
package com.redcircle.redcircle.Http;
import java.util.HashMap;
/**
* Created by Administrator on 2015/10/10.
*/
public class MyAllHttpMethod {
//需要加单例
public static void login(){
}
public static void regester(){
}
}
还要判断是否有网络可用,如果没有,提示打开WiFi或数据
package com.redcircle.redcircle.utils;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import com.redcircle.redcircle.AppApplication;
/**
* Created by Administrator on 2015/10/10.
*/
public class NetWorkUtils {
public static boolean isConnection(){
ConnectivityManager mConnectivity = (ConnectivityManager)AppApplication.getApplication().getSystemService(Context.CONNECTIVITY_SERVICE);
// 检查网络连接,如果无网络可用,就不需要进行连网操作等
NetworkInfo info = mConnectivity.getActiveNetworkInfo();
if (info == null || !mConnectivity.getBackgroundDataSetting()) {
return false;
}
return true;
}
}