简体   繁体   中英

Using onCreate Bundle savedInstanceState with wallpaper service? (Watchface)

Im in need of some help. Secondary Activity package archtectsproductions.scriptpyandroidwearwatchface;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.wearable.view.WatchViewStub;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.PutDataMapRequest;
import com.google.android.gms.wearable.PutDataRequest;
import com.google.android.gms.wearable.Wearable;

import static android.graphics.Color.BLUE;
import static android.graphics.Color.GREEN;
import static android.graphics.Color.RED;


public class WatchfaceConfigActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private GoogleApiClient mGoogleApiClient;
private int mTextColor = 0xffffffff;

private TextView mTextView;

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

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Wearable.API)
            .build();

    Button buttonOK = (Button)findViewById(R.id.buttonOK);
    buttonOK.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            RadioGroup radioTextColor =
                    (RadioGroup)findViewById(R.id.rGroup);
            int selectedId = radioTextColor.getCheckedRadioButtonId();
            switch (selectedId) {
                default:
                case R.id.rDarkpastel:
                    mTextColor = 0xffffffff;
                    break;
                case R.id.Notepad:
                        mTextColor = GREEN;
                    break;
                case R.id.rHarvenjark:
                        mTextColor = BLUE;
                    break;
                case R.id.rVibrant:
                        mTextColor = RED;
                    break;

            }
            sendParamsAndFinish();
        }
    });
}

// sends data through Google API
private void sendParamsAndFinish() {
    PutDataMapRequest putDataMapReq =
            PutDataMapRequest.create("/watch_face_config_cliu");
    putDataMapReq.getDataMap().putInt("text_color", mTextColor);
    PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
    Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq);

    finish();
}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
    super.onStop();
}

@Override
public void onConnected(Bundle bundle) {
}

@Override
public void onConnectionSuspended(int i) {
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    }

}

MAIN public class WatchFaceCLiuService extends CanvasWatchFaceService { private static final long UPDATE_INTERVAL = TimeUnit.SECONDS.toMillis(1);

@Override
public Engine onCreateEngine() {
    return new Engine();
}

private class Engine extends CanvasWatchFaceService.Engine implements
        GoogleApiClient.ConnectionCallbacks,
            GoogleApiClient.OnConnectionFailedListener {
    ...
    private GoogleApiClient mGoogleApiClient;                
    private int mTextColor = 0xffffffff;
    private float offsetx = (float)(-50 + 100 * Math.random());
    private float offsety = (float)(-50 + 100 * Math.random());

    @Override
    public void onCreate(SurfaceHolder holder) {
        super.onCreate(holder);
        ...
        mGoogleApiClient = new GoogleApiClient.Builder(WatchFaceCLiuService.this)
                .addApi(Wearable.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }

    ...

    @Override
    public void onDraw(Canvas canvas, Rect bounds) {            
        ...
        canvas.drawText(ts1, tx1, ty1, mDigitalPaint);
        ...
    }

    private void releaseGoogleApiClient() {
        if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
            Wearable.DataApi.removeListener(mGoogleApiClient,
                onDataChangedListener);
            mGoogleApiClient.disconnect();
        }
    }

    @Override
    public void onConnected(Bundle bundle) {
        Wearable.DataApi.addListener(mGoogleApiClient,
            onDataChangedListener);
        Wearable.DataApi.getDataItems(mGoogleApiClient).
            setResultCallback(onConnectedResultCallback);
    }

    private void updateParamsForDataItem(DataItem item) {
        if ((item.getUri().getPath()).equals("/watch_face_config_cliu")) {
            DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
            if (dataMap.containsKey("text_color")) {
                int tc = dataMap.getInt("text_color");
                mDigitalPaint.setColor(tc);
                invalidate();
            }
        }
    }

    private final DataApi.DataListener onDataChangedListener =
            new DataApi.DataListener() {
        @Override
        public void onDataChanged(DataEventBuffer dataEvents) {
            for (DataEvent event : dataEvents) {
                if (event.getType() == DataEvent.TYPE_CHANGED) {
                    DataItem item = event.getDataItem();
                    updateParamsForDataItem(item);
                }
            }

            dataEvents.release();
            if (isVisible() && !isInAmbientMode()) {
                invalidate();
            }
        }
    };

    private final ResultCallback<DataItemBuffer>
            onConnectedResultCallback =
            new ResultCallback<DataItemBuffer>() {
        @Override
        public void onResult(DataItemBuffer dataItems) {
            for (DataItem item : dataItems) {
                updateParamsForDataItem(item);
            }

            dataItems.release();
            if (isVisible() && !isInAmbientMode()) {
                invalidate();
            }
        }
    };

    @Override
    public void onConnectionSuspended(int i) {
    }

    @Override
    public void onConnectionFailed(ConnectionResult
        connectionResult) {
    }

    @Override
    public void onDestroy() {
        mUpdateTimeHandler.removeMessages(MESSAGE_ID_UPDATE_TIME);
        releaseGoogleApiClient();
        super.onDestroy();
    }
}
}

I really need help, I'm bashing my head against a brick wall. this just doesn't work. it wont send int across at all. Ive followed a guide. Ive done my best it just wont send, does anyone know a better way? should I make a global? would that work better but I'm not sure how id do it as I seem limited on what code I can use in the canvas watchface service.

all I want Is to send an int from one activity to another. Using a watchface service not a companion. I want to learn. so if you do answer don't just paste the code straight out, unless its easier to just paste the code and I can follow it, and decipher it.

thank you for any help .

<-- old While I understand how to make in java a simple app that saves the users selection of a radio button and then puts it over to a separate activity to, say, change the color of text, i have hit a point where im struggling to do so in android wear.

The reason being i cant Implement the following.

@overide
public void onCreate(Bundle savedInstanceState){
     super.onCreate(savedInstanceState);
      Radiogroup = (RadioGroup)findViewById(R.id.myyrad);
      Radiogroup.setOnCheckedChangedListener(new OnCheckedChangedListener()) { ....

the following cant be implemented on the wallpaper service?

is there any alternative? or do i have to go the extremely confusing googleapiclient route? any help would be greatly appreciated thank you.

This was solved. Nothing wrong with the tutorial or my code. Studio was looking for a different android play service. Changed it in the gradel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM