创建android Notification

说明:
根据项目选择的基础sdk,选择不同的创建Notification的方法
1 在android sdk 3.0 之前直接通过 new Notification的方法直接创建通知对象

2 在android sdk 3.0 是通过Notification.Builer的方法间接配置Notification的属性和创建Notification的功能

3 在android sdk4.0以上 是通过android-support-v4.jar 包中的NotificationCompat.Builder类,进行配置创建Notification对象


下面是一个实例,用NotificationCompat.Builder的方式创建Notification

 

NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("My notification")
                .setContentText("Hello World!");
        
        //Notification.Builder builder=new Notification.Builder(this);
         
        mBuilder.setAutoCancel(true);
        mBuilder.setLights(Color.BLUE, 500, 500);
        long[] pattern = {500,500,500,500,500,500};
        mBuilder.setVibrate(pattern);
        
        Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.messagevoice);
        mBuilder.setSound(sound);
        //mBuilder.setSound(sound);
        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(this, MessageShowActivity.class);

        // The stack builder object will contain an artificial back stack for the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your application to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        // Adds the back stack for the Intent (but not the Intent itself)
        //stackBuilder.addParentStack(MessageShowActivity.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(this, 0, new Intent(this,MessageShowActivity.class), 0);
        
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // mId allows you to update the notification later on.
        mNotificationManager.notify(12, mBuilder.build());
      

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AndroidNotification类中提供了`BigContentView`属性,可以实现通知不折叠。默认情况下,当通知内容过长时,系统会将通知折叠显示,只显示一部分内容。但是使用`BigContentView`属性后,可以实现通知的完全展开,显示所有的内容。 要使用`BigContentView`,首先需要创建一个RemoteViews对象,用于自定义通知的布局。然后,将该布局设置给`NotificationCompat.Builder`对象的`setCustomBigContentView()`方法。这样,系统在展示通知时,就会使用自定义布局,实现通知的不折叠。 以下是一个简单的示例代码: ```java // 创建自定义的BigContentView布局 RemoteViews bigContentView = new RemoteViews(getPackageName(), R.layout.notification_big); // 设置自定义布局到NotificationCompat.Builder对象 NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_notification) .setContentTitle("通知标题") .setContentText("通知内容") .setCustomBigContentView(bigContentView); // 发送通知 NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(notificationId, builder.build()); ``` 在上面的示例中,`notification_big`是一个自定义的布局文件,表示大尺寸通知的内容。你可以在该布局中设计显示所有的通知内容,包括标题、文字、图标等。然后,将该布局设置给`NotificationCompat.Builder`对象,即可实现通知的不折叠显示。 需要注意的是,某些设备可能无法显示大尺寸通知,而是将其折叠显示。这是由于设备厂商对通知的样式和布局进行了定制。因此,在使用`BigContentView`时,需要考虑设备和系统的兼容性,并进行适当的测试。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值