Android Wear 进阶 - 1 Notification

2023-10-12 15:58

本文主要是介绍Android Wear 进阶 - 1 Notification,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

开发android wear可以分为4个部分:
1: 同步通知 
2: 声音控制
3: 创建可穿戴的应用 
4:和手机端之间发送数据


1:同步通知,一般来说手机端的通知是可以同步到可穿戴设备的wear里面的。但是也是可以单独的只给可穿戴设备上面设置action的,所以在开发的设计
的时候要注意考虑这两种设备
2:声音控制,注册自己的应用去处理声音action,例如“你好,安卓,打开一个记事本”,这样就可以不用手去点击应用了
3:创建可穿戴的应用,可以通过google的sdk开发自定义的有activit service sensor 等相关的应用

    adb forward tcp:4444 localabstract:/adb-hub
  adb connect localhost:4444


1: 第一步 导入包
To import the necessary packages, add this line to your build.gradlefile:

compile "com.android.support:support-v4:20.0.+"

我的support-v4的版本是
compile 'com.android.support:support-v4:23.1.0'
所以我就修改为这个


2:在activity 里面导入包:
Now that your project has access to the necessary packages, import the necessary classes from the support library:


import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.NotificationCompat.WearableExtender;


3:创建一个带有挂起的隐式意图的通知 notification,
注意PendingIntent.getActivity 里面的第二个参数是请求码。
然后在builder里面通过setContentIntent()来设置这个pendingIntent
Create Notifications with the Notification Builder
The v4 support library allows you to create notifications using the latest notification features such as action buttons and large icons, 


while remaining compatible with Android 1.6 (API level 4) and higher.


To create a notification with the support library, you create an instance of NotificationCompat.Builder and issue the notification by 


passing it to notify(). For example:




Intent intent =new Intent(this,DetailActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,888,intent,PendingIntent.FLAG_UPDATE_CURRENT);


        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher).setContentText("Text test").setContentTitle("Test Title");
        builder.setContentIntent(pendingIntent);
        Notification n = builder.build();
        NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
        managerCompat.notify(15800,n);
When this notification appears on a handheld device, the user can invoke the PendingIntent specified by the setContentIntent() method by 


touching the notification. When this notification appears on an Android wearable, the user can swipe the notification to the left to 


reveal the Open action, which invokes the intent on the handheld device.


4:还可以在这个pendingIntent 里面添加action,动作,就例如打电话,或者是发短信什么的
Add Action Buttons
In addition to the primary content action defined by setContentIntent(), you can add other actions by passing a PendingIntent to the 


addAction() method.


For example, the following code shows the same type of notification from above, but adds an action to view the event location on a map.


//PendingIntent 里面添加 一个 隐式意图action,可以打电话 ,一定要记得要添加权限啊 。
        Intent intent = new Intent();
        intent.setAction("android.intent.action.CALL");
        intent.setData(Uri.parse("tel://17761838076"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);


        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher).setContentText("Text test").setContentTitle("Test Title");
        builder.setContentIntent(pendingIntent);
        //需要添加addAction,这个
        builder.addAction(R.mipmap.yoyo, "helloAction", pendingIntent);
        Notification n = builder.build();
        NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
        managerCompat.notify(15800,n);

这篇关于Android Wear 进阶 - 1 Notification的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/196902

相关文章

MySQL进阶之路索引失效的11种情况详析

《MySQL进阶之路索引失效的11种情况详析》:本文主要介绍MySQL查询优化中的11种常见情况,包括索引的使用和优化策略,通过这些策略,开发者可以显著提升查询性能,需要的朋友可以参考下... 目录前言图示1. 使用不等式操作符(!=, <, >)2. 使用 OR 连接多个条件3. 对索引字段进行计算操作4

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

Android里面的Service种类以及启动方式

《Android里面的Service种类以及启动方式》Android中的Service分为前台服务和后台服务,前台服务需要亮身份牌并显示通知,后台服务则有启动方式选择,包括startService和b... 目录一句话总结:一、Service 的两种类型:1. 前台服务(必须亮身份牌)2. 后台服务(偷偷干

Android kotlin语言实现删除文件的解决方案

《Androidkotlin语言实现删除文件的解决方案》:本文主要介绍Androidkotlin语言实现删除文件的解决方案,在项目开发过程中,尤其是需要跨平台协作的项目,那么删除用户指定的文件的... 目录一、前言二、适用环境三、模板内容1.权限申请2.Activity中的模板一、前言在项目开发过程中,尤

Python进阶之Excel基本操作介绍

《Python进阶之Excel基本操作介绍》在现实中,很多工作都需要与数据打交道,Excel作为常用的数据处理工具,一直备受人们的青睐,本文主要为大家介绍了一些Python中Excel的基本操作,希望... 目录概述写入使用 xlwt使用 XlsxWriter读取修改概述在现实中,很多工作都需要与数据打交

Android数据库Room的实际使用过程总结

《Android数据库Room的实际使用过程总结》这篇文章主要给大家介绍了关于Android数据库Room的实际使用过程,详细介绍了如何创建实体类、数据访问对象(DAO)和数据库抽象类,需要的朋友可以... 目录前言一、Room的基本使用1.项目配置2.创建实体类(Entity)3.创建数据访问对象(DAO

Android WebView的加载超时处理方案

《AndroidWebView的加载超时处理方案》在Android开发中,WebView是一个常用的组件,用于在应用中嵌入网页,然而,当网络状况不佳或页面加载过慢时,用户可能会遇到加载超时的问题,本... 目录引言一、WebView加载超时的原因二、加载超时处理方案1. 使用Handler和Timer进行超

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin