Skip to content

Commit

Permalink
1.欠流量统计功能 ✔
Browse files Browse the repository at this point in the history
2.http 服务器响应数据异常 更新ui 未解决  ✔
3.下拉列表无删除功能
4.下拉列表 无限增加连接 ✔
5.app 再次打开 无法恢复上次使用的url  ✔
6.实时网速有问题 ✔
7.后台运行按钮需要调整 ✔
  • Loading branch information
1174657425436453 committed Sep 25, 2022
1 parent 043de57 commit 3bebc1c
Show file tree
Hide file tree
Showing 10 changed files with 292 additions and 101 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'org.jetbrains:annotations:20.1.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Expand Down
Binary file removed app/debug/网速Beta.apk
Binary file not shown.
43 changes: 42 additions & 1 deletion app/src/main/java/com/baiyu/cewangsu/ApplicationFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static void createConfigurationFile(Activity act){

if(jsonString.length() == 0){
System.out.println("文件空内容:写入成功");
jsonString="{\"url\":[\"https:\\/\\/baidu.com\",\"https:\\/\\/pm.myapp.com\\/invc\\/xfspeed\\/qqpcmgr\\/download\\/QQPCDownload320001.exe\"],\"TCP\":true,\"UP\":false,\"switch\":true,\"pos\":1}";
jsonString="{\"url\":[\"https:\\/\\/gameplus-platform.cdn.bcebos.com\\/gameplus-platform\\/upload\\/file\\/source\\/9752b85b3f8bddf3c09e1f6b41433d27.apk\",\"https:\\/\\/pm.myapp.com\\/invc\\/xfspeed\\/qqpcmgr\\/download\\/QQPCDownload320001.exe\"],\"TCP\":true,\"UP\":false,\"switch\":true,\"pos\":1}";

}
else {
Expand Down Expand Up @@ -166,6 +166,7 @@ public static void getArraySpinnerFromFile(List<String> spinnerData){
for (int i = 0; i < jsonArray1.length(); i++) {
//System.out.println("hello:"+jsonArray1.getString(i));
spinnerData.add(jsonArray1.getString(i));

}

} catch (JSONException e) {
Expand Down Expand Up @@ -214,4 +215,44 @@ public static int getUrlRecord(){
throw new RuntimeException(e);
}
}


public static void setTotalData(long totalData){
jsonObject.remove("totalData");
try {
jsonObject.put("totalData",totalData);
loadFile();
} catch (JSONException e) {
throw new RuntimeException(e);
}

}

public static long getTotalData(){
try {
return jsonObject.getLong("totalData");
} catch (JSONException e) {
return 0;
}
}


public static void setBackGroupRun(boolean bool){
try {
jsonObject.remove("backRun");
jsonObject.put("backRun",bool);//true 为需要开启网速检测
} catch (JSONException e) {
throw new RuntimeException(e);
}

}


public static boolean getBackGroupRun(){
try {
return jsonObject.getBoolean("backRun");
} catch (JSONException e) {//获取不到
return true;
}
}
}
2 changes: 2 additions & 0 deletions app/src/main/java/com/baiyu/cewangsu/DownloadServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ public int onStartCommand(Intent intent, int flags, int startId) {
}*/

//System.out.println("url"+DownloadThread.url.split(":")[0].length());

downloadThread = new DownloadTeadUDPUp();//UDP 上传
ApplicationFile.setJsonObjectUrl(intent.getStringExtra("url"));
}
else {

Expand Down
116 changes: 100 additions & 16 deletions app/src/main/java/com/baiyu/cewangsu/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,32 @@
import android.net.Uri;
import android.os.*;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.*;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import org.w3c.dom.Text;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
* 欠流量统计功能
* 欠流量统计功能
* http 服务器响应数据异常 更新ui 未解决 ✔
* 下拉列表无删除功能
* 下拉列表 无限增加连接 ✔
* app 再次打开 无法恢复上次使用的url ✔
* 实时网速有问题
* 后台运行按钮需要调整
* 实时网速有问题
* 后台运行按钮需要调整
* 重要一点 app 需要重构 代码沉余验证
* 作者对于权限 也是迷迷糊糊 不知道要不要申请
* 作者对于应用权限 也是迷迷糊糊 不知道要不要申请
* */
public class MainActivity extends AppCompatActivity implements View.OnClickListener, SeekBar.OnSeekBarChangeListener,RadioGroup.OnCheckedChangeListener,AdapterView.OnItemSelectedListener {
public class MainActivity extends AppCompatActivity implements View.OnClickListener, SeekBar.OnSeekBarChangeListener,RadioGroup.OnCheckedChangeListener,AdapterView.OnItemSelectedListener , TextWatcher {
//下载链接
EditText downloadUrl;
//开始按钮
Expand Down Expand Up @@ -68,6 +72,10 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe

static TextView logError;

TextView totalData;//历史总流量

NetWorkSpeedUtils netWorkSpeedUtils;//为了能结束实时网速 没有版本

/**下拉列表第一次打开 自动就执行了监听方法 气死了 试了其他方法都不行, 只能这样了*/
static boolean isFirst=true;//true 表示第一次打开app
@Override
Expand All @@ -77,6 +85,8 @@ protected void onCreate(Bundle savedInstanceState) {

logError=findViewById(R.id.logArea);
downloadUrl=findViewById(R.id.download_url);
downloadUrl.addTextChangedListener(this);

startButton= findViewById(R.id.start_button);
startButton.setOnClickListener(this);//开始按钮

Expand All @@ -95,11 +105,11 @@ protected void onCreate(Bundle savedInstanceState) {
seekBar.setOnSeekBarChangeListener(this);
backrun=findViewById(R.id.back_run);
backrun.setOnClickListener(this);
backrun.setEnabled(false);
//backrun.setEnabled(false);
radioButtonUp=findViewById(R.id.up);//上传单选
radioButtonDown=findViewById(R.id.down);//下载
wangSu=findViewById(R.id.wang_su);

totalData=findViewById(R.id.totalData);
radioGroup1 = findViewById(R.id.is_down);
radioGroup1.setOnCheckedChangeListener(this);//监听选中上传还是下载

Expand All @@ -108,6 +118,21 @@ protected void onCreate(Bundle savedInstanceState) {

ApplicationFile.createConfigurationFile(this);//创建配置文件 记录用过的连接



if(ApplicationFile.getTotalData()<=1048576){
String s = new DecimalFormat("0.00").format(ApplicationFile.getTotalData() / 1024d) + "MB";
totalData.setText(s);
} else if (ApplicationFile.getTotalData()<=1073741824) {
String s = new DecimalFormat("0.00").format(ApplicationFile.getTotalData() / 1024 / 1024d) + "GB";
totalData.setText(s);
}
else {
String s = new DecimalFormat("0.00").format(ApplicationFile.getTotalData() / 1024 /1024/ 1024d) + "TB";
totalData.setText(s);
}


//文件 获取按钮状态
boolean[] bool=ApplicationFile.getSwitchCondiction();
if (bool[0]) {
Expand All @@ -129,19 +154,29 @@ protected void onCreate(Bundle savedInstanceState) {
boolean boolSwitch=ApplicationFile.getStartAndStop();//获取开始和停止按钮信息
if (boolSwitch) {
startButton.setEnabled(true);stopButton.setEnabled(false);//true 开始可以

}
else {
startButton.setEnabled(false);stopButton.setEnabled(true);//false 开始不可以
}
NetWorkSpeedUtils.totalData = ApplicationFile.getTotalData();//KB 一开始就要文件获取总流量

//System.out.println("hhheee:"+NetWorkSpeedUtils.totalData);
ApplicationFile.getArraySpinnerFromFile(spinnerDate);//获取json 对象数组连接 添加到列表
arrayAdapter=new ArrayAdapter<>(this,android.R.layout.simple_spinner_item,spinnerDate);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);

spinnerURL.setAdapter(arrayAdapter);//下拉列表添加适配器


//为下拉列表设置监听器
spinnerURL.setOnItemSelectedListener(this);

if(!ApplicationFile.getBackGroupRun()){//true 为起用
startCheckSpeed();
netWorkSpeedUtils=new NetWorkSpeedUtils(this,wsHnadler);
netWorkSpeedUtils.startShowNetSpeed();
}
}


Expand All @@ -160,7 +195,7 @@ public void onClick(View view) {
startActivityForResult(myFileIntent,10);//最后会返回数据到在本类的onActivityResult方法(也就是调用这个方法)
break;
case R.id.stop_button:
stopNetwork();
stopNetwork();//停止下载
break;
case R.id.back_run:
backRun();//后台运行
Expand All @@ -174,17 +209,24 @@ private void openNetWork(){
}
}
private void backRun(){//后台运行
ApplicationFile.setStartAndStop(false);
// System.out.println("12345789"+startButton.isClickable());

ApplicationFile.setBackGroupRun(ApplicationFile.getStartAndStop());//开始按钮决定是否开启网速
finish();
}
private void stopNetwork(){//点击停止按钮执行该函数
netWorkSpeedUtils.task.cancel();//结束网速检测 但是网速没有置0

wangSu.setText("网速:无网络传输");
ApplicationFile.setStartAndStop(true);//开始按钮起用
startButton.setEnabled(true);
stopButton.setEnabled(false);
//stopService(new Intent(MainActivity.this,DownloadServer.class));
DownloadThread.isRun=false;
DownloadTeadUDPUp.isRun=false;

ApplicationFile.setTotalData(NetWorkSpeedUtils.totalData*1024);

}

private void downloadDisribution(){
Expand Down Expand Up @@ -219,6 +261,8 @@ public void handleMessage(Message msg){
}
};

ApplicationFile.setStartAndStop(false);//false 开始按钮不起用

if(TCP) {
if(UP){
// downloadThread = new DownloadThread();//TCP 下载
Expand Down Expand Up @@ -249,9 +293,11 @@ public void handleMessage(Message msg){


DownloadThread.loghandler=this.logHandler;

ApplicationFile.removeSwitchCondiction();
ApplicationFile.setSwitchCondiction(false,true);

//ApplicationFile.setBackGroupRun(false);//记录开始按钮状态 false 开始按钮不可用
}
else {
Toast.makeText(this,"暂时不支持UDP 下载",Toast.LENGTH_SHORT).show();
Expand All @@ -261,24 +307,38 @@ public void handleMessage(Message msg){
if (ApplicationFile.isExisitInArray(downloadUrl.getText().toString())) {//true 表示连接不存在 要加连接?
ApplicationFile.insertUrlRecord(spinnerDate.size());//下拉列表长度加1
}




startService(intent);


startButton.setEnabled(false);
stopButton.setEnabled(true);
backrun.setEnabled(true);//点击开始按钮才可以点击后台执行
//backrun.setEnabled(true);//点击开始按钮才可以点击后台执行
startCheckSpeed();




netWorkSpeedUtils=new NetWorkSpeedUtils(this,wsHnadler);
netWorkSpeedUtils.startShowNetSpeed();
}

private void startCheckSpeed(){
wsHnadler= new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 100:
wangSu.setText("系统网速:" + msg.obj.toString());
break;
wangSu.setText("网速:" + msg.obj.toString());
totalData.setText(NetWorkSpeedUtils.total);
break;
}
super.handleMessage(msg);
//super.handleMessage(msg);
}
};

new NetWorkListenerUtils(this,wsHnadler).startShowNetSpeed();
}

@Override
Expand Down Expand Up @@ -328,7 +388,7 @@ protected void onActivityResult(int requestCode,int resultCode,Intent data) {

@Override//下拉列表的监听
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
System.out.println("wuwuwuwuwuwu");

if(isFirst){
downloadUrl.setText(arrayAdapter.getItem(ApplicationFile.getUrlRecord()));
isFirst=false;
Expand All @@ -338,6 +398,8 @@ public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)

//其他靠点击 下拉列表获取位置
ApplicationFile.insertUrlRecord(i);


downloadUrl.setText(arrayAdapter.getItem(i));
}
}
Expand All @@ -346,4 +408,26 @@ public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)
public void onNothingSelected(AdapterView<?> adapterView) {//Adapter 为空时候执行这个方法

}


@Override
protected void onDestroy() {
ApplicationFile.setTotalData(NetWorkSpeedUtils.totalData);
super.onDestroy();
}

@Override//url 文本监听
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override//url 文本监听
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override//url 文本监听
public void afterTextChanged(Editable editable) {
//downloadUrl.setHint("");
}
}
Loading

0 comments on commit 3bebc1c

Please sign in to comment.