Notification通知

来源:互联网 发布:日本讨厌韩国知乎 编辑:程序博客网 时间:2024/06/12 18:57

首先看看默认的样式效果:



系统默认的通知(通知的样式是默认的)

步骤一:创建通知并指定通知中的一些属性

//1.创建通知        Notification notification=new NotificationCompat                .Builder(this)                .setSmallIcon(R.drawable.icon_notification)                .setTicker("正在播放:海阔天空")                .setContentTitle("海阔天空")                .setContentText("beyound")                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.icon))                .setWhen(System.currentTimeMillis())  //系统时间                .setOngoing(true)  //左右滑动可以取消                .setContentIntent(geContentIntent())  //默认通知                .build();//点击通知后,跳转到某个activity,(还可以发送广播,开启服务)private PendingIntent geContentIntent() {    //非即时意图,未决定的意图不是马上执行,满足某个条件后执行,    //requestCode请求码   /* FLAG_UPDATE_CURRENT表示如果多次调用getActivity方法时,如果请求码一样,返回的还是同一个pendingIntent,不会再次重新创建一个pendingIntent只会刷新意图中的一些数据,    * 简单的来说就是请求码一样,pendingIntent只会刷新意图中的一些数据,而不会重新创建对象    */    Intent intent=new Intent(this,AudioPlayActivity.class);    int requestCode=0;    PendingIntent pendIntent=PendingIntent.getActivity(this,requestCode,intent,PendingIntent.FLAG_UPDATE_CURRENT);    return  pendIntent;}

步骤二:显示通知
 private NotificationManager notificationManager;    private  int notificationId=R.drawable.icon;    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);//2.显示通知notificationManager.notify(notificationId,notification);

步骤三:取消通知
/** 取消通知 */    private void cancelNotification() {        notificationManager.cancel(notificationId);    }

自定义的通知的通知(通知的样式可以是自定义的)
老样子先看看效果图
自定义通知步骤:
步骤一:既然是自定义的通知,样式肯定与系统的有区别喽,所以先准备一个样式也就是一个布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/root"    android:layout_width="match_parent"    android:layout_height="64dp"    android:background="#7b7575"    android:gravity="center_vertical"    android:orientation="horizontal">    <!--注意如果LinearLayout的layout_height是wrap_content,对于某些机型有可能会出现问题-->    <ImageView        android:layout_width="50dp"        android:layout_height="50dp"        android:background="@drawable/icon" />    <LinearLayout        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_marginLeft="5dp"        android:layout_weight="1"        android:orientation="vertical">        <TextView            android:id="@+id/tv_title"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="标题"            android:textSize="16sp" />        <TextView            android:id="@+id/tv_content"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginTop="5dp"            android:text="内容"            android:textSize="14sp" />    </LinearLayout>    <Button        android:id="@+id/btn_prev"        android:layout_width="35dp"        android:layout_height="35dp"        android:background="@drawable/icon_notification_prev" />    <Button        android:id="@+id/btn_next"        android:layout_width="35dp"        android:layout_height="35dp"        android:layout_marginLeft="10dp"        android:layout_marginRight="10dp"        android:background="@drawable/icon_notification_next" /></LinearLayout>

步骤二:创建通知并指定通知中的一些属性
  //1.创建通知        Notification notification=new NotificationCompat                .Builder(this)                .setSmallIcon(R.drawable.icon_notification)                .setTicker("正在播放:海阔天空")                .setOngoing(true)  //左右滑动可以取消                .setContent(getRemoteView())//自定义通知                .build();//通知栏的根布局private  static  final  int NOTIFICATION_ROOT=1;//通知栏的上一首按钮private  static  final  int NOTIFICATION_PREV=2;//通知栏的下一首按钮private  static  final  int NOTIFICATION_NEXT=3;private PendingIntent getPendingIntent(int what) {    int requestCode=what;    Intent intent=new Intent(this,AudioPlayActivity.class);    intent.putExtra("what",what);//仅仅是用来作区分是点击的哪个按钮    PendingIntent pendIntent=PendingIntent.getActivity(this,requestCode,intent,PendingIntent.FLAG_UPDATE_CURRENT);    return  pendIntent;}        /**         * 定义通知的布局         * 自定义通知与默认通知之间的区别就在此处         * @return         */        private RemoteViews getRemoteView() {            RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.activity_custom_notification);            remoteViews.setTextViewText(R.id.tv_title,"海阔天空");//有直接设置属性的方法,不用等findById然后再设置属性            remoteViews.setTextViewText(R.id.tv_content,"beyound");            //设置控件的点击事件            remoteViews.setOnClickPendingIntent(R.id.root,getPendingIntent(NOTIFICATION_ROOT));            remoteViews.setOnClickPendingIntent(R.id.btn_prev,getPendingIntent(NOTIFICATION_PREV));            remoteViews.setOnClickPendingIntent(R.id.btn_next,getPendingIntent(NOTIFICATION_NEXT));            return  remoteViews;        }


步骤二:显示通知
private NotificationManager notificationManager;    private  int notificationId=R.drawable.icon;    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);//2.显示通知notificationManager.notify(notificationId,notification);
步骤三:跳转到另一个页面进行处理操作
public class AudioPlayActivity  extends Activity {    private TextView textView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_audio_play);        textView = (TextView) findViewById(R.id.tv_content);        int what = getIntent().getIntExtra("what", -1);        textView.setText("what : " + what);    }    /**     * 点击返回按钮出现的页面会重复打开     * 解决办法:在清单文件中指定启动模式(process的属性是singleTop)     * 但是有一个缺陷就是点击上一首,下一首按钮时界面不会发生变化     * @param intent     */    /**     * 解决上面描述的问题,重写onNewIntent方法当activity已经存在,不会重新创建,会调用此方法传入Intent     * @param intent     */    @Override    protected void onNewIntent(Intent intent) {        super.onNewIntent(intent);        int what = intent.getIntExtra("what", -1);        textView.setText("what : " + what);    }}


步骤四:取消通知
/** 取消通知 */    private void cancelNotification() {        notificationManager.cancel(notificationId);    }
清单文件:
 <activity            android:launchMode="singleTop"            android:name=".AudioPlayActivity"/>

Demo下载地址:http://download.csdn.net/detail/k2514091675/9819149

0 0