BaseMVPActivity的代码
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
/**
* 所有MVP模式中activity必须继承此界面
* @param 代表view的泛型
* @param 代表presenter的泛型
* 每个mvp中,v与p是不同的 所以才BaseMvp中使用泛型代替
* Created by Administrator on 2019/1/2 0002.
*/
public abstract class BaseMVPActivity> extends BaseActivity {
public T presenter ;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {;
presenter=initPresenter();
presenter.attach((V) this);
super.onCreate(savedInstanceState);
}
/**
* 判断网络是否连接
*
*/
public static boolean isNetWorkAvailable(Context context) {
//网络连接管理器
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//网络信息
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null) {
return true;
}
Toast.makeText(context, "网络已断开", Toast.LENGTH_SHORT).show();
return false;
}
/**
* 判断是否是wifi
*
*/
public static boolean isWifi(Context context) {
//网络连接管理器
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//网络信息
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null && info.getType() == connectivityManager.TYPE_WIFI) {
return true;
}
return false;
}
/**
* 判断是否是手机流量
*
*/
public static void isMobile(Context context) {
//网络连接管理器
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//网络信息
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null && info.getType() == connectivityManager.TYPE_MOBILE) {
Toast.makeText(context, "正在使用手机流量", Toast.LENGTH_SHORT).show();
}
}
/**
* Toast
*/
public void showToat(String text) {
if (text != null && !text.trim().equals("")) {
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
}
/**
* log
*/
public void LogE(String context, String msg) {
Log.e(context, msg);
}
/**
* 跳转Activity的逻辑(不传递数据)
* @param clazz 所需要跳转的界面
* boolean 是否关闭当前界面
*/
public void startActivity(Class clazz, boolean isFinish) {
startActivity(clazz, isFinish, null,null);
}
/**
* 跳转Activity的逻辑(传递数据)
*@param clazz 所需要跳转的界面
* @param isFinish 是否关闭当前页面
* @param contantName 传递参数的key值
* @param contant 传递参数values值
*/
public void startActivity(Class clazz, boolean isFinish,String contantName, String contant) {
Intent intent = new Intent(this, clazz);
if (contant != null) {
intent.putExtra(contantName, contant);
}
startActivity(intent);
if (isFinish) {
finish();
}
}
/**
* 传入String 权限,权限
*
* @param permissions
*/
List permissionList = new ArrayList<>();
/**
* 为子类提供一个权限检查方法
* 同时申请多个权限
*/
public void checkPermissions(String... permissions) {
//判断版本是否大于6.0
if (Build.VERSION.SDK_INT >= 23) {
if (permissions.length > 0) {
for (int i = 0; i < permissions.length; i++) {
if (ContextCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
permissionList.add(permissions[i]);
}
}
if (!permissionList.isEmpty()) {
String[] permissionArray = permissionList.toArray(new String[permissionList.size()]);
for (int i = 0; i < permissionArray.length; i++) {
//权限申请
ActivityCompat.requestPermissions(this, permissionArray, 66);
}
}
}
}
}
/**
* 检查用户是否已经同意过该权限
* 申请一个权限
*/
public void checkPermission(String permission, OnToDo onToDo) {
this.onToDo = onToDo;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{permission}, 88);
} else {
if (onToDo != null) {
//申请过后直接接口回调调用功能
onToDo.onToDoListener();
}
}
}else{
Toast.makeText(this, "版本低于6.0", Toast.LENGTH_SHORT).show();
}
}
private OnToDo onToDo;
public interface OnToDo {
void onToDoListener();
}
@SuppressLint("MissingPermission")
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1111:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent intent = new Intent(Intent.ACTION_CALL);
String phoneNum = "10086";
Uri data = Uri.parse("tel:" + phoneNum);
intent.setData(data);
startActivity(intent);
}
break;
case 66:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "申请成功", Toast.LENGTH_SHORT).show();
} else {
//用户拒绝了权限请求,给用户提示权限的功能
Toast.makeText(this, "权限没有授予", Toast.LENGTH_SHORT).show();
}
break;
case 88:
if (onToDo != null) {
onToDo.onToDoListener();
}
break;
}
}
/**
* 初始化布局
*/
public abstract int getLayout();
/**
* 初始化界面
*/
public abstract void initView();
/**
* 初始化数据
*/
public abstract void initData();
/**
* 设置监听器
*/
public abstract void initListener();
/**
* 初始化p层
*
*/
public abstract T initPresenter();
/**
*
*释放资源
*/
@Override
protected void onDestroy() {
super.onDestroy();
presenter.detach();
}
}
BasePresenter的代码
/**
*
* P层基类,用来统一处理mvp模式内存泄露的问题,所有p层类需要继承此类
* Created by Administrator on 2019/1/2 0002.
*/
public class BasePresenter {
public T view;
public void attach(T view) {
this.view = view;
}
//防止内存泄漏,将对象置为null
public void detach() {
this.view = null;
}
}
BaseActivity 的代码
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
/**
* data:2018/12/25
* function:
*/
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onStart() {
super.onStart();
isNetWorkAvailable(this);
isWifi(this);
isMobile(this);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayout());
initView();
initData();
initListener();
}
/**
* 判断网络是否连接
*
*/
public static boolean isNetWorkAvailable(Context context) {
//网络连接管理器
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//网络信息
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null) {
return true;
}
Toast.makeText(context, "网络已断开", Toast.LENGTH_SHORT).show();
return false;
}
/**
* 判断是否是wifi
*
*/
/**
* 判断是否是wifi
*
*/
public static boolean isWifi(Context context) {
//网络连接管理器
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//网络信息
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null && info.getType() == connectivityManager.TYPE_WIFI) {
return true;
}
return false;
}
/**
* 判断是否是手机流量
*
*/
public static void isMobile(Context context) {
//网络连接管理器
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//网络信息
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null && info.getType() == connectivityManager.TYPE_MOBILE) {
Toast.makeText(context, "正在使用手机流量", Toast.LENGTH_SHORT).show();
}
}
/**
* Toast
*/
public void showToat(String text) {
if (text != null && !text.trim().equals("")) {
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
}
/**
* log
*/
public void LogE(String context, String msg) {
Log.e(context, msg);
}
/**
* 跳转Activity的逻辑
*/
public void startActivity(Class clazz, boolean isFinish) {
startActivity(clazz, isFinish, null,null);
}
public void startActivity(Class clazz, boolean isFinish,String contantName, String contant) {
Intent intent = new Intent(this, clazz);
if (contant != null) {
intent.putExtra(contantName, contant);
}
startActivity(intent);
if (isFinish) {
finish();
}
}
/**
* 传入String 权限,权限
*
* @param permissions
*/
List permissionList = new ArrayList<>();
/**
* 为子类提供一个权限检查方法
* 同时申请多个权限
*/
public void checkPermissions(String... permissions) {
//判断版本是否大于6.0
if (Build.VERSION.SDK_INT >= 23) {
if (permissions.length > 0) {
for (int i = 0; i < permissions.length; i++) {
if (ContextCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
permissionList.add(permissions[i]);
}
}
if (!permissionList.isEmpty()) {
String[] permissionArray = permissionList.toArray(new String[permissionList.size()]);
for (int i = 0; i < permissionArray.length; i++) {
//权限申请
ActivityCompat.requestPermissions(this, permissionArray, 66);
}
}
}
}
}
/**
* 检查用户是否已经同意过该权限
* 申请一个权限
*/
public void checkPermission(String permission, OnToDo onToDo) {
this.onToDo = onToDo;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{permission}, 88);
} else {
if (onToDo != null) {
//申请过后直接接口回调调用功能
onToDo.onToDoListener();
}
}
}else{
Toast.makeText(this, "版本低于6.0", Toast.LENGTH_SHORT).show();
}
}
private OnToDo onToDo;
public interface OnToDo {
void onToDoListener();
}
@SuppressLint("MissingPermission")
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1111:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent intent = new Intent(Intent.ACTION_CALL);
String phoneNum = "10086";
Uri data = Uri.parse("tel:" + phoneNum);
intent.setData(data);
startActivity(intent);
}
break;
case 66:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "申请成功", Toast.LENGTH_SHORT).show();
} else {
//用户拒绝了权限请求,给用户提示权限的功能
Toast.makeText(this, "权限没有授予", Toast.LENGTH_SHORT).show();
}
break;
case 88:
if (onToDo != null) {
onToDo.onToDoListener();
}
break;
}
}
/**
* 初始化布局
*/
public abstract int getLayout();
/**
* 初始化数据
*/
protected abstract void initData();
/**
* 初始化界面
*/
protected abstract void initView();
/**
* 设置监听器
*/
protected abstract void initListener();
}
BaseObserver 的代码
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
/**
*使用适配器,模式,简化observer所需要的重写方法,只提取我们需要的
* Created by Administrator on 2019/1/2 0002.
*/
public abstract class BaseObserver implements Observer {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(String s) {
onSuccess(s);
}
@Override
public void onError(Throwable e) {
onFailed(e);
}
@Override
public void onComplete() {
}
/**
*成功,回传的数据
*/
public abstract void onSuccess(String result);
/**
*失败所需要提示的异常信息
*/
public abstract void onFailed(Throwable e);
}
ApiService 的代码
import java.util.Map;
import io.reactivex.Observable;
import retrofit2.http.FieldMap;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.QueryMap;
import retrofit2.http.Url;
/**
* 使用Rxjava,网络请求的值,使用String接收,避免写很多的bean类
* 对于接口的拼接,在调用方法的时候,进行拼接,简单的接口,可直接传入完整的网址。
* Created by Administrator on 2019/1/2 0002.
*/
public interface ApiService {
/**
* 普通Get基本请求
*/
@GET
Observable get(@Url String url);
/**
* Get请求提交表单
*/
@GET
public Observable get(@Url String url, @QueryMap Map map);
@GET
Observable gets(@Url String url, @QueryMap Map maps);
@GET
Observable getx(@Url String url);
@FormUrlEncoded
@POST
Observable posts(@Url String url, @FieldMap Map maps);
@FormUrlEncoded
@POST
Observable post(@Url String url, @FieldMap Map maps);
}
MyIntercepter拦截器(加头参) 的代码
import java.io.IOException;
import lyq.com.mylianxi.MyApp;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created by Administrator on 2019/1/2 0002.
*/
public class MyIntercepter implements Interceptor {
private UtilsSharep mWd;
@Override
public Response intercept(Chain chain) throws IOException {
mWd = new UtilsSharep(MyApp.getContext(), "WD");
int userId= (int) mWd.getSharedPreference("userId", 0);
String sessionId= (String) mWd.getSharedPreference("sessionId", "");
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder()
.addHeader("userId",userId+"")
.addHeader("sessionId", sessionId)
.addHeader("ak", "0110010010000");
Request request = requestBuilder.build();
return chain.proceed(request);
}
}
RetorfitFactory 的代码
import java.util.Map;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
/**
* Created by Administrator on 2019/1/2 0002.
*/
public class RetorfitFactory {
public static ApiService apiService;
public static RetorfitFactory instencs;
public static OkHttpClient client;
public static Retrofit retrofit;
private RetorfitFactory(){
if(client==null){
synchronized (OkHttpClient.class){
if (client==null){
client=new OkHttpClient.Builder()
.writeTimeout(3000, TimeUnit.MILLISECONDS)
.readTimeout(3000,TimeUnit.MILLISECONDS)
.connectTimeout(3000,TimeUnit.MILLISECONDS)
.addInterceptor(new MyIntercepter())
.build();
}
}
}
if(retrofit==null){
synchronized (Retrofit.class){
if (retrofit==null){
retrofit=new Retrofit.Builder()
.baseUrl("http://172.17.8.100/")
.addConverterFactory(ScalarsConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(client)
.build();
apiService = retrofit.create(ApiService.class);
}
}
}
}
public static RetorfitFactory getInstencs(){
if (instencs==null){
synchronized (RetorfitFactory.class){
if (instencs==null){
instencs=new RetorfitFactory();
}
}
}
return instencs;
}
public static Observable get(String url) {
return apiService.get(url)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
public static Observable get(String url, Map map) {
return apiService.get(url, map).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
public static Observable post(String url, Map map) {
return apiService.post(url, map).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
public static Observable gets(String url, Map maps) {
return apiService.gets(url, maps).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
public static Observable getX(String url) {
return apiService.getx(url).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
public static Observable posts(String url, Map maps) {
return apiService.posts(url, maps).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
}
UtilsSharep (sharedPreferences封装)的代码
import android.content.Context;
import android.content.SharedPreferences;
import java.util.Map;
public class UtilsSharep {
private SharedPreferences sharedPreferences;
/*
* 保存手机里面的名字
*/private SharedPreferences.Editor editor;
public UtilsSharep(Context context, String FILE_NAME) {
sharedPreferences = context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
}
/**
* 存储
*/
public void put(String key, Object object) {
if (object instanceof String) {
editor.putString(key, (String) object);
} else if (object instanceof Integer) {
editor.putInt(key, (Integer) object);
} else if (object instanceof Boolean) {
editor.putBoolean(key, (Boolean) object);
} else if (object instanceof Float) {
editor.putFloat(key, (Float) object);
} else if (object instanceof Long) {
editor.putLong(key, (Long) object);
} else {
editor.putString(key, object.toString());
}
editor.commit();
}
/**
* 获取保存的数据
*/
public Object getSharedPreference(String key, Object defaultObject) {
if (defaultObject instanceof String) {
return sharedPreferences.getString(key, (String) defaultObject);
} else if (defaultObject instanceof Integer) {
return sharedPreferences.getInt(key, (Integer) defaultObject);
} else if (defaultObject instanceof Boolean) {
return sharedPreferences.getBoolean(key, (Boolean) defaultObject);
} else if (defaultObject instanceof Float) {
return sharedPreferences.getFloat(key, (Float) defaultObject);
} else if (defaultObject instanceof Long) {
return sharedPreferences.getLong(key, (Long) defaultObject);
} else {
return sharedPreferences.getString(key, null);
}
}
/**
* 移除某个key值已经对应的值
*/
public void remove(String key) {
editor.remove(key);
editor.commit();
}
/**
* 清除所有数据
*/
public void clear() {
editor.clear();
editor.commit();
}
/**
* 查询某个key是否存在
*/
public Boolean contain(String key) {
return sharedPreferences.contains(key);
}
/**
* 返回所有的键值对
*/
public Map getAll() {
return sharedPreferences.getAll();
}
}
MainActivity 的代码
import android.widget.TextView;
import java.util.HashMap;
import java.util.Map;
import lyq.com.mylianxi.R;
import lyq.com.mylianxi.bean.DataJsonBean;
import lyq.com.mylianxi.presenters.Presenters;
import lyq.com.mylianxi.base.BaseMVPActivity;
public class MainActivity extends BaseMVPActivity implements ViewInterface{
TextView istext;
private Map mMap;
@Override
public int getLayout() {
return R.layout.activity_main;
}
@Override
public void initView() {
istext = findViewById(R.id.istext);
mMap = new HashMap<>();
mMap.put("page",1);
presenter.getData(mMap);
}
@Override
public void initData() {
}
@Override
public void initListener() {
}
@Override
public Presenters initPresenter() {
return new Presenters();
}
@Override
public void onSuccessUpdate(DataJsonBean result) {
istext.setText(result.getData().getNews().get(1).getTitle());
}
@Override
public void onFailedUpdate(Throwable e) {
}
}
ViewInterface 的代码
/**
* date:2019/6/22
* author:理元旗(ynkj)
* function:
*/
public interface ViewInterface {
/**
*成功,回传的数据
* @param result
*/
void onSuccessUpdate(DataJsonBean result);
/**
*失败所需要提示的异常信息
*/
void onFailedUpdate(Throwable e);
}
Presenters 的代码
import com.google.gson.Gson;
import java.util.Map;
import io.reactivex.Observable;
import lyq.com.mylianxi.model.Model;
import lyq.com.mylianxi.base.BaseObserver;
import lyq.com.mylianxi.base.BasePresenter;
import lyq.com.mylianxi.bean.DataJsonBean;
import lyq.com.mylianxi.view.ViewInterface;
/**
* date:2019/6/22
* function:
*/
public class Presenters extends BasePresenter implements PresentersIntreface{
private Model mModel;
public Presenters() {
mModel=new Model();
}
@Override
public void getData(Map map) {
final Observable data = mModel.getData(map);
data.subscribe(new BaseObserver() {
@Override
public void onSuccess(String result) {
DataJsonBean dataJsonBean = new Gson().fromJson(result, DataJsonBean.class);
view.onSuccessUpdate(dataJsonBean);
}
@Override
public void onFailed(Throwable e) {
}
});
}
}
PresentersIntreface 的代码
import java.util.Map;
/**
* date:2019/6/22
* function:
*/
public interface PresentersIntreface {
void getData(Map map);
}
Model 的代码
import java.util.Map;
import io.reactivex.Observable;
import lyq.com.mylianxi.uitls.RetorfitFactory;
/**
* date:2019/6/22
* function:
*/
public class Model implements ModelInterface{
@Override
public Observable getData(Map map) {
Observable gets = RetorfitFactory.getInstencs().gets("http://blog.zhaoliang5156.cn/zixunnew/fengjing", map);
return gets;
}
}
ModelInterface 的代码
import java.util.Map;
import io.reactivex.Observable;
/**
* date:2019/6/22
* function:
*/
public interface ModelInterface {
Observable getData(Map map);
}