Android wear watchface settings on the host

I currently have a developed Android wear dial. However, I now want to create a setting part on the host application that allows users to customize the dial. I'm a novice in Android development, so I'm curious about the right way to do this

Is there a way to update sharing preferences on the host and then push or synchronize sharing preferences on worn devices? Or should I see a completely different way?

resolvent:

You can use dataapi or messageapi to synchronize your watchface configuration between phone and watch devices

Please review the document and select a document that better meets your needs: https://developer.android.com/training/wearables/data-layer/index.html https://developer.android.com/training/wearables/data-layer/data-items.html https://developer.android.com/training/wearables/data-layer/messages.html

The following is an example of using dataapi

All content pushed to dataapi is shared between devices and can be used at the same time. You can change this data, and the other party will immediately notify you of such changes (when the devices are connected to each other). You can also read this data at any time (for example, when the user selects your dial on the watch – the configuration data will be waiting for you there)

On the phone:

public class WatchfaceConfigActivity extends Activity {
    private GoogleApiClient mGoogleApiClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(new ConnectionCallbacks() {
                    @Override
                    public void onConnected(Bundle connectionHint) {
                    }
                    @Override
                    public void onConnectionSuspended(int cause) {
                    }
            })
            .addOnConnectionFailedListener(new OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(ConnectionResult result) {
                    }
                })
            .addApi(Wearable.API)
            .build();
        mGoogleApiClient.connect();
    }

Each time you want to synchronize a new configuration with an Android wear device, you must place a datarequest through the wearable dataapi:

    private void syncConfiguration() {
        if(mGoogleApiClient==null)
            return;

        final PutDataMapRequest putRequest = PutDataMapRequest.create("/CONfig");
        final DataMap map = putRequest.getDataMap();
        map.putInt("mode", 1);
        map.putInt("color", Color.RED);
        map.putString("string_example", "MyWatchface");
        Wearable.DataApi.putDataItem(mGoogleApiClient,  putRequest.asPutDataRequest());
    }
}

In terms of watches:

You need to create a class that extends wearablelistenerservice:

public class DataLayerListenerService extends WearableListenerService {

    @Override
    public void onDataChanged(DataEventBuffer dataEvents) {
        super.onDataChanged(dataEvents);

        final List<DataEvent> events = FreezableUtils.freezeIterable(dataEvents);
        for(DataEvent event : events) {
            final Uri uri = event.getDataItem().getUri();
            final String path = uri!=null ? uri.getPath() : null;
            if("/CONfig".equals(path)) {
                final DataMap map = DataMapItem.fromDataItem(event.getDataItem()).getDataMap();
                // read your values from map:
                int mode = map.getInt("mode");
                int color = map.getInt("color");
                String stringExample = map.getString("string_example");
            }
        }
    }
}

And declare it in androidmanifest:

<service android:name=".DataLayerListenerService" >
    <intent-filter>
        <action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
    </intent-filter>
</service>

Please note that this is just a usage example. Maybe (instead of registering an instance of wearablelistener service) you'd better directly create an mgogleapiclient instance in watchface and add a datalistener there:

    Wearable.DataApi.addListener(mGoogleApiClient, new DataListener() {
        @Override
        public void onDataChanged(DataEventBuffer dataEvents) {
            // read config here and update the watchface
        }
    });

Maybe you don't need to share data – then you can use messageapi to communicate and send messages only when you save the new configuration, or then monitor if you want to read the current configuration from the phone

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>