xiongxiaohe лет назад: 8
Родитель
Сommit
151ea04c17

+ 7 - 0
app/src/main/AndroidManifest.xml

@@ -70,6 +70,13 @@
             android:exported="true">
             android:exported="true">
         </service>
         </service>
 
 
+        <service
+            android:name=".service.DownloadService"
+      >
+        </service>
+
+
+
         <provider
         <provider
             android:name=".provider.SheepFileProvider"
             android:name=".provider.SheepFileProvider"
             android:authorities="${applicationId}.fileprovider"
             android:authorities="${applicationId}.fileprovider"

+ 2 - 2
app/src/main/java/com/sheep/jiuyan/samllsheep/Config.java

@@ -18,10 +18,10 @@ public class Config {
     //        public String mBaseUrl = "http://10.8.220.213:8088";
     //        public String mBaseUrl = "http://10.8.220.213:8088";
     //    public String mBaseUrl = "http://10.8.220.130:8088";
     //    public String mBaseUrl = "http://10.8.220.130:8088";
     //何威
     //何威
-//      public String mBaseUrl = "http://10.8.220.6:8088";
+    public String mBaseUrl = "http://10.8.220.6:8088";
 //张伟
 //张伟
 // public String mBaseUrl = "http://10.8.220.132:8088";
 // public String mBaseUrl = "http://10.8.220.132:8088";
-   public String mBaseUrl = "http://api.task.bandaimi.com";
+//   public String mBaseUrl = "http://api.task.bandaimi.com";
 //    public String mBaseUrl = "http://test.ss.shouyouquan.com";
 //    public String mBaseUrl = "http://test.ss.shouyouquan.com";
     public final static String WX_APPID = "wx3c0376c4fcc8b723";
     public final static String WX_APPID = "wx3c0376c4fcc8b723";
     public final static String BUGLY_APPID = "0dd998212c";
     public final static String BUGLY_APPID = "0dd998212c";

+ 1 - 0
app/src/main/java/com/sheep/jiuyan/samllsheep/page/InputWXAccountActivity.java

@@ -123,6 +123,7 @@ public class InputWXAccountActivity extends BaseActivity implements View.OnClick
         map.put("ps_3", et_pwd_3.getText().toString());
         map.put("ps_3", et_pwd_3.getText().toString());
         map.put("ps_4", et_pwd_4.getText().toString());
         map.put("ps_4", et_pwd_4.getText().toString());
         showProgress();
         showProgress();
+
         NetManager.get(Url.COMMIT_WXACCOUNT, map, getApplicationContext(), new SheepCallback<String>(this) {
         NetManager.get(Url.COMMIT_WXACCOUNT, map, getApplicationContext(), new SheepCallback<String>(this) {
 
 
 
 

+ 42 - 3
app/src/main/java/com/sheep/jiuyan/samllsheep/page/MainActivity.java

@@ -50,6 +50,7 @@ import com.sheep.jiuyan.samllsheep.net.NetManager;
 import com.sheep.jiuyan.samllsheep.net.SheepCallback;
 import com.sheep.jiuyan.samllsheep.net.SheepCallback;
 import com.sheep.jiuyan.samllsheep.net.Url;
 import com.sheep.jiuyan.samllsheep.net.Url;
 import com.sheep.jiuyan.samllsheep.page.adapter.AdpMainPageViewPager;
 import com.sheep.jiuyan.samllsheep.page.adapter.AdpMainPageViewPager;
+import com.sheep.jiuyan.samllsheep.page.entry.User;
 import com.sheep.jiuyan.samllsheep.page.fragment.FgtMainPageOffline;
 import com.sheep.jiuyan.samllsheep.page.fragment.FgtMainPageOffline;
 import com.sheep.jiuyan.samllsheep.page.fragment.FgtMainPageTask;
 import com.sheep.jiuyan.samllsheep.page.fragment.FgtMainPageTask;
 import com.sheep.jiuyan.samllsheep.page.view.RappTabLayout;
 import com.sheep.jiuyan.samllsheep.page.view.RappTabLayout;
@@ -216,9 +217,47 @@ if (BuildConfig.DEBUG)
             @Override
             @Override
             public void onClick(View view) {
             public void onClick(View view) {
                 if (SpUtils.isLogin(getApplicationContext())) {
                 if (SpUtils.isLogin(getApplicationContext())) {
-                    Intent intent = new Intent(MainActivity.this, WithdrawActivity.class);
-                    intent.putExtra("money", G.mUser.getBalance());
-                    startActivity(intent);
+
+                    HashMap<String, String> map = new HashMap<>();
+                    map.put("wx_open_id", SpUtils.getOpenId(MainActivity.this));
+                    NetManager.get(Url.USER_INFO, map, getApplicationContext(), new SheepCallback<User>(MainActivity.this) {
+                        @Override
+                        public void success(User user) {
+                            hideProgress();
+
+                            if(!TextUtils.isEmpty(user.getBalance())){
+
+                                try {
+                                    float money =Float.parseFloat(user.getBalance());
+                                    if(money>=5){
+                                        Intent intent = new Intent(MainActivity.this, WithdrawActivity.class);
+                                        intent.putExtra("money", user.getBalance());
+                                        startActivity(intent);
+                                    }
+
+                                }catch (Exception e){
+                                      Toast.makeText(MainActivity.this,"亲!最低体现金额是5元哟!",Toast.LENGTH_LONG).show();
+                                }
+
+                            }else{
+                                Toast.makeText(MainActivity.this,"亲!最低体现金额是5元哟!",Toast.LENGTH_LONG).show();
+                            }
+
+
+                        }
+
+                        @Override
+                        public void failure(IOException e) {
+                            hideProgress();
+                            Toast.makeText(MainActivity.this,"系统异常,请稍后访问",Toast.LENGTH_LONG).show();
+
+                        }
+                    });
+
+
+
+
+
                 }
                 }
             }
             }
         });
         });

+ 38 - 23
app/src/main/java/com/sheep/jiuyan/samllsheep/page/WithdrawActivity.java

@@ -1,5 +1,6 @@
 package com.sheep.jiuyan.samllsheep.page;
 package com.sheep.jiuyan.samllsheep.page;
 
 
+import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.Intent;
 import android.os.Bundle;
 import android.os.Bundle;
 import android.support.annotation.Nullable;
 import android.support.annotation.Nullable;
@@ -36,6 +37,8 @@ public class WithdrawActivity extends BaseActivity implements View.OnClickListen
     private GvAdapter mAdapter;
     private GvAdapter mAdapter;
     private AlertDialog mDialog;
     private AlertDialog mDialog;
     private TextView mTvMsg;
     private TextView mTvMsg;
+    private  String  accountMoney;
+
 
 
     @Override
     @Override
     public void onCreate(@Nullable Bundle savedInstanceState) {
     public void onCreate(@Nullable Bundle savedInstanceState) {
@@ -54,22 +57,10 @@ public class WithdrawActivity extends BaseActivity implements View.OnClickListen
 
 
     @Override
     @Override
     public void initData() {
     public void initData() {
-        showProgress();
-        HashMap<String, String> map = new HashMap<>();
-        map.put("wx_open_id", SpUtils.getOpenId(this));
-        NetManager.get(Url.USER_INFO, map, getApplicationContext(), new SheepCallback<User>(this) {
-            @Override
-            public void success(User user) {
-                hideProgress();
-                mMoney.setText(String.format("账户余额:%s", user.getBalance()));
-            }
-
-            @Override
-            public void failure(IOException e) {
-                super.failure(e);
-                hideProgress();
-            }
-        });
+       Intent i=getIntent();
+        if(i.hasExtra("money")){
+            accountMoney=i.getStringExtra("money");
+        }
     }
     }
 
 
     @Override
     @Override
@@ -94,13 +85,24 @@ public class WithdrawActivity extends BaseActivity implements View.OnClickListen
 
 
     //提现
     //提现
     private void commit() {
     private void commit() {
-        if (!SpUtils.isLogin(getApplicationContext())) {
-            //            startActivity(new Intent(this, LoginActivity.class));
-        } else {
             if (mAdapter.checked == -1) {
             if (mAdapter.checked == -1) {
                 Toast.makeText(this, "请选择提现金额", Toast.LENGTH_SHORT).show();
                 Toast.makeText(this, "请选择提现金额", Toast.LENGTH_SHORT).show();
                 return;
                 return;
             }
             }
+
+        try {
+            float money =Float.parseFloat(accountMoney);
+            if(money<mMoneys[mAdapter.checked]){
+                Toast.makeText(this,"亲!余额不足哟",Toast.LENGTH_LONG).show();
+                return ;
+            }
+
+        }catch (Exception e){
+
+            Toast.makeText(this,"亲!余额不足哟",Toast.LENGTH_LONG).show();
+            return ;
+        }
+
             HashMap<String, String> map = new HashMap<>();
             HashMap<String, String> map = new HashMap<>();
             map.put("money", mMoneys[mAdapter.checked] + "");
             map.put("money", mMoneys[mAdapter.checked] + "");
             map.put("wx_open_id", SpUtils.getOpenId(this));
             map.put("wx_open_id", SpUtils.getOpenId(this));
@@ -116,13 +118,26 @@ public class WithdrawActivity extends BaseActivity implements View.OnClickListen
                 public void otherCode(int code, String string) {
                 public void otherCode(int code, String string) {
                     hideProgress();
                     hideProgress();
                     if (code == 406) {
                     if (code == 406) {
-                        startActivity(new Intent(WithdrawActivity.this, BindWxActivity.class));
-                        return;
+                         android.app.AlertDialog alertDialog = new android.app.AlertDialog.Builder(WithdrawActivity.this)
+                                .setTitle("温馨提示!")
+                                .setMessage("未绑定微信号不能体现哟!")
+                                .setPositiveButton("马上去绑定", new DialogInterface.OnClickListener() {
+                                    @Override
+                                    public void onClick(DialogInterface dialogInterface, int i) {
+                                        startActivity(new Intent(WithdrawActivity.this, BindWxActivity.class));
+                                    }
+                                })
+                                .setNegativeButton("稍后",null)
+                                .setCancelable(false)
+                                .create();
+                        alertDialog.setCanceledOnTouchOutside(false);
+                        alertDialog.show();
+                    }else {
+                        showMsgDialog(string);
                     }
                     }
-                    showMsgDialog(string);
                 }
                 }
             });
             });
-        }
+
     }
     }
 
 
     private void showMsgDialog(String msg) {
     private void showMsgDialog(String msg) {

+ 273 - 0
app/src/main/java/com/sheep/jiuyan/samllsheep/service/DownloadService.java

@@ -0,0 +1,273 @@
+package com.sheep.jiuyan.samllsheep.service;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.RandomAccessFile;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import android.app.Notification;
+import android.app.NotificationManager;
+import android.app.Service;
+import android.content.Context;
+import android.content.Intent;
+import android.net.Uri;
+import android.os.Environment;
+import android.os.Handler;
+import android.os.IBinder;
+import android.text.TextUtils;
+import android.util.Log;
+import android.widget.ProgressBar;
+import android.widget.RemoteViews;
+import android.widget.TextView;
+import android.widget.Toast;
+import com.sheep.jiuyan.samllsheep.R;
+import com.sheep.jiuyan.samllsheep.utils.PackageUtil;
+
+/**
+ *  @auther dabiaoge
+ * 自动更新的Service
+ */
+
+public class DownloadService extends Service{
+
+    public static final String DOWNLOAD_PATH =
+            Environment.getExternalStorageDirectory().getAbsolutePath()+
+                    "Sheep/downloads/";
+    public static final String TAG = "download";
+    private String url;//下载链接
+    private int length;//文件长度
+    private String fileName=null;//文件名
+    private Notification notification;
+    private RemoteViews contentView;
+    private NotificationManager notificationManager;
+
+    private static final int MSG_INIT = 0;
+    private static final int URL_ERROR = 1;
+    private static final int NET_ERROR = 2;
+    private static final int DOWNLOAD_SUCCESS = 3;
+    private Handler mHandler = new Handler(){
+        public void handleMessage(android.os.Message msg) {
+            switch (msg.what) {
+                case MSG_INIT:
+                    length = (int) msg.obj;
+                    new DownloadThread(url,length).start();
+                    createNotification();
+                    break;
+                case DOWNLOAD_SUCCESS:
+                    //下载完成
+                    notifyNotification(100, 100);
+                    PackageUtil.installApk(DownloadService.this, DOWNLOAD_PATH+File.separator+fileName);
+                    Toast.makeText(DownloadService.this, "下载完成", Toast.LENGTH_LONG).show();
+                    break;
+                case URL_ERROR:
+                    Toast.makeText(DownloadService.this, "下载地址错误", Toast.LENGTH_LONG).show();
+                    break;
+                case NET_ERROR:
+                    Toast.makeText(DownloadService.this, "连接失败,请检查网络设置", Toast.LENGTH_LONG).show();
+            }
+        };
+    };
+
+
+    public IBinder onBind(Intent arg0) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public int onStartCommand(Intent intent, int flags, int startId) {
+        if(intent != null){
+            url = intent.getStringExtra("url");
+            if(url != null && !TextUtils.isEmpty(url)){
+                new InitThread(url).start();
+            }else{
+                mHandler.sendEmptyMessage(URL_ERROR);
+            }
+
+        }
+        return super.onStartCommand(intent, flags, startId);
+    }
+
+    /**
+     * 初始化子线程
+     * @author dong
+     *
+     */
+    class InitThread extends Thread{
+        String url = "";
+
+        public InitThread(String url) {
+            this.url = url;
+        }
+        public void run() {
+            HttpURLConnection conn= null;
+            RandomAccessFile raf = null;
+            try {
+                //连接网络文件
+                URL url = new URL(this.url);
+                conn = (HttpURLConnection) url.openConnection();
+                conn.setConnectTimeout(3000);
+                conn.setRequestMethod("GET");
+                int length = -1;
+                if(conn.getResponseCode() == 200){
+                    //获得文件长度
+                    length = conn.getContentLength();
+                }
+                if(length <= 0){
+                    return;
+                }
+                File dir = new File(DOWNLOAD_PATH);
+                if(!dir.exists()){
+                    dir.mkdir();
+                }
+                fileName = this.url.substring(this.url.lastIndexOf("/")+1, this.url.length());
+                if(fileName==null && TextUtils.isEmpty(fileName) && !fileName.contains(".apk")){
+                    fileName = getPackageName()+".apk";
+                }
+                File file = new File(dir, fileName);
+                raf = new RandomAccessFile(file, "rwd");
+                //设置文件长度
+                raf.setLength(length);
+                mHandler.obtainMessage(MSG_INIT,length).sendToTarget();
+            } catch (Exception e) {
+                mHandler.sendEmptyMessage(URL_ERROR);
+                e.printStackTrace();
+            } finally{
+                try {
+                    conn.disconnect();
+                    raf.close();
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+    /**
+     * 下载线程
+     * @author dong
+     *
+     */
+    class DownloadThread extends Thread{
+        String url;
+        int length;
+        public DownloadThread(String url, int length) {
+            this.url = url;
+            this.length = length;
+        }
+        @Override
+        public void run() {
+            HttpURLConnection conn = null;
+            RandomAccessFile raf = null;
+            InputStream input = null;
+            try {
+
+                URL url = new URL(this.url);
+                conn = (HttpURLConnection) url.openConnection();
+                conn.setConnectTimeout(3000);
+                conn.setRequestMethod("GET");
+                //设置下载位置
+                int start =0;
+                conn.setRequestProperty("Range", "bytes="+0+"-"+length);
+                //设置文件写入位置
+                File file = new File(DownloadService.DOWNLOAD_PATH,fileName);
+                raf = new RandomAccessFile(file, "rwd");
+                raf.seek(start);
+                long mFinished = 0;
+                //开始下载
+                if(conn.getResponseCode() == 206){
+                    //LogUtil.i("下载开始了。。。");
+                    //读取数据
+                    input = conn.getInputStream();
+                    byte[] buffer = new byte[1024*4];
+                    int len = -1;
+                    long speed = 0;
+                    long time = System.currentTimeMillis();
+                    while((len = input.read(buffer)) != -1){
+                        //写入文件
+                        raf.write(buffer,0,len);
+                        //把下载进度发送广播给Activity
+                        mFinished += len;
+                        speed += len;
+                        if(System.currentTimeMillis() - time > 1000){
+                            time = System.currentTimeMillis();
+
+                            notifyNotification(mFinished,length);
+                            Log.i(TAG, "mFinished=="+mFinished);
+                            Log.i(TAG, "length=="+length);
+                            Log.i(TAG, "speed=="+speed);
+                            speed = 0;
+                        }
+                    }
+                    mHandler.sendEmptyMessage(DOWNLOAD_SUCCESS);
+                    Log.i(TAG, "下载完成了。。。");
+                }else{
+                    Log.i(TAG, "下载出错了。。。");
+                    mHandler.sendEmptyMessage(NET_ERROR);
+                }
+
+            } catch (Exception e) {
+                e.printStackTrace();
+            } finally{
+                try {
+                    if(conn != null){
+                        conn.disconnect();
+                    }
+                    if(raf != null){
+                        raf.close();
+                    }
+                    if(input != null ){
+                        input.close();
+                    }
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+
+            }
+        }
+    }
+    @SuppressWarnings("deprecation")
+    public void createNotification() {
+        notification = new Notification(
+                R.drawable.icon,//应用的图标
+                "安装包正在下载...",
+                System.currentTimeMillis());
+        notification.flags = Notification.FLAG_ONGOING_EVENT;
+        //notification.flags = Notification.FLAG_AUTO_CANCEL;
+
+        /*** 自定义  Notification 的显示****/
+        contentView = new RemoteViews(getPackageName(), R.layout.notification_item);
+        contentView.setProgressBar(R.id.progress, 100, 0, false);
+        contentView.setTextViewText(R.id.tv_progress, "0%");
+        notification.contentView = contentView;
+
+        /*updateIntent = new Intent(this, AboutActivity.class);
+      	updateIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
+     	updateIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+     	pendingIntent = PendingIntent.getActivity(this, 0, updateIntent, 0);
+      	notification.contentIntent = pendingIntent;*/
+        notification.flags = Notification.FLAG_AUTO_CANCEL;
+        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+
+        //设置notification的PendingIntent
+		/*Intent intt = new Intent(this, MainActivity.class);
+		PendingIntent pi = PendingIntent.getActivity(this,100, intt,Intent.FLAG_ACTIVITY_NEW_TASK	| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
+		notification.contentIntent = pi;*/
+
+        notificationManager.notify(R.layout.notification_item, notification);
+    }
+
+    private void notifyNotification(long percent,long length){
+
+        contentView.setTextViewText(R.id.tv_progress, (percent*100/length)+"%");
+        contentView.setProgressBar(R.id.progress, (int)length,(int)percent, false);
+        notification.contentView = contentView;
+        notificationManager.notify(R.layout.notification_item, notification);
+    }
+    /**
+     * 安装apk
+     *
+     * @param context 上下文
+     * @param file    APK文件
+     */
+}

+ 45 - 0
app/src/main/res/layout/notification_item.xml

@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="60dp"
+    android:orientation="horizontal">
+
+    <ImageView
+        android:layout_width="60dp"
+        android:layout_height="60dp"
+        android:background="@drawable/icon"/>
+
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:orientation="vertical">
+        <RelativeLayout
+            android:layout_width="fill_parent"
+            android:layout_height="wrap_content"
+            >
+            <TextView
+                android:layout_centerVertical="true"
+                android:padding="5dp"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:text="安装包正在下载..."
+                />
+            <TextView
+                android:id="@+id/tv_progress"
+                android:layout_alignParentRight="true"
+                android:padding="5dp"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:text="0%"
+                />
+        </RelativeLayout>
+
+        <ProgressBar
+            android:padding="5dp"
+            android:id="@+id/progress"
+            style="?android:attr/progressBarStyleHorizontal"
+            android:layout_width="fill_parent"
+            android:layout_height="wrap_content" />
+    </LinearLayout>
+
+</LinearLayout>