我有一个可穿戴应用程序。完成后的应用程序具有时间/日期、UUID、地理位置、选择的参数等数据,如数据报告或在彼此下方的多个 TextView 中显示的参数。就像一个列表。我希望将这些数据从我的可穿戴设备传输到我的安卓手机。
现在我不得不问一下,将手机与手表配对的 WearOS 应用程序能实现这样的功能吗?像可以通过它发送数据吗?或者我到底能做什么?我在文档中阅读了有关使用数据层 API 同步数据项的信息,但我不确定提供的代码片段是否有助于实现我想要的。
public class MainActivity extends Activity {
private static final String COUNT_KEY = "com.example.key.count";
private DataClient dataClient;
private int count = 0;
...
// Create a data map and put data in it
private void increaseCounter() {
PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/count");
putDataMapReq.getDataMap().putInt(COUNT_KEY, count++);
PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
Task<DataItem> putDataTask = dataClient.putDataItem(putDataReq);
}
...
}
我在文本视图中显示的数据是通过我调用的方法调用的:getLocation、getUUID、getDateTime、getSelections 等……当我单击一个按钮时,我在 setOnClickListener 中调用它们。我希望将 TextViews 中的这些数据放在文件或类似文件中,并在生成时将它们从手表发送到手机。
private void getDateTime()
{
SimpleDateFormat sdf_date = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat sdf_time = new SimpleDateFormat("HH:mm:ss z");
String currentDate= sdf_date.format(new Date());
String currentTime= sdf_time.format(new Date());
textView_date_time.setText("Date: "+currentDate+"\n"+"Time: "+currentTime);
}
@SuppressLint("SetTextI18n")
private void getUUID()
{
// Retrieving the value using its keys the file name
// must be same in both saving and retrieving the data
@SuppressLint("WrongConstant") SharedPreferences sh = getSharedPreferences("UUID_File", MODE_APPEND);
// The value will be default as empty string because for
// the very first time when the app is opened, there is nothing to show
String theUUID = sh.getString(PREF_UNIQUE_ID, uniqueID);
// We can then use the data
textView_UUID.setText("UUID: "+theUUID);
}
@SuppressLint("SetTextI18n")
private void getSelections()
{
textView_data_selected.setText("Tool No.: "+c.getToolNo()+
"\nTool Size: " +c.getToolSizeStr()+
"\nFrom Mode: " +c.getCurrentModeStr()+
"\nGoto Mode: " +c.getModeStr()+
"\nMethod: " +c.getMethodStr()+
"\nBit Duration: " +c.getBitDuration()+
"\nUpper bound" +c.getUpStageValue()+
"\nLower bound: "+c.getDownStageValue());
}
以上是我用来获取数据的方法的示例。然后我在这里打电话给他们:
gps_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= 26) {
getLocation();
getDateTime();
getUUID();
getSelections();
}
else
{
//ActivityCompat.requestPermissions(get_location.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
Toast.makeText(get_location.this,"Build SDK too low",Toast.LENGTH_SHORT);
}
}
});
现在我如何把这一切从我的设备发送到手机?
注意:我想作为文件发送的数据报告,我希望它像在后台完成的事情一样巧妙地完成。我不知道还能做什么或去哪里看。