美文网首页
Notification

Notification

作者: Passon_Fang | 来源:发表于2016-03-22 18:49 被阅读277次

    Notification

    使用步骤

    第一步:获取系统服务

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    

    第二步:创建Notification

    创建方法一:使用远程View

    Notification noti = new Notification();
    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.通知布局文件); 
    noti.contentView = remoteViews;
    

    创建方法二:Notification.Builder

    Notification noti = new NotificationCompat.Builder(this)
        .setContentTitle("标题")
        .setContentText("content")
        .setSubText("subText")
        .setSmallIcon(R.mipmap.图片)
        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.图片))
        .setVibrate(new long[]{300,300,300,300})//震动
        .setAutoCancel(true)
        .setLights(0ffff0000,1000,1000)
        .build();
    

    设置通知不能被取消:

    mNotification.flags |= Notification.FLAG_NO_CLEAR;
    

    方式二使用自定义布局:.setContent(remoteView)

    消息的其他设置

    大视图通知:

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
            mNotification.bigContentView = remoteViews;
    }
    

    flags:

    // 设置flags
    notification.flags |= Notification.FLAG_NO_CLEAR;
    // 取消flags
    notification.flags &= ~Notification.FLAG_NO_CLEAR;
    // 判断是否设置了
    if ((notification.flags & Notification.FLAG_NO_CLEAR) != 0) {
    
    }
    

    设置属性有两种方式:1. 直接用相应的方法。 2. 通过flags(有些属性只能通过flags)

    PendingIntent使用

    views.setOnClickPendingIntent(R.id.play, PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CUTTENT))
    

    第三步:发送消息

    manager.notify(0,noti); //参数1:标识 参数2:消息对象
    

    扩展

    activity的onNewIntent方法

    activity中的protected void onNewIntent(Intent intent)方法是在活动假启动的时候执行的。

    当通知调用已经在任务栈顶的活动的时候,在这个方法内执行对通知的修改

    @Override
    protected void onNewIntent(Intent intent) {
        Log.d("Intent", "onNewIntent--------------");
        super.onNewIntent(intent);
        String text = intent.getStringExtra("text");
        Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
    
        // 修改通知状态
        if (mNotification != null) {
            mNotification.contentView.setImageViewResource(R.id.noti_play, android.R.drawable.ic_media_pause);
            // 重新发送通知,并覆盖原来的通知
            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            mNotification.defaults &= ~Notification.DEFAULT_VIBRATE; // 关闭震动
            manager.notify(0, mNotification);
        }
    }
    

    RemoteView

    提供了一组基础的操作用于跨进程更新它的界面,在android中的使用场景是通知栏桌面小部件

    作用:自定义通知栏和桌面小部件

    构造方法

    RemoteViews(String packageName, int layoutId) // getPackageName()
    
    RemoteViews(RemoteViews landscape, RemoteViews portrait)
    
    RemoteViews(Parcel parcel)
    

    点击事件

    需要使用pendingIntent

    remoteView.setOnClickPendingIntent(int viewId, PendingIntent pendingIntent)
    

    参数1:远程控件布局中的子控件的id

    桌面小部件:AppWidgetProvider

    1定义小部件的界面 在res/layout下创建xml

    2定义小部件的配置信息 在res/xml下创建xml文件 四个主要参数

    initialLayout 桌面小工具使用的初始化布局

    minHeight minWidth 最小宽高

    updatePeriodMillis 小部件自动更新周期 单位毫秒

    3.定义小部件的实现类 extends AppWidgetProvider

    onReceive onUpdate onWidgetUpdate onEnable onDisable onDeleted onReceive;

    4.清单文件中声明小部件

    PendingIntent

    等待意图

    pendingIntent表示在这个意图会在不确定的时候发生其他功能和Intent一样。

    相关文章

      网友评论

          本文标题:Notification

          本文链接:https://www.haomeiwen.com/subject/dhdllttx.html