I'm new with smartwatch development. I want to try to send string from hostapp to smartextension. Based on tutorial given by android, I have made simple application that can take the string and send with intent to another activity.
Right now, I want to extends it So it can communicates with smartwatch. In application intentHostapp. I have mainactivity.java :
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String buzzIntent = "com.example.myfirstapp.buzzintent";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Context mContext;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intentBuzz = new Intent();
EditText editText = (EditText) findViewById(R.id.edit_message);
String buzzIntent = editText.getText().toString();
intentBuzz.setAction(buzzIntent);
startService(intentBuzz);
}
}
I took those codes from this question.
In this part Do I have to make some Service class for my Application?
In another application, I have classes that I have made 5 classes:
- HelloWatchExtension : My idea is to make the String in here
- HelloWatchExtension Receiver: extends from Broadcast Receiver
- HelloWatchExtension Service: for smartextension Broadcastservice
SRegistrationInformation: for registration them smartextension to smart connect(I think) I have declare HelloWatchExtension for received the intent
package com.example.hellowatch;
public class HelloWatchExtension extends ControlExtension{ int width; int height; RelativeLayout layout; Canvas canvas; Bitmap bitmap; TextView textView; public void onCreate() { Intent intent = new Intent("com.example.myfirstapp.buzzintent"); String message = intent.getAction(); textView.setText(message); } public HelloWatchExtension(Context context, String hostAppPackageName) { super(context, hostAppPackageName); width = getSupportedControlWidth(context); height = getSupportedControlHeight(context); layout = new RelativeLayout(context); textView = new TextView(context); textView.setTextSize(9); textView.setGravity(Gravity.CENTER); textView.setTextColor(Color.WHITE); textView.layout(0, 0, width, height); } @Override public void onResume() { updateVisual(); } private void updateVisual() { bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); canvas = new Canvas(bitmap); layout.draw(canvas); showBitmap(bitmap); } public static int getSupportedControlWidth(Context context) { return context.getResources().getDimensionPixelSize( R.dimen.smart_watch_control_width); } public static int getSupportedControlHeight(Context context) { return context.getResources().getDimensionPixelSize( R.dimen.smart_watch_control_height); }
}
Those are classes I saw in the documentation, but I don't know to put receiver for intent from the HostApp?
Edit:
I still couldn't solve my problem. I don't know what mistake that I have made, As Marlin said, I determine 3 points: 1. make a Service to register your extension in the host app
public class HelloWatchRegistrationInformation extends RegistrationInformation {
final Context context;
protected HelloWatchRegistrationInformation(Context context) {
if (context == null) {
throw new IllegalArgumentException("context == null");
}
this.context = context;
}
@Override
public ContentValues getExtensionRegistrationConfiguration() {
String extensionIcon = ExtensionUtils.getUriString(context,
R.drawable.ic_extension);
String iconHostapp = ExtensionUtils.getUriString(context,
R.drawable.ic_launcher);
String configurationText = context.getString(R.string.configuration_text);
String extensionName = context.getString(R.string.extension_name);
ContentValues values = new ContentValues();
values.put(Registration.ExtensionColumns.CONFIGURATION_TEXT, configurationText);
values.put(Registration.ExtensionColumns.EXTENSION_ICON_URI, extensionIcon);
values.put(Registration.ExtensionColumns.EXTENSION_KEY,
HelloWatchExtensionService.EXTENSION_KEY);
values.put(Registration.ExtensionColumns.HOST_APP_ICON_URI, iconHostapp);
values.put(Registration.ExtensionColumns.NAME, extensionName);
values.put(Registration.ExtensionColumns.NOTIFICATION_API_VERSION,
getRequiredNotificationApiVersion());
values.put(Registration.ExtensionColumns.PACKAGE_NAME, context.getPackageName());
return values;
}
@Override
public int getRequiredNotificationApiVersion() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getRequiredWidgetApiVersion() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getRequiredControlApiVersion() {
// TODO Auto-generated method stub
return 1;
}
@Override
public int getRequiredSensorApiVersion() {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean isDisplaySizeSupported(int width, int height) {
return ((width == HelloWatchExtension.getSupportedControlWidth(context) && height == HelloWatchExtension
.getSupportedControlHeight(context)));
}
}
2.. Declare a BroadcastReceiver in your AndroidManifest.xml *
...
<service android:name=".HelloWatchExtensionService" />
<receiver android:name=".HelloWatchExtensionReceiver" >
<intent-filter>
<!-- Generic extension intents. -->
<action android:name="com.sonyericsson.extras.liveware.aef.registration.EXTENSION_REGISTER_REQUEST" />
<action android:name="com.sonyericsson.extras.liveware.aef.registration.ACCESSORY_CONNECTION" />
<action android:name="android.intent.action.LOCALE_CHANGED" />
<!-- Notification intents -->
<action android:name="com.sonyericsson.extras.liveware.aef.notification.VIEW_EVENT_DETAIL" />
<action android:name="com.sonyericsson.extras.liveware.aef.notification.REFRESH_REQUEST" />
<!-- Widget intents -->
<action android:name="com.sonyericsson.extras.aef.widget.START_REFRESH_IMAGE_REQUEST" />
<action android:name="com.sonyericsson.extras.aef.widget.STOP_REFRESH_IMAGE_REQUEST" />
<action android:name="com.sonyericsson.extras.aef.widget.ONTOUCH" />
<action android:name="com.sonyericsson.extras.liveware.extension.util.widget.scheduled.refresh" />
<!-- Control intents -->
<action android:name="com.sonyericsson.extras.aef.control.START" />
<action android:name="com.sonyericsson.extras.aef.control.STOP" />
<action android:name="com.sonyericsson.extras.aef.control.PAUSE" />
<action android:name="com.sonyericsson.extras.aef.control.RESUME" />
<action android:name="com.sonyericsson.extras.aef.control.ERROR" />
<action android:name="com.sonyericsson.extras.aef.control.KEY_EVENT" />
<action android:name="com.sonyericsson.extras.aef.control.TOUCH_EVENT" />
<action android:name="com.sonyericsson.extras.aef.control.SWIPE_EVENT" />
<!-- From SmartPhone -->
<action android:name="com.example.myfirstapp.buzzintent"/>
</intent-filter>
</receiver>
</application>
... 3. in the onReceive() method start your service.
@Override
public void onReceive(Context context, final Intent intent) {
//Log.d(HelloWatchExtensionReceiver.TAG,"onReceive: " + intent.getAction());
intent.setClass(context, HelloWatchExtensionService.class);
context.startService(intent);
}