1. 程式人生 > >下載進度條 加百分比

下載進度條 加百分比

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

    <Button
android:text="顯示通知"
android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button" android:onClick="show1" /> </RelativeLayout>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission 
android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
package com.example.text;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.AppOpsManager;
import android.app.Notification;
import 
android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.graphics.BitmapFactory; import android.graphics.Color; import android.os.Build; import android.os.SystemClock; import android.provider.Settings; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.Date; public class MainActivity extends AppCompatActivity { private NotificationManager manager; Notification.Builder builder; //設定日期格式 private SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss"); private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = findViewById(R.id.button); } // private void requestPermission() { // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.BASE) { // // 進入設定系統應用許可權介面 // Intent intent = new Intent(Settings.ACTION_SETTINGS); // startActivity(intent); // return; // } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {// 執行系統在5.x環境使用 // // 進入設定系統應用許可權介面 // Intent intent = new Intent(Settings.ACTION_SETTINGS); // startActivity(intent); // return; // } // return; // } public void show1(View v){ manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); builder=new Notification.Builder(this); builder.setTicker("檔案開始下載"); builder.setSmallIcon(R.mipmap.ic_launcher); builder.setContentTitle("檔案下載"); builder.setContentText("檔案正在下載中......"); builder.setWhen(SystemClock.currentThreadTimeMillis()); builder.setDefaults(Notification.DEFAULT_LIGHTS);//訊息提示模式 builder.setProgress(100,0,false); //模擬正在執行下載 new Thread(new Runnable() { @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override public void run() { for (int i=1;i<=100;i++){ builder.setProgress(100, i, false); if(i==100){ builder.setProgress(0, 0,false ); runOnUiThread(new Runnable() { @Override public void run() { button.setText("下載完畢"); } }); } final int finalI = i; runOnUiThread(new Runnable() { @Override public void run() { if(finalI%3==1){ builder.setContentText("檔案下載中. "+ finalI+"%"); }else if(finalI%3==2){ builder.setContentText("檔案下載中.. "+ finalI+"%"); }else{ builder.setContentText("檔案下載中... "+ finalI+"%"); } if(finalI==100){ builder.setContentText("檔案下載完畢!"); } } }); manager.notify(1, builder.build()); SystemClock.sleep(300); } //manager.cancel(1); } }).start(); } }