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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值