自定义Notification模拟数据下载状态栏的提示
2016-05-31 14:59
218 查看
运行效果图:
修改activity_main.xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="自定义Notification控件展示系统提示下载" /> <Button android:id="@+id/btnSend" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="发送下载命令"/> <Button android:id="@+id/btnClear" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="清除下载进度条"/> </LinearLayout>
新建自定义mynotification.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="match_parent" android:orientation="vertical" > <!-- 自定义的Notification布局 --> <TextView android:id="@+id/Tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="left" android:text="正在下载" /> <!-- 定义一个进度条 --> <ProgressBar android:id="@+id/Pb" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" /> </LinearLayout>
修改MainActivity.java文件,代码:
import android.os.Bundle; import android.os.Handler; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.ProgressBar; import android.widget.RemoteViews; public class MainActivity extends Activity { private Button btnSend,btnClear;//定义Button控件 //定义NotificationManager对象 private NotificationManager nm; //定义一个Notification对象 private Notification notification; //记录进度条的进度 private int count=0; //记录进度条是否取消 private Boolean isClear=false; //定义Notification的id private int notification_id=1; //定义主线程的Handler对象 private Handler handler=new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE);//清楚标题 setContentView(R.layout.activity_main); findById();//获取控件对象 SendNotification(); } private void SendNotification() { //得到NotificationManager的服务对象 nm=(NotificationManager) getSystemService(NOTIFICATION_SERVICE); ////初始化Notification notification=new Notification(R.drawable.bg, "开始下载", System.currentTimeMillis()); //得到Notification的视图对象 notification.contentView=new RemoteViews(getPackageName(),R.layout.mynotification); //设置视图中的ProgressBar对象 notification.contentView.setProgressBar(R.id.Pb, 100, 0, false); //定义单机通知的事件 Intent notificationIntent=new Intent(this, MainActivity.class); PendingIntent contentIntent= PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.contentIntent=contentIntent; } //自定义按钮单机监听器 OnClickListener mylis=new OnClickListener() { @Override public void onClick(View view) { switch (view.getId()) { case R.id.btnClear: nm.cancel(notification_id);//取消notification break; case R.id.btnSend: showNotification(); handler.post(run); break; default: break; } } }; //定义线程Runnable对象进行进度更新 Runnable run=new Runnable() { @Override public void run() { //如果线程没取消 if(!isClear){ count++;//更新进度 notification.contentView.setProgressBar(R.id.Pb, 100, count, false); //更新notification就是更新进度条 showNotification(); if(count<100){ //两百毫秒之后count+1 handler.postDelayed(run,200); } if(count==100){ isClear=true; nm.cancel(notification_id);//取消notification sendNotification(R.drawable.bg, "下载成功", "水木年华.MP3", "5.60M"); } } } }; //显示Notification private void showNotification() { nm.notify(notification_id, notification); } private void findById() { btnSend=(Button) findViewById(R.id.btnSend); btnSend.setOnClickListener(mylis); btnClear=(Button) findViewById(R.id.btnClear); btnClear.setOnClickListener(mylis); } //发送一个Notification系统通知,参数1:图片ID,参数2:显示notification对象的提示内容,参数3:状态栏中显示的标题,参数4:状态栏中的信息 private void sendNotification(int imageId,String titleText,String showTitleText,String content){ //得到系统的Notification服务对象 NotificationManager manager=(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); //创建一个Notification对象 Notification notification=new Notification(); //设置显示的Notification对象的图标 // notification.icon=R.drawable.notification_bg; notification.icon=imageId;//接收参数的图片信息 //设置显示的notification对象的内容 // notification.tickerText="您有一条新的消息!"; notification.tickerText=titleText;//接收参数的内容 //设置显示Notification对象的声音 notification.defaults=Notification.DEFAULT_SOUND; //设置显示notification对象的声音模拟 notification.audioStreamType=android.media.AudioManager.ADJUST_LOWER; //定义单机Notification的时间Intent Intent intent=new Intent(this, MainActivity.class); PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); //单机状态栏的图标出现的提示信息设置 // notification.setLatestEventInfo(this, "消息内容", "认识你很高心!", pendingIntent); notification.setLatestEventInfo(this, showTitleText, content, pendingIntent); //发送Notification消息 manager.notify(1, notification); } }
其中:是借用的模拟短信提醒的demo,个人查看地址:http://blog.csdn.net/zsh157621866651/article/details/51545002
相关文章推荐
- Notification系统提示,模拟短信消息
- ASP.NET MVC企业级项目框架搭建实战
- (Java)LeetCode-21. Merge Two Sorted Lists
- linux 用户授权
- MIT6.828 HW6: Threads and Locking
- android的versionCode与versionName的区别
- JavaScript table, grid, tree view or Gantt chart | TreeGrid
- android中src和background区别
- UI手势
- js 数组常用方法总结
- Light OJ - 1008 - Fibsieve`s Fantabulous Birthday(数学,找规律)
- 服务器上测试flask rest-api
- shell中的括号与引号
- 设计模式--Builder
- dom4j解析xml文件
- 7. xdebug
- Animation & Animator使用方法(Mooc Android加薪利器--自定义view 代码学习笔记)
- java spring hibernate
- gettid 和pthread_self的区别
- Linux系统实现mysql主从复制