gpt4 book ai didi

wear-os - Android Wear 上长时间运行的应用

转载 作者:行者123 更新时间:2023-12-01 03:48:41 27 4
gpt4 key购买 nike

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

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

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

最佳答案

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

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

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,您可以展示它们(仅导致 onPause() 的主 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 中的通知如果您与掌上电脑上的应用程序通信。因此,当您的应用程序打开时,可以弹出通知并轻松访问。
@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
}
}

关于wear-os - Android Wear 上长时间运行的应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24699845/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com