Commit f87a24feb37f50ecde1090ed840f588e0e91e8c6
1 parent
a2463c37
1,断网情况处理
2,网络连接后情况处理 3,流量使用逻辑处理
Showing
12 changed files
with
308 additions
and
40 deletions
app/src/main/AndroidManifest.xml
| ... | ... | @@ -4,12 +4,12 @@ |
| 4 | 4 | package="com.cnlive.downloadfile"> |
| 5 | 5 | |
| 6 | 6 | <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> |
| 7 | - <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> | |
| 7 | + <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | |
| 8 | 8 | <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> |
| 9 | - <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" | |
| 9 | + <uses-permission | |
| 10 | + android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" | |
| 10 | 11 | tools:ignore="ProtectedPermissions" /> |
| 11 | - | |
| 12 | - <uses-permission android:name="android.permission.INTERNET"/> | |
| 12 | + <uses-permission android:name="android.permission.INTERNET" /> | |
| 13 | 13 | |
| 14 | 14 | <application |
| 15 | 15 | android:name=".MyApplication" |
| ... | ... | @@ -19,6 +19,9 @@ |
| 19 | 19 | android:roundIcon="@mipmap/ic_launcher_round" |
| 20 | 20 | android:supportsRtl="true" |
| 21 | 21 | android:theme="@style/AppTheme"> |
| 22 | + <activity | |
| 23 | + android:name=".AudioDialogActivity" | |
| 24 | + android:theme="@style/CustomDialogStyle" /> | |
| 22 | 25 | <activity android:name=".MainActivity"> |
| 23 | 26 | <intent-filter> |
| 24 | 27 | <action android:name="android.intent.action.MAIN" /> |
| ... | ... | @@ -26,8 +29,9 @@ |
| 26 | 29 | <category android:name="android.intent.category.LAUNCHER" /> |
| 27 | 30 | </intent-filter> |
| 28 | 31 | </activity> |
| 29 | - <activity android:name=".LimitActivity"/> | |
| 30 | - <service android:name=".download.limit.DownLoadService"/> | |
| 32 | + <activity android:name=".LimitActivity" /> | |
| 33 | + | |
| 34 | + <service android:name=".download.limit.DownLoadService" /> | |
| 31 | 35 | </application> |
| 32 | 36 | |
| 33 | 37 | </manifest> |
| 34 | 38 | \ No newline at end of file | ... | ... |
app/src/main/java/com/cnlive/downloadfile/AudioDialogActivity.java
0 → 100644
| 1 | +package com.cnlive.downloadfile; | |
| 2 | + | |
| 3 | +import android.app.Activity; | |
| 4 | +import android.content.Context; | |
| 5 | +import android.content.Intent; | |
| 6 | +import android.os.Bundle; | |
| 7 | +import android.view.View; | |
| 8 | +import android.widget.TextView; | |
| 9 | + | |
| 10 | +import com.cnlive.downloadfile.download.limit.DownLoadService; | |
| 11 | +import com.cnlive.downloadfile.download.limit.DownloadInfo; | |
| 12 | +import com.cnlive.downloadfile.download.util.NetWorkUtil; | |
| 13 | + | |
| 14 | + | |
| 15 | +public class AudioDialogActivity extends Activity implements View.OnClickListener { | |
| 16 | + | |
| 17 | + private TextView btn_left,btn_right; | |
| 18 | + private String inType; | |
| 19 | + private DownloadInfo info; | |
| 20 | + | |
| 21 | + public static void startActivity(Context context, String inType, DownloadInfo info) { | |
| 22 | + if (!NetWorkUtil.isAppOnForeground(context)) return; | |
| 23 | + Intent intent = new Intent(context, AudioDialogActivity.class); | |
| 24 | + intent.putExtra("inType", inType); | |
| 25 | + intent.putExtra("info",info); | |
| 26 | + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
| 27 | + context.startActivity(intent); | |
| 28 | + } | |
| 29 | + | |
| 30 | + @Override | |
| 31 | + protected void onCreate(Bundle savedInstanceState) { | |
| 32 | + super.onCreate(savedInstanceState); | |
| 33 | + setContentView(R.layout.activity_audio_dialog); | |
| 34 | + initIntent(getIntent()); | |
| 35 | + btn_left = findViewById(R.id.btn_left); | |
| 36 | + btn_right = findViewById(R.id.btn_right); | |
| 37 | + btn_left.setOnClickListener(this); | |
| 38 | + btn_right.setOnClickListener(this); | |
| 39 | + } | |
| 40 | + | |
| 41 | + @Override | |
| 42 | + protected void onNewIntent(Intent intent) { | |
| 43 | + super.onNewIntent(intent); | |
| 44 | + initIntent(intent); | |
| 45 | + } | |
| 46 | + | |
| 47 | + private void initIntent(Intent intent) { | |
| 48 | + inType = intent.getStringExtra("inType"); | |
| 49 | + info = (DownloadInfo) intent.getSerializableExtra("info"); | |
| 50 | + } | |
| 51 | + | |
| 52 | + private void notifyService(String action) { | |
| 53 | + Intent intent = new Intent(this, DownLoadService.class); | |
| 54 | + intent.setAction(action); | |
| 55 | + intent.putExtra("fileUrl", info); | |
| 56 | + intent.putExtra("isNet","isNet"); | |
| 57 | + startService(intent); | |
| 58 | + } | |
| 59 | + | |
| 60 | + @Override | |
| 61 | + public void onClick(View v) { | |
| 62 | + switch (v.getId()){ | |
| 63 | + case R.id.btn_left: | |
| 64 | + finish(); | |
| 65 | + break; | |
| 66 | + case R.id.btn_right: | |
| 67 | + notifyService(inType); | |
| 68 | + finish(); | |
| 69 | + break; | |
| 70 | + } | |
| 71 | + } | |
| 72 | +} | ... | ... |
app/src/main/java/com/cnlive/downloadfile/LimitActivity.java
| ... | ... | @@ -80,10 +80,10 @@ public class LimitActivity extends AppCompatActivity implements View.OnClickList |
| 80 | 80 | |
| 81 | 81 | @Subscribe(threadMode = ThreadMode.MAIN) |
| 82 | 82 | public void update(DownloadInfo info) { |
| 83 | - if (DownloadStatus.DOWNLOAD.equals(info.getDownloadStatus())) { | |
| 83 | + if (info.getDownloadStatus().equals(DownloadStatus.DOWNLOAD)) { | |
| 84 | 84 | allStartPause.setText("全部暂停"); |
| 85 | 85 | mAdapter.updateProgress(info); |
| 86 | - } else if (DownloadStatus.DOWNLOAD_OVER.equals(info.getDownloadStatus())) { | |
| 86 | + } else if (info.getDownloadStatus().equals(DownloadStatus.DOWNLOAD_OVER)) { | |
| 87 | 87 | for (int i = 0; i < mData.size(); i++) { |
| 88 | 88 | if (mData.get(i).getUrl().equals(info.getUrl())) { |
| 89 | 89 | mData.set(i, info); |
| ... | ... | @@ -97,11 +97,11 @@ public class LimitActivity extends AppCompatActivity implements View.OnClickList |
| 97 | 97 | if (DownloadLimitManager.getInstance().getCurremtDownload() == null) { |
| 98 | 98 | allStartPause.setText("全部开始"); |
| 99 | 99 | } |
| 100 | - } else if (DownloadStatus.DOWNLOAD_PAUSE.equals(info.getDownloadStatus())) { | |
| 100 | + } else if (info.getDownloadStatus().equals(DownloadStatus.DOWNLOAD_PAUSE) || info.getDownloadStatus().equals(DownloadStatus.DOWNLOAD_NET_PAUSE)) { | |
| 101 | 101 | mAdapter.updateProgress(info); |
| 102 | 102 | boolean isAllPause = true; |
| 103 | 103 | for(int i = 0;i < mData.size();i++){ |
| 104 | - if(!mData.get(i).getDownloadStatus().equals(DownloadStatus.DOWNLOAD_PAUSE)){ | |
| 104 | + if(!mData.get(i).getDownloadStatus().equals(DownloadStatus.DOWNLOAD_PAUSE) && !info.getDownloadStatus().equals(DownloadStatus.DOWNLOAD_NET_PAUSE)){ | |
| 105 | 105 | isAllPause = false; |
| 106 | 106 | break; |
| 107 | 107 | } |
| ... | ... | @@ -109,10 +109,21 @@ public class LimitActivity extends AppCompatActivity implements View.OnClickList |
| 109 | 109 | if(isAllPause){ |
| 110 | 110 | allStartPause.setText("全部开始"); |
| 111 | 111 | } |
| 112 | - } else if (DownloadStatus.DOWNLOAD_WAIT.equals(info.getDownloadStatus())) { | |
| 112 | + } else if (info.getDownloadStatus().equals(DownloadStatus.DOWNLOAD_WAIT)) { | |
| 113 | 113 | mAdapter.updateProgress(info); |
| 114 | - } else if (DownloadStatus.DOWNLOAD_ERROR.equals(info.getDownloadStatus())) { | |
| 114 | + } else if (info.getDownloadStatus().equals(DownloadStatus.DOWNLOAD_ERROR)) { | |
| 115 | 115 | Toast.makeText(this, "下载出错", Toast.LENGTH_SHORT).show(); |
| 116 | + }else if(info.getDownloadStatus().equals(DownloadStatus.DOWNLOAD_CANCEL)){ | |
| 117 | + boolean isAllPause = true; | |
| 118 | + for(int i = 0;i < mData.size();i++){ | |
| 119 | + if(!mData.get(i).getDownloadStatus().equals(DownloadStatus.DOWNLOAD_PAUSE) && !info.getDownloadStatus().equals(DownloadStatus.DOWNLOAD_NET_PAUSE)){ | |
| 120 | + isAllPause = false; | |
| 121 | + break; | |
| 122 | + } | |
| 123 | + } | |
| 124 | + if(isAllPause){ | |
| 125 | + allStartPause.setText("全部开始"); | |
| 126 | + } | |
| 116 | 127 | } |
| 117 | 128 | } |
| 118 | 129 | ... | ... |
app/src/main/java/com/cnlive/downloadfile/adapter/LimitDownloadAdapter.java
| ... | ... | @@ -61,9 +61,9 @@ public class LimitDownloadAdapter extends RecyclerView.Adapter<LimitDownloadAdap |
| 61 | 61 | |
| 62 | 62 | final DownloadInfo info = mdata.get(position); |
| 63 | 63 | holder.name.setText(info.getUrl().substring(info.getUrl().length()-5,info.getUrl().length())); |
| 64 | - if (DownloadStatus.DOWNLOAD_WAIT.equals(info.getDownloadStatus())){ | |
| 64 | + if (info.getDownloadStatus().equals(DownloadStatus.DOWNLOAD_WAIT)){ | |
| 65 | 65 | holder.main_btn_down.setText("待下载"); |
| 66 | - }else if (DownloadStatus.DOWNLOAD_PAUSE.equals(info.getDownloadStatus())){ | |
| 66 | + }else if (info.getDownloadStatus().equals(DownloadStatus.DOWNLOAD_PAUSE) || info.getDownloadStatus().equals(DownloadStatus.DOWNLOAD_NET_PAUSE)){ | |
| 67 | 67 | holder.main_btn_down.setText("已暂停"); |
| 68 | 68 | } |
| 69 | 69 | |
| ... | ... | @@ -72,7 +72,7 @@ public class LimitDownloadAdapter extends RecyclerView.Adapter<LimitDownloadAdap |
| 72 | 72 | }else { |
| 73 | 73 | float d = info.getProgress() * holder.main_progress.getMax() / info.getTotal(); |
| 74 | 74 | holder.main_progress.setProgress((int) d); |
| 75 | - if(DownloadStatus.DOWNLOAD.equals(info.getDownloadStatus())) holder.main_btn_down.setText("下载中"); | |
| 75 | + if(info.getDownloadStatus().equals(DownloadStatus.DOWNLOAD)) holder.main_btn_down.setText("下载中"); | |
| 76 | 76 | } |
| 77 | 77 | holder.main_btn_down.setOnClickListener(new View.OnClickListener() { |
| 78 | 78 | @Override | ... | ... |
app/src/main/java/com/cnlive/downloadfile/download/limit/DownLoadService.java
| 1 | 1 | package com.cnlive.downloadfile.download.limit; |
| 2 | 2 | |
| 3 | 3 | import android.annotation.SuppressLint; |
| 4 | +import android.app.AlertDialog; | |
| 4 | 5 | import android.app.Service; |
| 5 | 6 | import android.content.BroadcastReceiver; |
| 6 | 7 | import android.content.Context; |
| 8 | +import android.content.DialogInterface; | |
| 7 | 9 | import android.content.Intent; |
| 8 | 10 | import android.content.IntentFilter; |
| 9 | 11 | import android.net.ConnectivityManager; |
| ... | ... | @@ -11,10 +13,18 @@ import android.net.NetworkInfo; |
| 11 | 13 | import android.os.Handler; |
| 12 | 14 | import android.os.IBinder; |
| 13 | 15 | import android.os.Message; |
| 16 | +import android.util.Config; | |
| 14 | 17 | import android.widget.Toast; |
| 15 | 18 | |
| 19 | +import com.cnlive.downloadfile.AudioDialogActivity; | |
| 20 | +import com.cnlive.downloadfile.MyApplication; | |
| 21 | +import com.cnlive.downloadfile.R; | |
| 16 | 22 | import com.cnlive.downloadfile.download.util.NetWorkUtil; |
| 17 | 23 | |
| 24 | +import org.greenrobot.eventbus.EventBus; | |
| 25 | + | |
| 26 | +import java.util.List; | |
| 27 | + | |
| 18 | 28 | import static com.cnlive.downloadfile.download.util.CheckNetworkStatusChangeListener.Status.TYPE_MOBILE; |
| 19 | 29 | import static com.cnlive.downloadfile.download.util.CheckNetworkStatusChangeListener.Status.TYPE_UN_NETWORK; |
| 20 | 30 | import static com.cnlive.downloadfile.download.util.CheckNetworkStatusChangeListener.Status.TYPE_WIFI; |
| ... | ... | @@ -32,6 +42,7 @@ public class DownLoadService extends Service { |
| 32 | 42 | public static final String ACTION_ALLSTART = "ACTION_ALLSTRAT"; |
| 33 | 43 | public static final String ACTION_DELECT = "ACTION_DELECT"; |
| 34 | 44 | public static final String ACION_CLEAR = "ACION_CLEAR"; |
| 45 | + private String isNet; | |
| 35 | 46 | |
| 36 | 47 | @Override |
| 37 | 48 | public int onStartCommand(Intent intent, int flags, int startId) { |
| ... | ... | @@ -50,31 +61,22 @@ public class DownLoadService extends Service { |
| 50 | 61 | mHandle.obtainMessage(5).sendToTarget(); |
| 51 | 62 | } |
| 52 | 63 | |
| 64 | + if(intent.getStringExtra("isNet") != null){ | |
| 65 | + showData(intent.getAction(),info); | |
| 66 | + return super.onStartCommand(intent, flags, startId); | |
| 67 | + } | |
| 53 | 68 | if(NetWorkUtil.getNetworkConnectionType(this).equals(TYPE_UN_NETWORK)){ |
| 54 | 69 | Toast.makeText(this,"无网络",Toast.LENGTH_SHORT).show(); |
| 55 | 70 | return super.onStartCommand(intent, flags, startId); |
| 56 | 71 | } |
| 57 | 72 | |
| 58 | 73 | if(NetWorkUtil.getNetworkConnectionType(this).equals(TYPE_WIFI)){ |
| 59 | - if (intent.getAction().equals(ACTION_START)) { | |
| 60 | - //启动初始化线程 | |
| 61 | - new MyThread(info).start(); | |
| 62 | - }else if(intent.getAction().equals(ACTION_PAUSE)){ | |
| 63 | - //暂停 | |
| 64 | - mHandle.obtainMessage(1, info).sendToTarget(); | |
| 65 | - }else if(intent.getAction().equals(ACTION_ALLPAUSE)){ | |
| 66 | - //全部暂停 | |
| 67 | - mHandle.obtainMessage(2).sendToTarget(); | |
| 68 | - }else if(intent.getAction().equals(ACTION_ALLSTART)){ | |
| 69 | - //全部开始 | |
| 70 | - mHandle.obtainMessage(3).sendToTarget(); | |
| 71 | - }else if(intent.getAction().equals(ACTION_WAITING)){ | |
| 72 | - mHandle.obtainMessage(6,info).sendToTarget(); | |
| 73 | - } | |
| 74 | + showData(intent.getAction(),info); | |
| 74 | 75 | } |
| 75 | 76 | |
| 76 | 77 | if(NetWorkUtil.getNetworkConnectionType(this).equals(TYPE_MOBILE)){ |
| 77 | 78 | //弹框...保存用户习惯,拒绝或者是允许,允许的话是仅允许一次还是都允许 |
| 79 | + tipClick(intent.getAction(),info); | |
| 78 | 80 | } |
| 79 | 81 | return super.onStartCommand(intent, flags, startId); |
| 80 | 82 | } |
| ... | ... | @@ -135,11 +137,40 @@ public class DownLoadService extends Service { |
| 135 | 137 | //子线程 获取文件长度 |
| 136 | 138 | class MyThread extends Thread { |
| 137 | 139 | private DownloadInfo fileInfo = null; |
| 138 | - public MyThread(DownloadInfo fileInfo) { | |
| 140 | + private int num = 0; | |
| 141 | + public MyThread(DownloadInfo fileInfo,int num) { | |
| 139 | 142 | this.fileInfo = fileInfo; |
| 143 | + this.num = num; | |
| 140 | 144 | } |
| 141 | 145 | public void run() { |
| 142 | - mHandle.obtainMessage(0, fileInfo).sendToTarget(); | |
| 146 | + mHandle.obtainMessage(num, fileInfo).sendToTarget(); | |
| 147 | + } | |
| 148 | + } | |
| 149 | + | |
| 150 | + | |
| 151 | + public DownLoadService getService() { | |
| 152 | + return DownLoadService.this; | |
| 153 | + } | |
| 154 | + | |
| 155 | + public void tipClick(final String type, final DownloadInfo info) { | |
| 156 | + AudioDialogActivity.startActivity(DownLoadService.this,type,info); | |
| 157 | + } | |
| 158 | + | |
| 159 | + public void showData(String type,DownloadInfo info){ | |
| 160 | + if (type.equals(ACTION_START)) { | |
| 161 | + //启动初始化线程 | |
| 162 | + new MyThread(info,0).start(); | |
| 163 | + }else if(type.equals(ACTION_PAUSE)){ | |
| 164 | + //暂停 | |
| 165 | + mHandle.obtainMessage(1, info).sendToTarget(); | |
| 166 | + }else if(type.equals(ACTION_ALLPAUSE)){ | |
| 167 | + //全部暂停 | |
| 168 | + mHandle.obtainMessage(2).sendToTarget(); | |
| 169 | + }else if(type.equals(ACTION_ALLSTART)){ | |
| 170 | + //全部开始 | |
| 171 | + new MyThread(info,3).start(); | |
| 172 | + }else if(type.equals(ACTION_WAITING)){ | |
| 173 | + mHandle.obtainMessage(6,info).sendToTarget(); | |
| 143 | 174 | } |
| 144 | 175 | } |
| 145 | 176 | |
| ... | ... | @@ -151,17 +182,31 @@ public class DownLoadService extends Service { |
| 151 | 182 | NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); |
| 152 | 183 | NetworkInfo wifiNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); |
| 153 | 184 | |
| 154 | - if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) { | |
| 185 | + if (!wifiNetInfo.isConnected()) { | |
| 155 | 186 | //网络断开 |
| 156 | - if(DownloadLimitManager.getInstance().getCurremtDownload() !=null && DownloadLimitManager.getInstance().getCurremtDownload().getUrl() != null){ | |
| 157 | - DownloadLimitManager.getInstance().pauseErrorDownload(); | |
| 158 | - } | |
| 187 | + DownloadLimitManager.getInstance().pauseErrorDownload(); | |
| 159 | 188 | } else { |
| 160 | 189 | //网络连接 |
| 161 | 190 | //启动初始化线程 |
| 162 | - if(DownloadLimitManager.getInstance().getCurremtDownload() !=null && DownloadLimitManager.getInstance().getCurremtDownload().getUrl() != null){ | |
| 163 | - new MyThread(DownloadLimitManager.getInstance().getCurremtDownload()).start(); | |
| 164 | - } | |
| 191 | + List<DownloadInfo> list = MyApplication.getInstances().getDaoSession().loadAll(DownloadInfo.class); | |
| 192 | + DownloadInfo info = null; | |
| 193 | + for(int i = 0;i < list.size();i++){ | |
| 194 | + if(list.get(i).getDownloadStatus().equals(DownloadStatus.DOWNLOAD)){ | |
| 195 | + info = list.get(i); | |
| 196 | + }else if(list.get(i).getDownloadStatus().equals(DownloadStatus.DOWNLOAD_NET_PAUSE)){ | |
| 197 | + list.get(i).setDownloadStatus(DownloadStatus.DOWNLOAD_WAIT); | |
| 198 | + EventBus.getDefault().post(list.get(i));//通知页面 | |
| 199 | + MyApplication.getInstances().getDaoSession().update(list.get(i)); | |
| 200 | + } | |
| 201 | + } | |
| 202 | + if(DownloadLimitManager.getInstance().getCurremtDownload() != null){ | |
| 203 | + info = DownloadLimitManager.getInstance().getCurremtDownload(); | |
| 204 | + } | |
| 205 | + if(info != null){ | |
| 206 | + new MyThread(info,0).start(); | |
| 207 | + }else { | |
| 208 | + DownloadLimitManager.getInstance().downNext(); | |
| 209 | + } | |
| 165 | 210 | } |
| 166 | 211 | } |
| 167 | 212 | } | ... | ... |
app/src/main/java/com/cnlive/downloadfile/download/limit/DownloadLimitManager.java
| ... | ... | @@ -183,6 +183,19 @@ public class DownloadLimitManager { |
| 183 | 183 | } |
| 184 | 184 | downCalls.remove(currentDownInfo.getUrl()); |
| 185 | 185 | } |
| 186 | + for(int i = 0;i < downWait.size();i++){ | |
| 187 | + if(downWait.get(i).getDownloadStatus().equals(DownloadStatus.DOWNLOAD_WAIT)){ | |
| 188 | + downWait.get(i).setDownloadStatus(DownloadStatus.DOWNLOAD_NET_PAUSE); | |
| 189 | + } | |
| 190 | + | |
| 191 | + if(currentDownInfo != null && downWait.get(i).getUrl().equals(currentDownInfo.getUrl())){ | |
| 192 | + currentDownInfo.setDownloadStatus(DownloadStatus.DOWNLOAD_NET_PAUSE); | |
| 193 | + downWait.set(i,currentDownInfo); | |
| 194 | + } | |
| 195 | + EventBus.getDefault().post(downWait.get(i));//通知页面 | |
| 196 | + MyApplication.getInstances().getDaoSession().update(downWait.get(i)); | |
| 197 | + } | |
| 198 | + | |
| 186 | 199 | } |
| 187 | 200 | |
| 188 | 201 | |
| ... | ... | @@ -223,6 +236,9 @@ public class DownloadLimitManager { |
| 223 | 236 | * 暂停所有的下载 |
| 224 | 237 | */ |
| 225 | 238 | public void pauseAllDownload() { |
| 239 | + List<DownloadInfo> list = MyApplication.getInstances().getDaoSession().loadAll(DownloadInfo.class); | |
| 240 | + downWait.clear(); | |
| 241 | + downWait = list; | |
| 226 | 242 | for (int i = 0; i < downWait.size(); i++) { |
| 227 | 243 | Call call = downCalls.get(downWait.get(i).getUrl()); |
| 228 | 244 | if (call != null) { | ... | ... |
app/src/main/java/com/cnlive/downloadfile/download/limit/DownloadStatus.java
| ... | ... | @@ -10,6 +10,7 @@ public class DownloadStatus { |
| 10 | 10 | public static final String DOWNLOAD_CANCEL = "cancel"; // 下载取消 |
| 11 | 11 | public static final String DOWNLOAD_OVER = "over"; // 下载结束 |
| 12 | 12 | public static final String DOWNLOAD_ERROR = "error"; // 下载出错 |
| 13 | + public static final String DOWNLOAD_NET_PAUSE = "netpause"; //网络中断状态 | |
| 13 | 14 | |
| 14 | 15 | public static final long TOTAL_ERROR = -1;//获取进度失败 |
| 15 | 16 | } | ... | ... |
app/src/main/java/com/cnlive/downloadfile/download/util/NetWorkUtil.java
| 1 | 1 | package com.cnlive.downloadfile.download.util; |
| 2 | 2 | |
| 3 | 3 | |
| 4 | +import android.app.ActivityManager; | |
| 4 | 5 | import android.content.Context; |
| 5 | 6 | import android.net.ConnectivityManager; |
| 6 | 7 | import android.net.NetworkInfo; |
| 7 | 8 | |
| 9 | +import java.util.List; | |
| 10 | + | |
| 8 | 11 | public class NetWorkUtil { |
| 9 | 12 | /** |
| 10 | 13 | * 获取当前网络类型 CheckNetworkStatusChangeListener.Status |
| ... | ... | @@ -28,4 +31,31 @@ public class NetWorkUtil { |
| 28 | 31 | } |
| 29 | 32 | return CheckNetworkStatusChangeListener.Status.TYPE_UN_NETWORK; |
| 30 | 33 | } |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + /** | |
| 38 | + * 程序是否在前台运行 | |
| 39 | + * | |
| 40 | + * @return | |
| 41 | + */ | |
| 42 | + public static boolean isAppOnForeground(Context context) { | |
| 43 | + ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); | |
| 44 | + String packageName = context.getPackageName(); | |
| 45 | + | |
| 46 | + List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager | |
| 47 | + .getRunningAppProcesses(); | |
| 48 | + if (appProcesses == null) | |
| 49 | + return false; | |
| 50 | + | |
| 51 | + for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) { | |
| 52 | + if (appProcess.processName.equals(packageName) | |
| 53 | + && appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { | |
| 54 | + return true; | |
| 55 | + } | |
| 56 | + } | |
| 57 | + | |
| 58 | + return false; | |
| 59 | + } | |
| 60 | + | |
| 31 | 61 | } |
| 32 | 62 | \ No newline at end of file | ... | ... |
app/src/main/res/drawable/bg_dialog.xml
0 → 100644
app/src/main/res/layout/activity_audio_dialog.xml
0 → 100644
| 1 | +<?xml version="1.0" encoding="utf-8"?> | |
| 2 | +<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| 3 | + android:layout_width="wrap_content" | |
| 4 | + android:layout_height="wrap_content" | |
| 5 | + android:background="#01000000"> | |
| 6 | + | |
| 7 | + <RelativeLayout | |
| 8 | + android:layout_width="wrap_content" | |
| 9 | + android:layout_height="wrap_content" | |
| 10 | + android:background="@drawable/bg_dialog" | |
| 11 | + android:layout_centerInParent="true" | |
| 12 | + android:layout_margin="20dp"> | |
| 13 | + | |
| 14 | + <TextView | |
| 15 | + android:id="@+id/content" | |
| 16 | + android:layout_width="wrap_content" | |
| 17 | + android:layout_height="wrap_content" | |
| 18 | + android:layout_centerHorizontal="true" | |
| 19 | + android:layout_marginLeft="24dp" | |
| 20 | + android:layout_marginTop="28dp" | |
| 21 | + android:layout_marginRight="24dp" | |
| 22 | + android:layout_marginBottom="25dp" | |
| 23 | + android:text="@string/audio_mobile_net_dialog_title" | |
| 24 | + android:textColor="#282828" | |
| 25 | + android:textSize="17sp" /> | |
| 26 | + | |
| 27 | + <View | |
| 28 | + android:id="@+id/line1" | |
| 29 | + android:layout_width="match_parent" | |
| 30 | + android:layout_height="1px" | |
| 31 | + android:layout_below="@id/content" | |
| 32 | + android:background="#ff09141f" /> | |
| 33 | + | |
| 34 | + <LinearLayout | |
| 35 | + android:id="@+id/buttons" | |
| 36 | + android:layout_width="match_parent" | |
| 37 | + android:layout_height="45dp" | |
| 38 | + android:layout_below="@+id/line1" | |
| 39 | + android:orientation="horizontal"> | |
| 40 | + | |
| 41 | + <TextView | |
| 42 | + android:id="@+id/btn_left" | |
| 43 | + android:layout_width="0dp" | |
| 44 | + android:layout_height="match_parent" | |
| 45 | + android:layout_weight="1" | |
| 46 | + android:gravity="center" | |
| 47 | + android:text="@string/audio_mobile_net_dialog_cancel" | |
| 48 | + android:textColor="#000000" | |
| 49 | + android:textSize="17sp" /> | |
| 50 | + | |
| 51 | + <View | |
| 52 | + android:id="@+id/line2" | |
| 53 | + android:layout_width="1px" | |
| 54 | + android:layout_height="match_parent" | |
| 55 | + android:background="#ff09141f" /> | |
| 56 | + | |
| 57 | + <TextView | |
| 58 | + android:id="@+id/btn_right" | |
| 59 | + android:layout_width="0dp" | |
| 60 | + android:layout_height="match_parent" | |
| 61 | + android:layout_weight="1" | |
| 62 | + android:gravity="center" | |
| 63 | + android:text="@string/audio_mobile_net_dialog_continue" | |
| 64 | + android:textColor="#000000" | |
| 65 | + android:textSize="17sp" /> | |
| 66 | + | |
| 67 | + </LinearLayout> | |
| 68 | + </RelativeLayout> | |
| 69 | + | |
| 70 | + | |
| 71 | +</RelativeLayout> | |
| 0 | 72 | \ No newline at end of file | ... | ... |
app/src/main/res/values/strings.xml
app/src/main/res/values/styles.xml
| ... | ... | @@ -7,5 +7,15 @@ |
| 7 | 7 | <item name="colorPrimaryDark">@color/colorPrimaryDark</item> |
| 8 | 8 | <item name="colorAccent">@color/colorAccent</item> |
| 9 | 9 | </style> |
| 10 | - | |
| 10 | + <style name="CustomDialogStyle" parent="@android:style/Theme.Dialog"> | |
| 11 | + <item name="android:windowFrame">@null</item> | |
| 12 | + <item name="android:windowIsFloating">true</item> | |
| 13 | + <item name="android:windowIsTranslucent">true</item> | |
| 14 | + <item name="android:windowNoTitle">true</item> | |
| 15 | + <item name="android:background">@android:color/transparent</item> | |
| 16 | + <item name="android:windowBackground">@android:color/transparent</item> | |
| 17 | + <item name="android:backgroundDimEnabled">true</item> | |
| 18 | + <item name="android:backgroundDimAmount">0.6</item> | |
| 19 | + <item name="android:windowCloseOnTouchOutside">true</item> | |
| 20 | + </style> | |
| 11 | 21 | </resources> | ... | ... |