带进度条的通知栏Notification
来源:互联网 发布:plc编程小游戏 编辑:程序博客网 时间:2024/05/29 19:05
在版本迭代时下载新版本的时候有些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;import android.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>
0 0
- 带进度条的通知栏Notification
- Notification通知实现自定义和带进度条
- android 带ProgressBar的Notification通知栏
- Android带下载进度条的通知栏
- Android更新带进度条的通知栏
- Notification进度条通知
- android-带进度条的系统通知栏消息
- android-带进度条的系统通知栏消息
- -------------------------android-带进度条的系统通知栏消息
- 通知栏Notification自定义视图方法(显示进度条)
- Android 检查更新版本(带有通知栏,进度条,Notification)
- 通知栏Notification自定义视图方法(显示进度条)
- 通知栏通知:Notification的实现
- Notification消息通知 通知栏的实现
- android通知栏的消息通知(Notification)
- Notification通知栏的使用
- Notification通知栏的使用
- 通知栏Notification的整合
- Android vector标签 PathData 画图超详解
- 【干货分享】流程DEMO-付款申请单
- 支持向量机(下)
- cogs742 [网络流24题]深海机器人
- 火狐浏览器看过的视频怎么提取出来
- 带进度条的通知栏Notification
- mac nmap 的下载
- SQLServer2012通过链接服务器执行SQLServer2000的存储过程的问题
- 今日使用学习MySql指令
- CSS—清除浮动的几种方式
- MockHttpServletRequest对于http前台信息的验证
- 驱动安装+打包程序
- PCR查找、提取、校正
- Android OpenCV Camera preview 横屏以及不全屏的问题