Notification 模拟收到短信,数据下载的状态栏提示
来源:互联网 发布:大数据时代与法治政府 编辑:程序博客网 时间:2024/04/29 18:43
Notification 模拟收到短信
功能代码实现
第二:定义消息通知的布局,进度条显示
监听按钮
得到NotificationManager的服务对象
初始化并得到Notification的视图对象,设置progressBar对象
定义单击通知事件。取消事件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="单击发送Notification信息" /></LinearLayout>
功能代码实现
package com.ncsyeyy.YeyyNotificaton;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.media.AudioManager;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MyActivity extends Activity implements OnClickListener { private Button btn; /** * Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findView(); setOnClick(); } private void findView(){ btn = (Button) findViewById(R.id.btn); } private void setOnClick(){ btn.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn: sendNotification(); break; } } private void sendNotification(){// 得到系统的Notification服务对象 NotificationManager manager=(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);// 创建一个Notification对象图标 Notification notification=new Notification();// 设置显示的Notification对象图标 notification.icon=R.drawable.ic_launcher;// 设置显示Notification对象的内容 notification.tickerText="您有一条新的短信!";// 设置显示Notification对象的声音模式 notification.audioStreamType= AudioManager.ADJUST_LOWER;// 定义单击Notification的事件Intent Intent intent=new Intent(this,MyActivity.class); PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);// 单击状态栏的图标出现的提示信息设置 notification.setLatestEventInfo(this,"短信提示内容","我是一个短消息,愚人节快乐!",pendingIntent);// 发送pendingIntent消息 manager.notify(1,notification); }}
思路:
第一:定义布局按钮
第二:定义消息通知的布局,进度条显示
第三:代码实现
监听按钮
得到NotificationManager的服务对象
初始化并得到Notification的视图对象,设置progressBar对象
定义单击通知事件。取消事件
第一:定义布局按钮
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/btnSend" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="发送下载Notification" /> <Button android:id="@+id/btnClean" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="清除Notification" /></LinearLayout>
第二:定义消息通知的布局,进度条显示
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <!--定义通知布局的文本框--> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下载中……" android:textSize="20sp" android:textColor="@android:color/white" /> <!--定义下载进度progressbar控件--> <ProgressBar android:id="@+id/pb" style="?android:attr/progressBarStyleHorizontal" android:layout_width="260dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" /></LinearLayout>
监听按钮
得到NotificationManager的服务对象
初始化并得到Notification的视图对象,设置progressBar对象
定义单击通知事件。取消事件
package com.ncsyeyy.YeyyNotificationDownload;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.RemoteViews;public class MyActivity extends Activity implements OnClickListener {// 定义notification的id private int notification_id=1;// 定义主线程的handler private Handler handler=new Handler() ;// 记录进度条进度 private int count=0;// 记录是否进度条取消 private Boolean isclean=false; private Button btnSend; private Button btnClean; private NotificationManager nm; private Notification notification; /** * Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findView(); setClick();// 得到NotificationManager的服务对象 nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);// 初始化notification对象 notification = new Notification(R.drawable.ic_launcher,"开始下载",System.currentTimeMillis());// 得到Notification的视图对象 notification.contentView=new RemoteViews(getPackageName(),R.layout.layout_notification);// 设置视图中的ProgressBar对象 notification.contentView.setProgressBar(R.id.pb,100,0,false);// 定义单击通知事件 Intent notificationIntent=new Intent(this,MyActivity.class); PendingIntent contentIntent= PendingIntent.getActivity(this,0,notificationIntent,0); notification.contentIntent=contentIntent; } private void findView(){ btnSend = (Button) findViewById(R.id.btnSend); btnClean = (Button) findViewById(R.id.btnClean); } private void setClick(){ btnSend.setOnClickListener(this); btnClean.setOnClickListener(this); } @Override public void onClick(View v) {// 自定义按钮单击监听器 switch (v.getId()){ case R.id.btnClean:// 取消notification nm.cancel(notification_id); isclean=true; break; case R.id.btnSend:// 显示notification showNotification(); handler.post(run); break; default: break; } }// 定义Runnable对象进行进度更新 Runnable run=new Runnable() { @Override public void run() {// 判断通知是否被取消 if (!isclean){// 如果没有取消就进行进度的更新 count++; notification.contentView.setProgressBar(R.id.pb,100,count,false);// 200毫秒count加1 if (count<100) handler.postDelayed(run,200); } }};// 显示notification public void showNotification(){ nm.notify(notification_id,notification); }}
源码地址:http://download.csdn.net/detail/csdnyuandaimaxuexi/9214261
0 0
- Notification 模拟收到短信,数据下载的状态栏提示
- 自定义Notification模拟数据下载状态栏的提示
- Notification系统提示,模拟短信消息
- ie8/9收到json数据后提示下载的解决方法
- 状态栏提示Notification NotificationManager
- 状态栏提示:notification
- 昨晚上收到的短信
- 我收到的垃圾短信
- 最近收到的骗子短信
- Android监听收到的短信
- android收到短信,用Toast显示“有短信”提示
- Android 接收短信并屏蔽notification 提示
- Android的状态栏通知(Notification)
- Android的状态栏通知(Notification)
- Android的状态栏通知(Notification)
- Android的状态栏通知(Notification)
- Android的状态栏通知(Notification)
- Android 的状态栏通知(Notification)
- 寻找峰值
- opencv2-第五章-图像处理之阈值化
- android binder 基础实例及解析(二)
- 077.Char Pointer String 用指针表示字符串
- android studio如何使用git
- Notification 模拟收到短信,数据下载的状态栏提示
- android 中EditText光标不显示问题
- Map、Set、List、Collection、Queue、Stack的特点与用法。
- 076.Pointer Two Array 指向多维数组的指针
- 当webView关闭的时候,音乐或者视频还在播放,没有停止
- php图形图像处理
- LeetCodeOJ_208_Course Schedule
- 075.Quick Sort 快速排序
- 性能用例编写方案