帶進度條的通知欄Notification
阿新 • • 發佈:2019-01-01
在版本迭代時下載新版本的時候有些APP展示帶進度條的通知欄,對使用者而言感覺更為友好,以下是在Activity裡的簡單實現.
截圖如下:
程式碼如下:
import android.app.Notification; import android.app.NotificationManager; import android.os.Bundle; import android.os.SystemClock; import android.support.v4.app.NotificationCompat; import android.support.v7.app.AppCompatActivity; importandroid.widget.RemoteViews; import java.util.Locale; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; public class MainActivity extends AppCompatActivity { private NotificationCompat.Builder builder; private NotificationManager manager; private ScheduledFuture<?> future; private int count;//模似進度 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); future = executor.scheduleAtFixedRate(new MyThread(), 0, 100, TimeUnit.MILLISECONDS); } private Notification customNotification(int progress, String text) {//自定義View通知 if (builder == null) builder = new NotificationCompat.Builder(this); RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification_upgrade); view.setProgressBar(R.id.bar, 100, progress, false); view.setTextViewText(R.id.tv_des, text); view.setTextViewText(R.id.tv_progress, String.format(Locale.getDefault(), "%d%%", progress)); builder.setCustomContentView(view) .setSmallIcon(R.mipmap.ic_launcher) .setAutoCancel(true); return builder.build(); } //這裡為了方便閱讀就放在Activity裡,實際情況放在Service裡更好 private class MyThread implements Runnable { @Override public void run() { manager.notify("Download", 0, customNotification(++count, "下載中")); if (count == 100) { manager.notify("Download", 0, customNotification(count, "下載完成了")); SystemClock.sleep(3000); manager.cancel("Download", 0); if (future != null) future.cancel(false); } } } @Override protected void onDestroy() { super.onDestroy(); if (future != null) future.cancel(true); } }
自定義通知的佈局檔案notification_upgrade.xml如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" android:paddingBottom="5dp" android:paddingLeft="10dp" android:paddingRight="10dp" android:paddingTop="5dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/app_name" android:scaleType="centerInside" android:src="@mipmap/ic_launcher" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginStart="10dp" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/tv_des" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/black" android:textStyle="bold" /> <TextView android:id="@+id/tv_progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginStart="10dp" android:textColor="@color/black" android:textStyle="bold" /> </LinearLayout> <ProgressBar android:id="@+id/bar" style="@style/Widget.AppCompat.ProgressBar.Horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>