您的位置:首页 > 移动开发 > Android开发

Android Wear 进阶 - 1 Notification

2016-02-13 00:07 519 查看
开发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);

On a handheld, the action appears as an additional button attached to the notification. On a wearable, the action appears as a large

button when the user swipes the notification to the left. When the user taps the action, the associated intent is invoked on the

handheld.

Tip: If your notifications include a "Reply" action (such as for a messaging app), you can enhance the behavior by enabling voice input

replies directly from the Android wearable. For more information, read Receiving Voice Input from a Notification.

5: 写单独为了可穿戴设备的notification的特性:

Specify Wearable-only Actions

If you want the actions available on the wearable to be different from those on the handheld, then use WearableExtender.addAction().

Once you add an action with this method, the wearable does not display any other actions added with

NotificationCompat.Builder.addAction(). That is, only the actions added with WearableExtender.addAction() appear on the wearable and

they do not appear on the handheld.

//单独的给可穿戴的 设备 发送 一示意图的

Intent intent = new Intent();

intent.setAction("android.intent.action.CALL");

intent.setData(Uri.parse("tel://17761838076"));

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

//单独给可穿戴的设备的 action

NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.mipmap.yoyo,

"Single for wearable",pendingIntent).build();

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

builder.setSmallIcon(R.mipmap.ic_launcher).setContentText("Text test").setContentTitle("Test Title");

builder.extend(new WearableExtender().addAction(action));

Notification n = builder.build();

NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);

managerCompat.notify(15800,n);

6:添加一个大的View。

在可穿戴设备上面就是背景图片了。

Add a Big View

You can insert extended text content to your notification by adding one of the "big view" styles to your notification. On a handheld

device, users can see the big view content by expanding the notification. On a wearable device, the big view content is visible by

default.

To add the extended content to your notification, call setStyle() on the NotificationCompat.Builder object, passing it an instance of

either BigTextStyle or InboxStyle.

For example, the following code adds an instance of NotificationCompat.BigTextStyle to the event notification, in order to include the

complete event description (which includes more text than can fit into the space provided for setContentText()).

//添加大图片Add a Big View ,同时单独的给可穿戴的 设备 发送 一示意图的

//其中的 大图 会成为 在可穿戴上面的 背景,其实没有默认的好看

//BigTextStyle

NotificationCompat.BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();

bigStyle.bigText("Add a Big View");

Intent intent = new Intent();

intent.setAction("android.intent.action.CALL");

intent.setData(Uri.parse("tel://17761838076"));

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

//单独给可穿戴的设备的 action

NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.mipmap.yoyo,

"Single for wearable",pendingIntent).build();

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

builder.setSmallIcon(R.mipmap.ic_launcher).setContentText("Text test").setContentTitle("Test Title");

builder.extend(new WearableExtender().addAction(action));

//添加LargeIcon

builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.qiu_ic_launcher));

//添加 Style

builder.setStyle(bigStyle);

Notification n = builder.build();

NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);

managerCompat.notify(15800,n);

Notice that you can add a large icon image to any notification using the setLargeIcon() method. However, these icons appear as large

background images on a wearable and do not look good as they are scaled up to fit the wearable screen. To add a wearable-specific

background image to a notification, see Add Wearable Features For a Notification. For more information about designing notifications

with large images, see the Design Principles of Android Wear.

7:Add Wearable Features For a Notification

If you ever need to add wearable-specific options to a notification, such as specifying additional pages of content or letting users

dictate a text response with voice input, you can use the NotificationCompat.WearableExtender class to specify the options. To use this

API:

Create an instance of a WearableExtender, setting the wearable-specific options for the notication.

Create an instance of NotificationCompat.Builder, setting the desired properties for your notification as described earlier in this

lesson.

Call extend() on the notification and pass in the WearableExtender. This applies the wearable options to the notification.

Call build() to build the notification.

For example, the following code calls the setHintHideIcon() method to remove the app icon from the notification card.

//添加 可穿戴设备的feature,设置不显示icon,设置背景图片

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

builder.setSmallIcon(R.mipmap.ic_launcher).setContentText("Text test").setContentTitle("Test Title");

//添加可穿戴设备的 feature

NotificationCompat.WearableExtender wearableExtender =

new NotificationCompat.WearableExtender()

.setHintHideIcon(true)

.setBackground(BitmapFactory.decodeResource(getResources(),R.mipmap.yoyo));

builder.extend(wearableExtender);

Notification n = builder.build();

NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);

managerCompat.notify(15800,n);

The setHintHideIcon() and setBackground() methods are just two examples of new notification features available with

NotificationCompat.WearableExtender.

Note: The bitmap that you use with setBackground() should have a resolution of 400x400 for non-scrolling backgrounds and 640x400 for

backgrounds that support parallax scrolling. Place these bitmap images in the res/drawable-nodpi directory. Place other non-bitmap

resources for wearable notifications, such as those used with the setContentIcon() method, in the res/drawable-hdpi directory.

8:

Deliver the Notification

When you want to deliver your notifications, always use the NotificationManagerCompat API instead of NotificationManager:

// Get an instance of the NotificationManager service

NotificationManagerCompat notificationManager =

NotificationManagerCompat.from(mContext);

// Issue the notification with notification manager.

notificationManager.notify(notificationId, notif);

If you use the framework's NotificationManager, some features from NotificationCompat.WearableExtender do not work, so make sure to use

NotificationCompat.

--------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------

注意Layer 是层的意思。Layout 是布局的意思。

所以Data Layer 是数据层的意思。

Data Items

A DataItem provides data storage with automatic syncing between the handheld and wearable.

数据项

一个数据项提供了 数据存储,这些数据会自动的在手持端和可穿戴端同步

Messages 消息

The MessageApi class can send messages and is good for remote procedure calls (RPC), such as controlling a handheld's media player from

the wearable or starting an intent on the wearable from the handheld. Messages are also great for one-way requests or for a

request/response communication model. If the handheld and wearable are connected, the system queues the message for delivery and returns

a successful result code. If the devices are not connected, an error is returned. A successful result code does not indicate that the

message was delivered successfully as the devices may disconnect after receiving the result code.

消息API类可以发送消息,并且对于RPC非常好,例如可以从可穿戴设备控制手持端设备的媒体播放,或者启动一个intent,。

消息同时对于单程(单行)的请求或者是请求回复通信模式也是很好的。

如果手持端和可穿戴连接了。系统将会排列好消息去传送,并且返回一个成功的code

如果手持端和可穿戴没有连接的话,一个error将会返回。

一个成功的结果码并不代表消息已经被传递成功了,因为设备可能在收到结果码后断开连接。

Asset资产

Asset objects are for sending binary blobs of data, such as images. You attach assets to data items and the system automatically takes

care of the transfer for you, conserving Bluetooth bandwidth by caching large assets to avoid re-transmission.

资产对象是为了传送二进制的对象例如images。 你将资产贴到数据项上面,系统会自动的为你进行转换,保持蓝牙的带宽,通过缓存大的资产去避免重复

传送。

WearableListenerService (for services),可穿戴的监听服务。

Extending WearableListenerService lets you listen for important data layer events in a service. The system manages the lifecycle of the

WearableListenerService, binding to the service when it needs to send data items or messages and unbinding the service when no work is

needed.

扩展的可穿戴监听服务,让你可以监听重要的数据结构事件在一个服务里面。

系统管理可穿戴监听服务的生命周期。当需要传送数据或者消息的时候进行绑定到服务,当不需要做任何工作的时候进行解绑的操作

DataListener (for foreground activities)数据监听,为了前台的activities

Implementing DataListener in an activity lets you listen for important data layer events when an activity is in the foreground. Using

this instead of the WearableListenerService lets you listen for changes only when the user is actively using your app.

在一个activity 中实现数据监听会让你监听重要的数据布局事件,当这个activity 是在前台的的时候。

使用这个而不是可穿戴监听服务,会让你监听到变换,这个变化是正当用户使用你的app的时候

Channel 频道(主要为了大数据)

You can use the ChannelApi class to transfer large data items, such as music and movie files, from a handheld to a wearable device. The

Channel API for data transfer has the following benefits:

Transfer large data files between two or more connected devices, without the automatic synchronization provided when using Asset objects

attached to DataItem objects. The Channel API saves disk space unlike the DataApi class, which creates a copy of the assets on the local

device before synchronizing with connected devices.

Reliably send a file that is too large in size to send using the MessageApi class.

Transfer streamed data, such as music pulled from a network server or voice data from the microphone.

Warning: Because these APIs are designed for communication between handhelds and wearables, these are the only APIs you should use to

set up communication between these devices. For instance, don't try to open low-level sockets to create a communication channel.

Android Wear supports multiple wearables connected to a handheld device. For example, when the user saves a note on a handheld, it

automatically appears on both of the user's Wear devices. To synchronize data between devices, Google’s servers host a cloud node in

the network of devices. The system synchronizes data to directly connected devices, the cloud node, and to wearable devices connected to

the cloud node via Wi-Fi.

你可以使用频道API 类去转换大的数据项目,例如音乐或者电影的文件,从一个手持端的设备到可穿戴的设备上面,

频道的API 专门为了数据转换 有一下几个优点:

1:传送大的数据文件,在两个或或者更多连接的设备,不需要当使用Asset 资产对象(这些对象被贴到数据项对象上面)自动同步。

这个频道API 可以节省磁盘空间,不想DataApi类,DataApi 类需要在同步连接设备的时候创建一个assets拷贝文件在本地的设备上面。

2:可以可靠的传送一个文件,这个文件的很大的文件,不可以通过MessageApi类来进行传送

3:转换流动的数据,例如音乐从网络的服务器或者声音数据从麦克风。

警告: 因为这些API 是为了手持端和可穿戴的通讯进行设计,这些是唯一的API 你可以使用来建立这些设备的通讯的。例如不要试着打开底层的sockets去

尽力通讯的信道。Android Wear 支持多个可穿戴设备连接到一个手持端的设备,例如当用户保存了一个记事本到了手持端,这个会自动的出现在用户的两

个可穿戴的设备上面,为了让设备之间进行同步,谷歌服务器 主持了一个云端的node在网络设备上面,系统同步数据到连接的设备上面,云端的node,和

可穿戴设备链接到云端node的(同步哦wifi)

Card ,--> Notification 就是使用的CardFragment。

You can add cards to your app in two ways:

Use or extend the CardFragment class.

Add a card inside a CardScrollView instance in your layout.

访问可穿戴设备的数据层。Accessing the Wearable Data Layer

使用GoogleApiClient 来访问

GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)

.addConnectionCallbacks(new ConnectionCallbacks() {

@Override

public void onConnected(Bundle connectionHint) {

Log.d(TAG, "onConnected: " + connectionHint);

// Now you can use the Data Layer API

}

@Override

public void onConnectionSuspended(int cause) {

Log.d(TAG, "onConnectionSuspended: " + cause);

}

})

.addOnConnectionFailedListener(new OnConnectionFailedListener() {

@Override

public void onConnectionFailed(ConnectionResult result) {

Log.d(TAG, "onConnectionFailed: " + result);

}

})

// Request access only to the Wearable API

.addApi(Wearable.API)

.build();

因为有可能手表并没有连接上,所以使用addApiIfAvailable() 。

同步数据项目:Syncing Data Iterms

数据项目可以用来同步手机端和可穿戴设备之间的通讯。

需要两个东西一个是是Payload 一个是path,

Payload就是一个字节数组,你可以设置任何你想要的数据,允许你去使用自己的序列化和反序列化。这个payload的大小限制在100KB.

Payload的意思就是有效载荷

一般不直接使用DataItem ,而是使用PutDataRequest 对象,使用setData 来设置payload,使用DataApi.putDataItem()来请求系统创建数据项

当请求数据项的时候,系统会将实现了DataItem 接口的对象返回。

然后,对于setData()来说,我们一般使用data map 来替代它,这个data map 将数据项暴露给一个数据项在一个可以轻松使用的Bundle类似的接口里面。

如果使用DataMap 类,这个方法可以使

Android之Sensor 简介

标签: android手机servicefloat编程

2010-11-02 00:23 15463人阅读 评论(2) 收藏 举报

版权声明:本文为博主原创文章,未经博主允许不得转载。

1.Sensor Type

重力感应/加速度传感器 (G-Sensor)

光感应 (Light-Sensor)

温度感应

方向感应

磁场、

临近性

2.如何实现Sensor编程

a.获取系统服务(SENSOR_SERVICE)返回一个SensorManager 对象

sensormanager = (SensorManager)getSystemSeriver(SENSOR_SERVICE);

b.通过SensorManager对象获取相应的Sensor类型的对象

sensorObject = sensormanager.getDefaultSensor(sensor Type);

c.声明一个SensorEventListener 对象用于侦听Sensor 事件,并重载onSensorChanged方法

SensorEventListener sensorListener = new SensorEventListener(){

};

d.注册相应的SensorService

sensormanager.registerListener(sensorListener, sensorObject, Sensor TYPE);

e.销毁相应的SensorService

sensormanager.unregisterListener(sensorListener, sensorObject);

f: SensorListener 接口是传感器应用程序的中心。它包括两个必需方法:

   onSensorChanged(int sensor,float values[]) 方法在传感器值更改时调用。

该方法只对受此应用程序监视的传感器调用(更多内容见下文)。该方法的参数包括:一个整数,指示更改的传感器;一个浮点值数组,表示传感

器数据本身。有些传感器只提供一个数据值,另一些则提供三个浮点值。方向和加速表传感器都提供三个数据值。

   当传感器的准确性更改时,将调用 onAccuracyChanged(int sensor,int accuracy) 方法。参数包括两个整数:一个表示传感器,另一个表示该传

感器新的准确值。

3.关于G-Sensor

Android 加速度传感器的类型是 Sensor.TYPE_ACCELEROMETER

通过 android.hardware.SensorEvent 返回加速度传感器值。

加速度传感器返回值的单位是加速度的单位 m/s^2(米每二次方秒),有三个方向的值分别是

  values[0]: x-axis 方向加速度

  values[1]: y-axis 方向加速度

  values[2]: z-axis 方向加速度

  其中x,y,z方向的定义是以水平放置在的手机的右下脚为参照系坐标原点

  x 方向就是手机的水平方向,右为正

  y 方向就是手机的水平垂直方向,前为正

  y 方向就是手机的空间垂直方向,天空的方向为正,地球的方向为负

需要注意的是,由于地球固有的重力加速度g (值为9.8 m/s^2),

  因此现实中实际加速度值应该是 z方向返回值 - 9.8 m/s^2.

  比如你以 2 m/s^2 的加速度将手机抛起,这时z方向的返回值应该是 11.8 m/s^2.

  反之若以手机以2 m/s^2 的加速度坠落,则z方向的返回值应该是 7.8 m/s^2.

  x,y方向则没有上述限制。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: