我的程序要求我在点击Button
时将数据从手机中的数据库发送到同一程序的android服装。我一直试图通过将数据库转换为JSON
来发送它,然后从JSON
接收这些数据并将它们输入到具有相同名称的数据库中(但是有额外的记录)。将JSON
数据发送到手表的效果非常好,但是当接收手表上的数据时,手表永远不会收到它。我已经尝试了几种方法试试,看看手表是否收到任何东西,但到目前为止没有。我没有像谷歌的一些教程那样实现DataMap
,但我不知道该怎么做。我的代码完全显示了到目前为止我所做的事情:
电话 - MainPhoneApp.java的onCreate方法:
final Button updateWearDatabaseButton = (Button) findViewById(R.id.updateWearDatabaseButton);
updateWearDatabaseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DatabaseToJSON dbJSON = new DatabaseToJSON(MainPhoneApp.this);
try {
JSONArray json = dbJSON.getJSON();
new SendToDataLayerThread(DatabaseHelper.DB_DIRECTORY, json.toString()).start();
Toast.makeText(getApplicationContext(), "Wear Database Updated", Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
电话 - DatabaseToJSON.java:
package com.jawad.wifihotspotfinder;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class DatabaseToJSON {
DatabaseHelper dbhandler;
public DatabaseToJSON(Context context) {
dbhandler = new DatabaseHelper(context);
}
public JSONArray getJSON() throws JSONException {
String myPath = DatabaseHelper.DB_DIRECTORY; // Set path to your database
String myTable = DatabaseHelper.DB_TABLE; // Set name of your table
SQLiteDatabase myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
String searchQuery = "SELECT * FROM " + myTable;
Cursor cursor = myDataBase.rawQuery(searchQuery, null );
JSONArray resultSet = new JSONArray();
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
int totalColumn = cursor.getColumnCount();
JSONObject rowObject = new JSONObject();
for( int i=0 ; i< totalColumn ; i++ ) {
if( cursor.getColumnName(i) != null ) {
try {
if( cursor.getString(i) != null )
{
Log.d("TAG_NAME", cursor.getString(i));
rowObject.put(cursor.getColumnName(i) , cursor.getString(i) );
}
else
{
rowObject.put( cursor.getColumnName(i) , "" );
}
} catch( Exception e ) {
Log.d("TAG_NAME", e.getMessage() );
}
}
}
resultSet.put(rowObject);
cursor.moveToNext();
}
cursor.close();
Log.d("TAG_NAME", resultSet.toString());
return resultSet;
}
}
Wear - DataLayerListenerService.java:
package com.jawad.wifihotspotfinder;
import android.content.ContentValues;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.MessageEvent;
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.Wearable;
import com.google.android.gms.wearable.WearableListenerService;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class DataLayerListenerService extends WearableListenerService {
public final String TAG = "NodeListener";
@Override
public void onPeerConnected(Node peer) {
super.onPeerConnected(peer);
String id = peer.getId();
String name = peer.getDisplayName();
Log.d(TAG, "Connected peer name & ID: " + name + "|" + id);
}
@Override
public void onMessageReceived(MessageEvent messageEvent) {
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this).addApi(Wearable.API).build();
ConnectionResult connectionResult = googleApiClient.blockingConnect(30, TimeUnit.SECONDS);
if(!connectionResult.isSuccess()) {
Log.e("Error", "Failed to connect to Google API Client");
return;
}
if (messageEvent.getPath().equals(DatabaseHelperWear.DB_DIRECTORY)) {
final String message = new String(messageEvent.getData());
Log.v("myTag", "Message path received on watch is: " + messageEvent.getPath());
Log.v("myTag", "Message received on watch is: " + message);
try {
DatabaseHelperWear myDbHelper;
myDbHelper = new DatabaseHelperWear(this);
try {
myDbHelper.createDataBase();
} catch (IOException io) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
} catch (SQLException sqle) {
throw new Error("Unable to open database");
}
SQLiteDatabase db = myDbHelper.getWritableDatabase();
try {
JSONArray jArray = new JSONArray(message);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
ContentValues initialValues = new ContentValues();
initialValues.put("_id", json_data.getString("_id"));
initialValues.put("Type", json_data.getString("Type"));
initialValues.put("Postcode", json_data.getString("Postcode"));
initialValues.put("Latitude", json_data.getString("Latitude"));
initialValues.put("Longitude", json_data.getString("Longitude"));
String where = "_id = ?";
String[] whereArgs = new String[] {String.valueOf(i)};
db.update(DatabaseHelperWear.DB_TABLE, initialValues, where, whereArgs);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
myDbHelper.close();
} catch (Exception ex) {
ex.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Database updated", Toast.LENGTH_SHORT).show();
}
else {
super.onMessageReceived(messageEvent);
}
}
}
更新
我忘了添加AndroidManifest.xml
部分。
<service android:name=".NodeListenerService">
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
答案 0 :(得分:0)
如果您要延长WearableListenerService
,则不需要与GoogleApiClient
建立联系;这些都是在幕后完成的。所以你真正需要的是处理来自消息的数据:
@Override
public void onMessageReceived(MessageEvent messageEvent) {
if (messageEvent.getPath().equals(DatabaseHelperWear.DB_DIRECTORY)) {
// process data
}
}