简体   繁体   English

在墙纸服务中使用onCreate Bundle savedInstanceState吗? (表面)

[英]Using onCreate Bundle savedInstanceState with wallpaper service? (Watchface)

Im in need of some help. 我需要一些帮助。 Secondary Activity package archtectsproductions.scriptpyandroidwearwatchface; 辅助活动包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); MAIN公共类WatchFaceCLiuService扩展了CanvasWatchFaceService {私有静态最终长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. 它根本不会发送int。 Ive followed a guide. Ive遵循了指南。 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. 会更好地工作,但是我不确定id是如何实现的,因为我似乎限制了可以在canvas表面服务中使用的代码。

all I want Is to send an int from one activity to another. 我想要的只是将一个int从一个活动发送到另一个活动。 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. <-老虽然我知道如何在Java中制作一个简单的应用程序,该应用程序可以保存用户对单选按钮的选择,然后将其置于单独的活动中,例如,更改文本的颜色,但我遇到了一个问题努力做到这一点在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? 还是我必须走极为混乱的googleapiclient路线? 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. Studio正在寻找其他Android播放服务。 Changed it in the gradel 在Gradel中进行了更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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