Long Android Apps on Android

The evolution of Android Wear seems to be built on fast tasks that the user will interact with and then close. This works great for most applications, but what about what covers a long-term task and should not automatically close when it is in sleep mode?

My specific case: Swing by Swing Golf GPS . The preferred operation would be to keep the application active and display when the screen wakes up due to user action. And the service life of one use will be from 2 to 4 hours.

What are some methods to keep the app and center on the Android Wear device for periods longer than one use?

+4
source share
3 answers

So here is what I found as a solution:

Create a notification using PendingIntentto open the main Activity. Also give it the intent for the delete action, so we know if the user rejected 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;
        }
    };

}

Use onStop(), not onPause()for notification. Thus, if you have several actions in your application, you can present them (invoking only the onPause()main one 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());
}

Also use the notification inside yours WearableListenerServiceif you are communicating with the application on the PDA. Thus, a notification can be pushed out and easily accessible when you open your application.

@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
    }
}
+2
source

OnPause() , . , .

0

"extends Service" Wear, .

: Wear - . . , GUI Activity.

, , ( ).

0

Source: https://habr.com/ru/post/1548076/


All Articles