mangledBadger
mangledBadger

Reputation: 103

Android Wear passing data from Activity back to watchface service

I have an Android Wear watch face created, an interactive one. On the face I have a grid area where I listen for an onTapCpmmand:

else if (x >= x6 & x <= x9 & y >= (y5 - (gridYUnit / 2)) & y <= (y8 - (gridYUnit / 2))) {
            activityLaunched = new Intent(MyWatchFace.this, ActivityLaunched.class);
            activityLaunched .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(activityLaunched );
        }

The mew activity then takes over. Within this activity, I have three options, basically ImageButtons, with respective onClickHandlers:

optionOne.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            confirmationIntent = new Intent(ActivityLaunched.this, ConfirmationActivity.class);
            confirmationIntent .putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE,
                    ConfirmationActivity.SEARCH_SERVICE);
            confirmationIntent .putExtra(ConfirmationActivity.EXTRA_MESSAGE,
                    getString(R.string.option_one));

            currentOption = "1";
            broadcastIntent(currentOption);
            startActivity(confirmationIntent );
            ActivityLaunched.this.finish();

        }
    });

With broadcastIntent handling the broadcast:

public void broadcastIntent(String currentOption){


    Intent optionUpdated = new Intent();
    optionUpdated .setAction("com.example.packagename....");
    optionUpdated .putExtra("Option", currentOption);
    sendBroadcast(optionUpdated );
}

The users selects an options, the activity closes and the flow of control passes to my broadcastReceiver.

Now, I had set up a broadcastReceiver to make a simple toast when an option has been selected. However, I am unable to do any more with this data, other than show a toast.

Within my broadcastReceiver:

public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Option Updated." + intent.getStringExtra("Option"), Toast.LENGTH_LONG).show();


    currentOption = intent.getStringExtra("Option");
    sendOption .setAction("com.example.packagename....");
    sendOption tion.putExtra("Option", currentOption );
    context.sendBroadcast(sendOption );
    Log.d(TAG, "THIS WORKS : " + currentOption );
}

In my WatchFaceService, I have registered the receiver along with the batteryinforeceivers, and any other system ones, as normal. I am receiving the messages within my broadcastReceiver

Back again to my WatchFaceService, it's where I'm getting issues, I'm not receiving any updates:

optionUpdateReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Bundle results = getResultExtras(true);
                String option = results.getString("Option");
                Log.d(TAG, " NOTHING SHOWING HERE  " + currentOption + option );
            }
        };

I have tried using setters and getters, which required a second launch of the activity to get the real value.

Any ideas as to what I am doing wrong?

I've tried following some other answers and ideas here, as well as other external sources. But as it's Android Wear I'm working with, as volatile as the OS is, anything I tried, that was suggested that worked for Android, appears to be ineffective for Android Wear.

Thanks, Emmett

Upvotes: 1

Views: 744

Answers (2)

mangledBadger
mangledBadger

Reputation: 103

I've actually managed to solve it.

I created a second broadcastreceiver, passed this back to the watchface, and then overrode the register / unregister methods to handle the transmission.

Initially, when I had registered the second receiver, it was spamming the log files and crashing the watch. The reason I had to override was to handle the passing of a filter, which cannot be done from within a watchfacecanvas.engine for some strange reason.

Anyway, it's working fine now, but thanks for help

Upvotes: 0

gruszczy
gruszczy

Reputation: 42188

Your watch face service is not guaranteed to be running when there another activity on top of it. It might be preserved, but equally likely it might be torn down. You could try to register your receiver in WatchFaceService.onCreate() and WatchFaceService.onDestroy(), but that's not a way that is guaranteed to work.

Instead, inside the Activity save the information into SharedPreferences and then read the information within you watch face. When your watch face is created, read the value from the prefs (you can also have a listener for the preferences, to update on their change when they change while the watch face is already launched).

Upvotes: 2

Related Questions