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

相关文章

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

Android App安装列表获取方法(实践方案)

《AndroidApp安装列表获取方法(实践方案)》文章介绍了Android11及以上版本获取应用列表的方案调整,包括权限配置、白名单配置和action配置三种方式,并提供了相应的Java和Kotl... 目录前言实现方案         方案概述一、 androidManifest 三种配置方式

Java进阶学习之如何开启远程调式

《Java进阶学习之如何开启远程调式》Java开发中的远程调试是一项至关重要的技能,特别是在处理生产环境的问题或者协作开发时,:本文主要介绍Java进阶学习之如何开启远程调式的相关资料,需要的朋友... 目录概述Java远程调试的开启与底层原理开启Java远程调试底层原理JVM参数总结&nbsMbKKXJx

Android WebView无法加载H5页面的常见问题和解决方法

《AndroidWebView无法加载H5页面的常见问题和解决方法》AndroidWebView是一种视图组件,使得Android应用能够显示网页内容,它基于Chromium,具备现代浏览器的许多功... 目录1. WebView 简介2. 常见问题3. 网络权限设置4. 启用 JavaScript5. D

Android如何获取当前CPU频率和占用率

《Android如何获取当前CPU频率和占用率》最近在优化App的性能,需要获取当前CPU视频频率和占用率,所以本文小编就来和大家总结一下如何在Android中获取当前CPU频率和占用率吧... 最近在优化 App 的性能,需要获取当前 CPU视频频率和占用率,通过查询资料,大致思路如下:目前没有标准的

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

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

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

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