Android代码之路:Notification消息通知

Notification简介

Notification是Android中用于提示用户消息的小工具,它显示在屏幕的顶端。

随着Android API版本的升级,Notification的创建方式也在改变。
Android 3.0以前,一般使用NotificationCompate.Builder构建Notification。
Android 3.0以后,一般推荐使用Notification.Builder来构建Notification。

Notification的几个必须要设置的属性:

-小图标,对应setSamllIcon
-标题,对应setContentTitle
-内容,对应setContentText

Demo1:普通的Notification

public class MainActivity extends AppCompatActivity {
    private static final int REQUEST_CODE_NORMAL = 1;
    private static final int NORMAL_ID = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onClick(View view) {
        
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        //为builder设置相关属性
        builder.setContentTitle("Weather");
        builder.setContentText("it's sunny today");
        builder.setSmallIcon(R.mipmap.weather);
        //设置展示内容的大图片
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.weather);
        builder.setLargeIcon(bitmap);
        
        //设置通知点击后跳转到详情界面
        PendingIntent pendingIntent =  PendingIntent.getActivity(this, REQUEST_CODE_NORMAL,
                        new Intent(this, WeatherActivity.class), PendingIntent.FLAG_ONE_SHOT);
        builder.setContentIntent(pendingIntent);
        builder.setAutoCancel(true);
        
        //通过NotificationManager.notify将通知发送
        NotificationManager manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification=builder.build();
        manager.notify(NORMAL_ID,notification);
        
    }
}

Demo2:带进度条的Notification

public class MainActivity extends AppCompatActivity {
    private static final int REQUEST_CODE_NORMAL = 1;
    private static final int PROGRESS_ID =2;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onClick(View view) {
        
        final NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
        builder.setContentTitle("提示");
        builder.setContentText("正在下载一部小电影");
        builder.setSmallIcon(R.mipmap.download);

        final NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <=100 ; i+=10) {
                    builder.setProgress(100,i,false);
                    manager.notify(PROGRESS_ID,builder.build());
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                builder.setContentText("下载完成");
                manager.notify(PROGRESS_ID,builder.build());
            }
        }).start();

    }
}

Demo3:自定义的Notification

public class MainActivity extends AppCompatActivity {
    private static final int REQUEST_CODE_NORMAL = 1;
    private static final int CUSTOM_ID =2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onClick(View view) {
            
        NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        
        RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.remote_layout);
        /**
         * @param ResId:控件的id
         * @param ResId:图片资源的Id
         */
        remoteViews.setImageViewResource(R.id.img_show,R.mipmap.weather);
        remoteViews.setTextViewText(R.id.txt_show,"it's cloudy today");
        builder.setContent(remoteViews);
        NotificationManager manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(CUSTOM_ID,builder.build());

       
    }
}

推荐阅读更多精彩内容

  • 原文出处: http://www.androidchina.net/6174.html Notification在...
    木木00阅读 10,267评论 3 31
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 164,590评论 24 698
  • 最近时不时地有人问我这样或那样的通知如何实现,所以本文将根据个人经验对Notification做个总结,以供参考!...
    ConnorLin阅读 31,772评论 27 99
  • 版权声明:本文为博主原创文章,未经博主允许不得转载。微博:厉圣杰源码:AndroidDemo/Notificati...
    牙锅子阅读 3,040评论 11 47
  • 他一直是我爱而不得的人。谈不上一场轰轰烈烈的爱情,也没有撕心裂肺的分别。他走,我走,都很自然,如同呼吸一般...
    有月啊阅读 294评论 0 1
  • 有些人特别会讲笑话,可惜我属于不太会讲笑话的那一类,而且好笑的故事被我讲出来貌似都没那么好玩了 笑话有很强大的社交...
    poppy彩阅读 942评论 0 0
  • 喜欢上手作是11年样子,就这样一路断断续续坚持到了现在。 一开始只是做给宝宝戴,后来越做越多,就想着去摆摊卖。 最...
    香草羚羊阅读 460评论 0 1
  • -1- 在老家的小山村,距离我们家不远处,有个傻子。他父母亲给他取名元旦,我想大概是因为他元旦那天出生的吧。 按照...
    文科升阅读 481评论 2 5