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());

       
    }
}

你可能感兴趣的:(Android代码之路:Notification消息通知)