android的NotificationManager的使用,快捷通知栏

这是简单实现运行项目,常驻通知一直显示在通知栏里。下面贴出代码

MainActivity:![这里写图片描述](http://img.blog.csdn.net/20160613155031569)
import com.example.adlayoutcode.R;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.RemoteViews;

public class MainActivity extends Activity{

    private NotificationManager notificationManager;
    private int mNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNumber=(int)android.os.Build.VERSION.SDK_INT;//获取手机版本
        setNotification();
        finish();
    }
    private void setNotification() {

        // 实例化一个notification
        long when = System.currentTimeMillis();//获取当前系统时间
        Notification notification = new Notification(R.drawable.ic_launcher, "",when);
        // 不能用户不能手动清理掉
        notification.flags = Notification.FLAG_NO_CLEAR;

        // 设置用户点击notification的动作
        // pendingIntent 延期的意图
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");    
        Uri url = Uri.parse("https://www.baidu.com/");   
        intent.setData(url); 
        //5.0以下默认使用系统浏览器打开连接
        if(mNumber<20){
            intent.setClassName("com.android.browser","com.android.browser.BrowserActivity");
        }
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,intent, 0);
        notification.contentIntent = pendingIntent;
        // 自定义通知栏显示的界面
        RemoteViews rv = new RemoteViews(getPackageName(), R.layout.auto_test_activity);
        notification.contentView = rv;

        notificationManager.notify(0, notification);
    }

}
`

项目截图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值