Android Wear 上长时间运行的应用 [英] Long Running Apps on Android Wear

查看:30
本文介绍了Android Wear 上长时间运行的应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android Wear 生态系统似乎是围绕用户将与之交互然后关闭的快速任务构建的.这对大多数应用程序都非常有效,但是对于涵盖长时间运行的任务并且不应在手表休眠时自动关闭的应用程序呢?

The Android Wear ecosystem seems to be built around quick tasks which a user will interact with, and then close. This works great for most applications, but what about one which covers a long running task, and should not be automatically closed when the watch sleeps?

我的具体案例:Swing by Swing Golf GPS.首选操作是让应用程序保持活动状态,并在屏幕因用户操作而唤醒时显示.并且单次使用的续航时间在2到4小时之间.

My specific case: Swing by Swing Golf GPS. The preferred operation would be to have the application remain active, and shown when the screen wakes due to user action. And the life-time of a single use will be between 2 to 4 hours.

有哪些方法可以使应用程序保持在 Android Wear 设备的前端和中心位置超过一次使用时间?

What are some methods to go about keeping an application front and center on the Android Wear device for periods longer than a single use?

推荐答案

所以,这里是我想出的解决方案:

So, here is what I have come up with as a solution:

使用 PendingIntent 构建通知以打开主 Activity.还要向它传递删除操作的意图,以便我们知道用户是否已关闭它.

Build a notification with a PendingIntent to open the main Activity. Also pass it an intent for the delete action, so we know if the user has dismissed it.

public class SbsNotificationHelper {
    private static final String NOTIFICATION_DELETED_INTENT = "sbs.notificationDeleted";
    private static boolean _isNotificationActive = false;

    /** Public static methods */

    public static NotificationCompat.Builder buildRoundInProgressNotification(Context context) throws Throwable {
        Intent viewIntent = new Intent(context, SbsRoundActivity.class);
        PendingIntent viewPendingIntent = PendingIntent.getActivity(context, 0, viewIntent, 0);

        context.registerReceiver(_broadcastReceiver, new IntentFilter(NOTIFICATION_DELETED_INTENT));
        _isNotificationActive = true;

        Intent deleteIntent = new Intent(NOTIFICATION_DELETED_INTENT);
        PendingIntent deletePendintIntent = PendingIntent.getBroadcast(context, 0, deleteIntent, 0);

        NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.circle_button, "", viewPendingIntent).build();

        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.bottom_bg);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.iphone_57x57)
                .setLargeIcon(bitmap)
                .setContentTitle("Golf GPS")
                .setContentText("Swing by Swing")
                .addAction(action)
                .setDeleteIntent(deletePendintIntent)
                .extend(new NotificationCompat.WearableExtender()
                        .setContentAction(0));

        return notificationBuilder;
    }

    public static boolean isNotificationActive() {
        return _isNotificationActive;
    }

    /** BroadcastReceiver */

    private static final BroadcastReceiver _broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            _isNotificationActive = false;
        }
    };

}

使用 onStop() 而不是 onPause() 来发出通知.这样,如果您的应用中有多个 Activity,您可以展示它们(仅导致主 ActivityonPause()).

Use onStop() as opposed to onPause() to issue the notification. This way, if you have multiple activities in your app, you can present them (only causing onPause() of the main Activity).

@Override
protected void onStop() {
    super.onStop();

    int notificationId = 001;
    NotificationCompat.Builder notificationBuilder = SbsNotificationHelper.buildRoundInProgressNotification(context);

    NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
    notificationManagerCompat.notify(notificationId, notificationBuilder.build());
}

如果您与手持设备上的应用程序进行通信,也可以使用 WearableListenerService 中的通知.因此,当您的应用打开时,可以弹出通知并轻松访问.

Also use the notification inside of your WearableListenerService if you communicate with an app on the handheld. Thus a notification can be popped and easily accessed when your app is opened.

@Override
public void onMessageReceived(MessageEvent messageEvent) {
    super.onMessageReceived(messageEvent);

    try {
        if (SEND_MESSAGE_PATH.equalsIgnoreCase(messageEvent.getPath())) {
            if (!SbsNotificationHelper.isNotificationActive()) {
                int notificationId = 001;
                NotificationCompat.Builder notificationBuilder = SbsNotificationHelper.buildRoundInProgressNotification(sbsApplication);

                NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
                notificationManagerCompat.notify(notificationId, notificationBuilder.build());
            }
        }
    }
    catch (Throwable throwable) {
        //Handle errors
    }
}

这篇关于Android Wear 上长时间运行的应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆