本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!