Android Wear创建一个通知

创建Android Wear的通知实际上和手机上创建没啥区别,主要是多了几个新类,只要用熟悉了一切都好办了。(如果只是测试通知,则直接运行wear app就能够看到效果)

创建一个简单的wear通知分为3步:

一、创建一个Intent用于设置你要做的动作

二、创建一个PendingIntent把Intent放进去(主要是根据intent传入的内容做跳转动作)

三、创建一个NotificationCompat.Builder用于设置通知内容,例如:将PendingIntent传递进去用于action的点击跳转,设置通知内容、设置通知标题、设置通知图标等

四、创建一个NotificationManagerCompat实例并调用notify()方法将通知发送出去

 

下面是实例代码:

1.创建一个通知

public void showNotification(Context context) {
        int notificationId = 001;
        Intent intent = new Intent();
        intent.setClass(context, ViewNotificationActivity.class);
        //传递数值
        //viewIntent.putExtra(EXTRA_EVENT_ID, eventId);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)//设置notification小图标
                .setContentTitle("这是一个标题")//设置notification标题
                .setContentText("这是内容")//设置notification文本内容
                .setContentIntent(pendingIntent);//设置点击设置点击action时要跳转的页面(wear设备向左滑动第二个按钮的点击后所做的操作)

        //创建一个notifaicationmanamgercompat实例
        NotificationManagerCompat manager = NotificationManagerCompat.from(context);
        //将通知发送出去
        manager.notify(notificationId, builder.build());
    }

2.将上述通知在wear app项目中的主方法中调用运行即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值