Android 使用通知技巧

直接上代码,注意要是用support-v4库中提供的 NotificationCompat 类以便达兼容

                Intent intent = new Intent(SendNotification.this, MainActivity.class);
                PendingIntent pi = PendingIntent.getActivity(SendNotification.this, 0,
                        intent, 0);
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                Notification builder = new NotificationCompat.Builder(SendNotification.this)
                        .setContentTitle("This is title")                  //标题
                        .setContentText("This is content")                 //内容
                        .setVibrate(new long[]{0,1000,1000,1000})          //振动,振1s停1s振1s
                        .setLights(Color.GREEN,1000,1000)                  //呼吸灯,亮1s,暗1s
                        .setWhen(System.currentTimeMillis())               //通知时间
                        .setSmallIcon(R.mipmap.ic_launcher)                //状态栏图标
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)) //下拉时图标
                        .setContentIntent(pi)
                        .setAutoCancel(true)                               //点击通知自动消失
                        .build();
                manager.notify(1,builder);

你可能感兴趣的:(Android基础)