package com.hjly.performancetestdemo;
import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import com.hjly.performancetestdemo.databinding.ActivityMainBinding;
import java.math.BigDecimal;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
private Handler handler;
private Runnable runnable;
private Runnable runnable_loop;
private String storage_can_use;
private float cpu_rate = 0;
private AlertDialog dialog;
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
private boolean havePermission = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
handler = new Handler();
updateData();
binding.btn50Cpu.setOnClickListener(view -> {
if (!havePermission) {
return;
}
createLoop();
createLoop();
createLoop();
createLoop();
});
}
@Override
protected void onResume() {
super.onResume();
checkPermission();
}
private void checkPermission() {
//检查权限(NEED_PERMISSION)是否被授权 PackageManager.PERMISSION_GRANTED表示同意授权
if (Build.VERSION.SDK_INT >= 30) {
if (!Environment.isExternalStorageManager()) {
if (dialog != null) {
dialog.dismiss();
dialog = null;
}
dialog = new AlertDialog.Builder(this)
.setTitle("提示")//设置标题
.setMessage("请开启文件访问权限,否则无法正常使用本应用!")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
dialog.dismiss();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
startActivity(intent);
}
}).create();
dialog.show();
} else {
havePermission = true;
Log.i("swyLog", "Android 11以上,当前已有权限");
}
} else {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
//申请权限
if (dialog != null) {
dialog.dismiss();
dialog = null;
}
dialog = new AlertDialog.Builder(this)
.setTitle("提示")//设置标题
.setMessage("请开启文件访问权限,否则无法正常使用本应用!")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
}
}).create();
dialog.show();
} else {
havePermission = true;
Log.i("swyLog", "Android 6.0以上,11以下,当前已有权限");
}
} else {
havePermission = true;
Log.i("swyLog", "Android 6.0以下,已获取权限");
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_EXTERNAL_STORAGE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
havePermission = true;
Toast.makeText(this, "授权成功!", Toast.LENGTH_SHORT).show();
} else {
havePermission = false;
Toast.makeText(this, "授权被拒绝!", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
private void createLoop() {
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
TestProcess.writePng(MainActivity.this);
Log.i("swyLog", "cpu_rate is : " + cpu_rate);
}
}
}).start();
}
public void updateData() {
if (null != handler && runnable != null) {
handler.removeCallbacks(runnable);
runnable = null;
}
runnable = new Runnable() {
@Override
public void run() {
refreshParams();
handler.postDelayed(this, (1 * 1000));
}
};
handler.post(runnable);
}
private void refreshParams() {
storage_can_use = Utils.getAvailMemory();
String storage_total = Utils.getTotalMemory();
cpu_rate = Utils.getCpuUsed();
boolean outside_available = Utils.isExternalStorageAvailable();
String inside_can_use = Utils.getAvailableInternalMemorySize();
String inside_total = Utils.getTotalInternalMemorySize();
String outside_can_use = Utils.getAvailableExternalMemorySize();
String outside_total = Utils.getTotalExternalMemorySize();
binding.tvStorageCanUse.setText(!"".equals(storage_can_use) ? storage_can_use : "");
binding.tvStorageTotal.setText(!"".equals(storage_total) ? storage_total : "");
binding.tvInsideCanUse.setText(!"".equals(inside_can_use) ? inside_can_use : "");
binding.tvInsideTotal.setText(!"".equals(inside_total) ? inside_total : "");
binding.tvOutsideCanUse.setText(!"".equals(outside_can_use) ? outside_can_use : "");
binding.tvOutsideTotal.setText(!"".equals(outside_total) ? outside_total : "");
binding.tvCpuRate.setText(formatData((cpu_rate * 100)) + "%");
binding.tvOutsideAvailable.setText(outside_available ? "true" : "false");
}
private String formatData(double old) {
BigDecimal bigDecimal = new BigDecimal(old);
return bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).toString();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (runnable != null) {
handler.removeCallbacks(runnable);
}
}
}