如何向Android Wear模拟器发送通知

时间:2014-09-15 13:36:00

标签: android wear-os

关注如何实施notifications on wearables

的goolge指南

我是在模拟器上开发的,但该死的东西不会接收来自其他仿真器甚至物理设备的通知..我需要一些指导。

这是我的代码

    Intent viewIntent = new Intent(getApplicationContext(),
                    Not.class);
            // viewIntent.putExtra(EXTRA_EVENT_ID, eventId);
            PendingIntent viewPendingIntent = PendingIntent.getActivity(
                    getApplicationContext(), 0, viewIntent, 0);

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
                    getApplicationContext())
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("Done").setContentText("In my face")
                    .setContentIntent(viewPendingIntent);

            // Get an instance of the NotificationManager service
            NotificationManagerCompat notificationManager = NotificationManagerCompat
                    .from(getApplicationContext());

            // Build the notification and issues it with notification
            // manager.
            notificationManager.notify(1,
                    notificationBuilder.build());

1 个答案:

答案 0 :(得分:1)

创建移动应用程序whitout wear。

然后插入此代码。

然后在手持设备模拟器上运行app。并查看磨损模拟器上的通知。 (可能需要向下滑动才能找到它)

public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);


    Intent intent = new Intent(this, MyActivity2.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(MyActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Action action = new NotificationCompat.Action.Builder(
            R.drawable.ic_launcher,
            getString(R.string.wearTitle),
            pendingIntent).build();

    Notification notification = new NotificationCompat.Builder(MyActivity.this)
            .setContentText("title")
            .setContentText("content")
            .setSmallIcon(R.drawable.ic_launcher)
            .extend(new NotificationCompat.WearableExtender().addAction(action))
            .build();

    NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
    notificationManagerCompat.notify(001, notification);

}
}