我需要一些帮助。 二级Activity 包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 公共类 WatchFaceCLiuService 扩展 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();
}
}
}
我真的需要帮助,我正在用头撞砖墙。这只是行不通。它根本不会发送 int 。我跟着一个指南。我已经尽力了,只是不会发送,有人知道更好的方法吗?我应该做一个全球性的吗?那会更好吗,但我不确定 id 是如何做到的,因为我似乎限制了我可以在画布表盘服务中使用的代码。
我想要的只是将一个 int 从一个活动发送到另一个活动。使用表盘服务而不是伴侣。我想学习。因此,如果您确实回答,请不要直接粘贴代码,除非粘贴代码更容易,我可以按照它并破译它。
感谢您的任何帮助 。
<-- old 虽然我了解如何在 java 中制作一个简单的应用程序,该应用程序可以保存用户对单选按钮的选择,然后将其放到一个单独的活动中,例如更改文本的颜色,但我已经达到了一个点在 android wear 中苦苦挣扎。
原因是我无法实现以下内容。
@overide
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Radiogroup = (RadioGroup)findViewById(R.id.myyrad);
Radiogroup.setOnCheckedChangedListener(new OnCheckedChangedListener()) { ....
以下不能在壁纸服务上实现?
有什么选择吗?还是我必须走极其混乱的 googleapiclient 路线?任何帮助将不胜感激,谢谢。