简体   繁体   English

如何将通知发送到Android Wear模拟器

[英]How to send notifications to Android wear emulator

am following goolge's guide on how to implement notifications on wearables 正在遵循goolge有关如何在可穿戴设备上实施通知的指南

Am developing on an emulator, but the damned thing won't take notifications from either another emulator or even physical device .. I need some guidance please. 我正在仿真器上进行开发,但是该死的东西不会接受来自其他仿真器甚至物理设备的通知..我需要一些指导。

here's my code 这是我的代码

    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());

Create mobile app whitout wear. 建立行动应用程式的穿着。

Then insert this code. 然后插入此代码。

Then run app on handheld emulator. 然后在手持模拟器上运行应用程序。 And see the notification on wear emulator. 并参阅磨损模拟器上的通知。 (May be needed to slide down to find it) (可能需要向下滑动才能找到它)

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);

}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM