Commit aa7df233 by jeffrey

升级适配

parent 4d7bd544
...@@ -22,8 +22,8 @@ android { ...@@ -22,8 +22,8 @@ android {
applicationId "com.yuqirong.listenwechatnotification" applicationId "com.yuqirong.listenwechatnotification"
minSdkVersion 16 minSdkVersion 16
targetSdkVersion 26 targetSdkVersion 26
versionCode 200000 versionCode 200002
versionName "2.0.0" versionName "2.0.02"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
} }
buildTypes { buildTypes {
......
...@@ -101,7 +101,7 @@ public class MainActivity extends ParentActivity { ...@@ -101,7 +101,7 @@ public class MainActivity extends ParentActivity {
buttonUpDate.setOnClickListener(new View.OnClickListener() { buttonUpDate.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
checkPermission(); upgrade();
} }
}); });
...@@ -184,7 +184,7 @@ public class MainActivity extends ParentActivity { ...@@ -184,7 +184,7 @@ public class MainActivity extends ParentActivity {
private void checkPermission() { private void upgrade() {
//检查权限(NEED_PERMISSION)是否被授权 PackageManager.PERMISSION_GRANTED表示同意授权 //检查权限(NEED_PERMISSION)是否被授权 PackageManager.PERMISSION_GRANTED表示同意授权
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) { != PackageManager.PERMISSION_GRANTED) {
...@@ -197,7 +197,7 @@ public class MainActivity extends ParentActivity { ...@@ -197,7 +197,7 @@ public class MainActivity extends ParentActivity {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
} else { } else {
UpdateApk updateApkhttps = new UpdateApk(MainActivity.this); UpdateApk.checkAndUpdate(MainActivity.this);
} }
} }
......
...@@ -3,18 +3,27 @@ package com.yuqirong.listenwechatnotification.askhttp; ...@@ -3,18 +3,27 @@ package com.yuqirong.listenwechatnotification.askhttp;
import android.content.Context; import android.content.Context;
import android.widget.Toast; import android.widget.Toast;
import com.yuqirong.listenwechatnotification.MainActivity;
import com.yuqirong.listenwechatnotification.MyApplication;
import com.yuqirong.listenwechatnotification.bean.BaseResult;
import com.yuqirong.listenwechatnotification.bean.UpdateBean; import com.yuqirong.listenwechatnotification.bean.UpdateBean;
import com.yuqirong.listenwechatnotification.bean.UpgradeResult;
import com.yuqirong.listenwechatnotification.function.UpgradeFunction; import com.yuqirong.listenwechatnotification.function.UpgradeFunction;
import com.yuqirong.listenwechatnotification.http.HttpService; import com.yuqirong.listenwechatnotification.http.HttpService;
import com.yuqirong.listenwechatnotification.http.PayHttp;
import com.yuqirong.listenwechatnotification.uitl.Save2SharedPreferences; import com.yuqirong.listenwechatnotification.uitl.Save2SharedPreferences;
import com.yuqirong.listenwechatnotification.uitl.Utils; import com.yuqirong.listenwechatnotification.uitl.Utils;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
import retrofit2.Retrofit; import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory; import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory; import retrofit2.converter.scalars.ScalarsConverterFactory;
import retrofit2.http.HTTP;
import static com.yuqirong.listenwechatnotification.MainActivity.API; import static com.yuqirong.listenwechatnotification.MainActivity.API;
...@@ -32,45 +41,56 @@ public class UpdateApk { ...@@ -32,45 +41,56 @@ public class UpdateApk {
public UpdateApk() { public UpdateApk() {
} }
public UpdateApk(final Context context){ public static void checkAndUpdate(final Context context){
String temp = Utils.getLocalVersionName(context); final String versionName = Utils.getLocalVersionName(context);
String url = Save2SharedPreferences.read(API,context); String token = Save2SharedPreferences.read(MainActivity.TOKEN, MyApplication.get());
//建立retrofit对象
Retrofit retrofit = new Retrofit.Builder().baseUrl(url)
//添加返回字符串的支持--不知道返回的是什么,添加字符串支持 PayHttp.get()
.addConverterFactory(ScalarsConverterFactory.create()) .postSoftVersion(versionName,token)
//添加GSON转换支持 .subscribeOn(Schedulers.io())
.addConverterFactory(GsonConverterFactory.create()) .observeOn(AndroidSchedulers.mainThread())
.build(); .subscribe(new Consumer<BaseResult<UpgradeResult>>() {
@Override
public void accept(BaseResult<UpgradeResult> upgradeResult) throws Exception {
if (upgradeResult.getStatus_code() == 200 && upgradeResult.getData() != null ){
if(checkNew(versionName,upgradeResult.getData().getVersion())){
UpgradeFunction updateApk = new UpgradeFunction(context);
String baseUrl = Save2SharedPreferences.read(MainActivity.API,MyApplication.get());
updateApk.downloadApk(baseUrl+upgradeResult.getData().getUrl());
Toast.makeText(context, "有新版本,准备下载", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(context, "当前为最高版本", Toast.LENGTH_SHORT).show();
}
}
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Toast.makeText(context, throwable.getMessage(), Toast.LENGTH_SHORT).show();
}
});
//获取接口 }
HttpService service = retrofit.create(HttpService.class);
//调用方法-返回 回调更换为对象
Call<UpdateBean> call = service.postSoftVersion(temp);
//异步调用
call.enqueue(new Callback<UpdateBean>() {
@Override
public void onResponse(Call<UpdateBean> call, Response<UpdateBean> response) {
if (response.body().getStatus().equals(NEEDUPGRADE)){
updateApk = new UpgradeFunction(context);
updateApk.downloadApk(response.body().getUrl());
}else{
Toast.makeText(context, "当前为最高版本", Toast.LENGTH_SHORT).show();
}
}
@Override /**
public void onFailure(Call<UpdateBean> call, Throwable t) { *
* @param ourVersion
* @param remoteVersion
* @return true remote > our
*/
private static boolean checkNew(String ourVersion,String remoteVersion){
String[] ourVersions = ourVersion.split("[.]");
String[] remoteVersions = remoteVersion.split("[.]");
int our = Integer.valueOf(ourVersions[0])*10000 + Integer.valueOf(ourVersions[1])*100 + Integer.valueOf(ourVersions[2]);
int remote = Integer.valueOf(remoteVersions[0])*10000 + Integer.valueOf(remoteVersions[1])*100 + Integer.valueOf(remoteVersions[2]);
if (remote > our){
return true;
}else {
return false;
}
}
});
} }
} }
...@@ -6,9 +6,10 @@ package com.yuqirong.listenwechatnotification.bean; ...@@ -6,9 +6,10 @@ package com.yuqirong.listenwechatnotification.bean;
* 作者:Li * 作者:Li
*/ */
public class BaseResult { public class BaseResult<T> {
int status_code; int status_code;
String message; String message;
T data;
public int getStatus_code() { public int getStatus_code() {
return status_code; return status_code;
...@@ -25,4 +26,12 @@ public class BaseResult { ...@@ -25,4 +26,12 @@ public class BaseResult {
public void setMessage(String message) { public void setMessage(String message) {
this.message = message; this.message = message;
} }
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
} }
package com.yuqirong.listenwechatnotification.bean;
/**
* 创建日期:2018/3/21 on 15:08
* 描述:
* 作者:Li
*/
public class UpgradeResult extends BaseResult {
/**
* id : 2
* version : 2.2.33
* version_number : 20233
* created_at : 2018-03-20 16:53:48
* updated_at : 2018-03-20 16:53:48
* url : http://localhost/storage/public/apps/5879ab88346017d0ed1ffec92227c9e9.apk
*/
private int id;
private String version;
private int version_number;
private String created_at;
private String updated_at;
private String url;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public int getVersion_number() {
return version_number;
}
public void setVersion_number(int version_number) {
this.version_number = version_number;
}
public String getCreated_at() {
return created_at;
}
public void setCreated_at(String created_at) {
this.created_at = created_at;
}
public String getUpdated_at() {
return updated_at;
}
public void setUpdated_at(String updated_at) {
this.updated_at = updated_at;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
...@@ -4,6 +4,7 @@ import com.yuqirong.listenwechatnotification.bean.BaseResult; ...@@ -4,6 +4,7 @@ import com.yuqirong.listenwechatnotification.bean.BaseResult;
import com.yuqirong.listenwechatnotification.bean.ImageBean; import com.yuqirong.listenwechatnotification.bean.ImageBean;
import com.yuqirong.listenwechatnotification.bean.PullMoneyResp; import com.yuqirong.listenwechatnotification.bean.PullMoneyResp;
import com.yuqirong.listenwechatnotification.bean.UpdateBean; import com.yuqirong.listenwechatnotification.bean.UpdateBean;
import com.yuqirong.listenwechatnotification.bean.UpgradeResult;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -26,7 +27,7 @@ public interface HttpService { ...@@ -26,7 +27,7 @@ public interface HttpService {
/** /**
*固定的 在服务器后台配置的 *固定的 在服务器后台配置的
*/ */
public static final String token ="43ee9ffb8ce4509437b7734ae3095473"; public static final String token ="52e05c504da884303a10e8d0fa749c12";
// public String base_url = "http://test.fourth-pay.checheng.net/"; // public String base_url = "http://test.fourth-pay.checheng.net/";
public String base_url = "http://test.personal-pay.checheng.net"; public String base_url = "http://test.personal-pay.checheng.net";
...@@ -41,8 +42,12 @@ public interface HttpService { ...@@ -41,8 +42,12 @@ public interface HttpService {
}) })
Observable<BaseResult> postClientMoney(@Field("text") String text, @Field("username") String userName, @Field("gateway") String gateway, @Field("token") String token); Observable<BaseResult> postClientMoney(@Field("text") String text, @Field("username") String userName, @Field("gateway") String gateway, @Field("token") String token);
@GET("/app-api/app/status") @POST("/app-api/upgrade")
Call<UpdateBean> postSoftVersion(@Query("version") String version); @FormUrlEncoded
@Headers({
"Accept: application/json"
})
Observable<BaseResult<UpgradeResult>> postSoftVersion(@Field("version") String version, @Field("token") String token);
@POST("/app-api/amounts") @POST("/app-api/amounts")
@FormUrlEncoded @FormUrlEncoded
......
...@@ -13,6 +13,7 @@ import android.service.notification.StatusBarNotification; ...@@ -13,6 +13,7 @@ import android.service.notification.StatusBarNotification;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.widget.RemoteViews; import android.widget.RemoteViews;
import android.widget.Toast;
import com.orhanobut.logger.AndroidLogAdapter; import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.CsvFormatStrategy; import com.orhanobut.logger.CsvFormatStrategy;
...@@ -33,6 +34,7 @@ import java.util.ArrayList; ...@@ -33,6 +34,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import io.reactivex.Observer; import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer; import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
import okhttp3.FormBody; import okhttp3.FormBody;
...@@ -123,18 +125,21 @@ public class WeChatNotificationListenerService extends NotificationListenerServi ...@@ -123,18 +125,21 @@ public class WeChatNotificationListenerService extends NotificationListenerServi
if (!TextUtils.isEmpty(sbn.getNotification().tickerText)){ if (!TextUtils.isEmpty(sbn.getNotification().tickerText)){
contentText = sbn.getNotification().tickerText.toString(); contentText = sbn.getNotification().tickerText.toString();
} }
String token = Save2SharedPreferences.read(MainActivity.TOKEN,MyApplication.get());
if(!TextUtils.isEmpty(contentText)) { if(!TextUtils.isEmpty(contentText)) {
if (weChatPayPackageName.equals(sbn.getPackageName())) { if (weChatPayPackageName.equals(sbn.getPackageName())) {
if (contentText.contains("微信支付:")) { if (contentText.contains("微信支付")) {
contentText = contentText.substring(5); int pos = contentText.indexOf(":");
if (pos >0){
contentText = contentText.substring(pos+1);
}
mBuildNotification.listenSuccess(contentText); mBuildNotification.listenSuccess(contentText);
sendRequestWithOkHttp(contentText,pay2set,paytype,HttpService.token); sendRequestWithOkHttp(contentText,pay2set,paytype,token);
} }
}else{ }else{
mBuildNotification.listenSuccess(contentText); mBuildNotification.listenSuccess(contentText);
sendRequestWithOkHttp(contentText,pay2set,paytype,HttpService.token); sendRequestWithOkHttp(contentText,pay2set,paytype,token);
} }
...@@ -207,13 +212,14 @@ public class WeChatNotificationListenerService extends NotificationListenerServi ...@@ -207,13 +212,14 @@ public class WeChatNotificationListenerService extends NotificationListenerServi
//开启线程来发起网络请求 //开启线程来发起网络请求
PayHttp.get().postClientMoney(ticker,payto,paytype, token) PayHttp.get().postClientMoney(ticker,payto,paytype, token)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<BaseResult>() { .subscribe(new Consumer<BaseResult>() {
@Override @Override
public void accept(BaseResult s) throws Exception { public void accept(BaseResult s) throws Exception {
//发送请求获取服务器数据 //发送请求获取服务器数据
String result = "返回结果:"+s.getStatus_code() + " " + s.getMessage();
Logger.d("返回结果:"+s.getStatus_code() + " " + s.getMessage()); Logger.d(result);
Toast.makeText(MyApplication.get(),result,Toast.LENGTH_LONG).show();
BuildNotification mBuildNotification = new BuildNotification(MyApplication.get()); BuildNotification mBuildNotification = new BuildNotification(MyApplication.get());
mBuildNotification.postSuccessOrFaild(s.getMessage()); mBuildNotification.postSuccessOrFaild(s.getMessage());
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment