安卓简单技术-Notificationz自定义通知栏

来源:互联网 发布:nginx内置变量详解 编辑:程序博客网 时间:2024/06/06 07:30

NotificationNotificationManger统一管理,目前包含的能力有:

创建一个状态条图标。

 

在扩展的状态条窗口中显示额外的信息(和启动一个Intent)。

 

闪灯或LED

 

电话震动。

 

发出听得见的警告声(铃声,保存的声音文件)。

自定义Notification效果图:

   

 

自定义的布局文件:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:orientation="vertical"  
  5.   android:layout_width="match_parent"  
  6.   android:layout_height="match_parent">  
  7.  <TextView   
  8.   android:id="@+id/tv_rv"  
  9.   android:layout_width="wrap_content"  
  10.   android:layout_height="wrap_content"  
  11.   android:text="haha"  
  12.  />   
  13. <ProgressBar   
  14.   style="@android:style/Widget.ProgressBar.Horizontal"  
  15.   android:id="@+id/pb_rv"  
  16.   android:layout_width="wrap_content"  
  17.   android:layout_height="wrap_content"  
  18.  />   
  19. </LinearLayout>  


创建Notification

[java] view plain copy
  1. public class CustomNotificationActivity extends Activity {  
  2.     NotificationManager notificationManager;  
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.main);  
  7.         //获取到系统的notificationManager  
  8.         notificationManager =  (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
  9.     }  
  10.       
  11.     public void click(View view ){  
  12.         //实例化一个notification   
  13.          String tickerText = "IP号码 设置完毕";  
  14.          long when = System.currentTimeMillis();  
  15.          Notification notification = new Notification(R.drawable.icon, tickerText, when);  
  16.            
  17.          //不能手动清理  
  18.          //notification.flags= Notification.FLAG_NO_CLEAR;  
  19.          //添加音乐  
  20.          //notification.sound = Uri.parse("/sdcard/haha.mp3");   
  21.            
  22.          //设置用户点击notification的动作   
  23.          // pendingIntent 延期的意图   
  24.          Intent intent = new Intent(this,Bactivity.class);  
  25.          PendingIntent pendingIntent  = PendingIntent.getActivity(this0, intent, 0);  
  26.          notification.contentIntent = pendingIntent;  
  27.           
  28.          //自定义界面   
  29.          RemoteViews rv = new RemoteViews(getPackageName(), R.layout.noti_layout);  
  30.          rv.setTextViewText(R.id.tv_rv, "我是自定义的 notification");  
  31.          rv.setProgressBar(R.id.pb_rv, 8020false);  
  32.          notification.contentView = rv;  
  33.            
  34.          //把定义的notification 传递给 notificationmanager   
  35.          notificationManager.notify(0, notification);  
  36.     }  
  37. }  


0 0
原创粉丝点击