个人作业冲刺(四)
package com.example.temperature; import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import android.Manifest; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.StrictMode; import android.view.View; import android.widget.Button; import android.widget.Toast; import java.io.File; public class InfoActivity extends AppCompatActivity { private AlertDialog alertDialog; String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}; private int REQUEST_PERMISSION_CODE = 1000; //请求权限 private void requestPermission() { if (Build.VERSION.SDK_INT > 23) { if (ContextCompat.checkSelfPermission(InfoActivity.this, permissions[0]) == PackageManager.PERMISSION_GRANTED) { } else { requestPermissions(permissions, REQUEST_PERMISSION_CODE); } } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_info); requestPermission(); StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder.build()); builder.detectFileUriExposure(); Button add =(Button) findViewById(R.id.add); add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(InfoActivity.this,AddInfoActicity.class); startActivity(intent); } }); Button count = findViewById(R.id.account); count.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(InfoActivity.this,CountActivity.class); startActivity(intent); } }); Button table = findViewById(R.id.table); table.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { exportExcel(InfoActivity.this); AlertDialog.Builder dialog = new AlertDialog.Builder(InfoActivity.this); dialog.setTitle("提示"); dialog.setMessage("创建成功"); dialog.setCancelable(false); dialog.setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(InfoActivity.this,"创建成功",Toast.LENGTH_SHORT).show(); } }); dialog.show(); } }); final Button zhexian=(Button)findViewById(R.id.zhexian); zhexian.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(InfoActivity.this,zhexian.class); startActivity(intent); } }); Button ditu=(Button)findViewById(R.id.ditu); ditu.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("https://me.bdp.cn/api/su/1QLYZ7X0")); startActivity(intent); } }); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == REQUEST_PERMISSION_CODE) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { } else { AlertDialog.Builder builder = new AlertDialog.Builder(InfoActivity.this); builder.setTitle("permission"); builder.setMessage("点击允许才可以使用"); builder.setPositiveButton("去允许", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (alertDialog != null && alertDialog.isShowing()) { alertDialog.dismiss(); } ActivityCompat.requestPermissions(InfoActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); } }); alertDialog = builder.create(); alertDialog.setCanceledOnTouchOutside(false); alertDialog.show(); } } } private void showDialogTipUserRequestPermission() { ActivityCompat.requestPermissions(this, permissions, 321); } //导出 private void exportExcel(Context context) { String path = getExternalFilesDir("test").getPath(); File file = new File(path+"/"+"xx"); if (!file.exists()) { file.mkdirs(); } File files = new File(file.getPath()+"/"+"text.xls"); ExcelUtil.initExcel(files); } }