Android下使用Notification的例子

本文介绍了一种在安卓应用中实现版本更新通知的方法。通过发送带有更新链接的通知,引导用户下载最新版本的应用。代码示例展示了如何使用Intent、PendingIntent及NotificationManager等组件来创建并显示自定义的通知。
public void showUpdateNotification ( String content ,String url){

        Intent updateIntent = new Intent();        
        updateIntent.setAction("android.intent.action.VIEW");    
        Uri content_url = Uri.parse(url);   
        updateIntent.setData(content_url);  
        PendingIntent pt =PendingIntent.getActivity (this , 0,updateIntent , 0);//点击通知要跳到的activity
        NotificationManager notificationManager  = (NotificationManager )getSystemService (Context .NOTIFICATION_SERVICE );            
        Resources res = getResources();
        String title = "有更新版本啦";

        Notification notification = null;
        int NOTIF_ID = R.string.app_name;
        Builder builder = new Notification.Builder(this);  
        builder.setContentIntent(pt)  
        .setSmallIcon(R.drawable.ic_launcher)  
        .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
        .setWhen(System.currentTimeMillis()).setAutoCancel(false) 
        .setTicker(title)
        .setAutoCancel(false)
        .setContentTitle(title)
        .setContentText(content);  

        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN){

            notification = builder.build();
        }else {

            notification = builder.getNotification();;
        }
        notificationManager.notify (NOTIF_ID , notification );
    }


转载于:https://my.oschina.net/kevinvane/blog/190205

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值