Android之Notification使用

一种可以显示即时信息的控件,该控件显示在标题栏中,拉开后会看到通知的完整样式


notification有“普通通知”、“大视图通知”、“进度条通知”、“自定义通知”四种样式


首先是第一种普通通知,我在代码里面给大家详细介绍一下吧

public class NotiFiActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_noti_fi);
        initView();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void initView() {

        //得到NotificationCompat.Builder对象
        NotificationCompat.Builder builder=new NotificationCompat.Builder(getApplicationContext());

        builder.setContentTitle("我是标题");  //设置标题

        builder.setSmallIcon(R.drawable.ic_launcher);  //设置图标

        builder.setContentText("我是通知内容");  //设置内容
        //以上3项是必须设置的


        //builder.setSound(Uri sound):设定一个铃声,用于在通知的时候响应。传递一个Uri的参数,格式为“file:///mnt/sdcard/Xxx.mp3”。
        builder.setLights(100, 10, 1);      //设定前置LED灯的闪烁速率,持续毫秒数,停顿毫秒数。
        builder.setVibrate(new long[]{10,20,20});    //设定震动的模式,以一个long数组保存毫秒级间隔的震动。

        /** 大多数时候,我们并不需要设定一个特定的响应效果,只需要遵照用户设备上系统通知的效果即可,
        那么可以使用setDefaults(int)方法设定默认响应参数,在Notification中,对它的参数使用常量定义了,我们只需使用即可:
         1:DEFAULT_ALL:铃声、闪光、震动均系统默认。
         2:DEFAULT_SOUND:系统默认铃声。
         3:DEFAULT_VIBRATE:系统默认震动。
         4:DEFAULT_LIGHTS:系统默认闪光。

         需要访问硬件设备的话,是需要对其进行授权的,所以需要在清单文件AndroidManifest.xml中增加两个授权
         ,分别授予访问振动器与闪光灯的权限:

          闪光灯权限   <uses-permission android:name="android.permission.FLASHLIGHT"/>
          振动器权限    <uses-permission android:name="android.permission.VIBRATE"/>
         */
        builder.setDefaults(Notification.DEFAULT_ALL);


        /**
         * PendingIntent.FLAG_ONE_SHOT:  send()只能被执行一次,即是说,假如该通知点击后不消失,那么再次点击不会发生任何事。
         * PendingIntent.FLAG_UPDATE_CURRENT:  Extra会被更新为最后一个传入的Intent的Extra
         * PendingIntent.FLAG_CANCEL_CURRENT:  这个,会取消之前的intent
         * PendingIntent.FLAG_NO_CREATE: 这个最好不用,不会创建
         */
        Intent intent=new Intent(this, IndexActivity.class);    //这里写好您要跳转的activity

        PendingIntent pendingIntent=PendingIntent.getActivities(this,230,new Intent[]{intent},PendingIntent.FLAG_ONE_SHOT);

        builder.setContentIntent(pendingIntent);



        NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(1,builder.build());   //发送通知

        //manager.cancelAll();清除通知
    }
}



第二种样式==》大视图

  NotificationCompat.Builder builder = new NotificationCompat.Builder(
                this);
        builder.setContentTitle("大视图来了");
        builder.setSmallIcon(R.drawable.ic_launcher);
//		builder.setContentText(">>>>");
        NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
        for (int i = 0; i < 10; i++) {
            style.addLine(">>>" + i);
        }
        builder.setStyle(style);
        NotificationManager manager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(0, builder.build());


第三章样式  ==》进度条通知  


   NotificationCompat.Builder builder=new NotificationCompat.Builder(getApplicationContext());
        builder.setContentTitle("我是标题");  //设置标题

        builder.setSmallIcon(R.drawable.ic_launcher);  //设置图标

        builder.setContentText("已下载50%");  //设置内容
        builder.setProgress(100,50,true);    //可以开一个线程,在该线程中使用setProgress(进度值的最大值,当前进度值,false(是否流动)),在notify方法即时通知进度条

        NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(1,builder.build());


接下来就是第四种样式==》自定义通知


首先创建一个layout布局noti_layout.xml

<?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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="sdbhfgshdfhsdgvf"/>

</LinearLayout>

注意:应该是你的R.layout.my_status_service 这个layout中使用了RemoteViews不支持的组件
在RemoteViews这种调用方式中,你只能使用以下几种界面组件:
Layout:
    FrameLayout, LinearLayout, RelativeLayout
Component:
  AnalogClock, Button, Chronometer, ImageButton, ImageView, ProgressBar, TextView, ViewFlipper, ListView, GridView, StackView, AdapterViewFlipper


使用示例代码:



public class NotiFiActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_noti_fi);
        initView4();
    }


    private void initView4(){


        Notification  notification=new Notification();


        notification.icon=R.drawable.ic_launcher;
        RemoteViews views=new RemoteViews(getPackageName(),R.layout.noti_layout);   //创建自定义的view


        notification.contentView=views;   //使用setContent(content)方法,将RemoteViews对象加载到普通通知对象中
        NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(1,notification);


    }
}

以上就是四种样式的使用案例,新手有空多敲两下就可以了



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值