I am trying to trigger a button click on smartwatch and then change the text in host application in android phone.
I tried to send a broadcast intent to the broadcast receiver and start a service which include a method that change the text in host application. However, the changeText()
method seems not working. I am able to start a service but not able to change the text. Please see what is wrong in my code. It would be nice if you could give a brief example on how to send broadcast intent from the smartwatch to the host application in best practice.
My control extension class
class SampleControlSmartWatch2 extends ControlExtension {
// Other code
public void onObjectClick(ControlObjectClickEvent event) {
Intent intent = new Intent(SampleExtensionService.INTENT_ACTION_BROADCAST);
intent.putExtra("Name", "something");
mContext.sendBroadcast(intent);
}
}
My broadcast receiver
public class ExtensionReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
intent.setClass(context, HostService.class);
context.startService(intent);
}
}
My host application service
public class HostService extends Service {
// Other code
@Override
public void onCreate() {
super.onCreate();
changeText();
}
public void changeText() {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layoutHost = inflater.inflate(R.layout.activity_main, null);
TextView textView = (TextView) layoutHost.findViewById(R.id.textToBeChanged);
Log.i(TAG, textView.getText().toString()); // It shows "Original"
textView.setText("Changed Text");
Log.i(TAG, textView.getText().toString()); // It shows "Changed Text"
}
}
My AndroidManifest.xml
<application
<!-- Other attribute -->
<service android:name="com.sony.samplecontrolnotification.SampleExtensionService" />
<service android:name="com.sony.samplecontrolnotification.HostService" />
<receiver android:name="com.sony.samplecontrolnotification.ExtensionReceiver" >
<intent-filter>
<action android:name="com.sony.samplecontrolnotification.BROADCAST" />
<!-- Other action -->
</intent-filter>
</receiver>
</application>