我是智能手表开发的新手。我想尝试将字符串从 hostapp 发送到 smartextension。根据 android 给出的教程,我制作了一个简单的应用程序,它可以获取字符串并有意发送到另一个活动。
现在,我想扩展它以便它可以与智能手表通信。在应用程序 intentHostapp。我有 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);
}
}
我从这个问题中获取了这些代码。
在这部分中,我是否必须为我的应用程序创建一些服务类?
在另一个应用程序中,我创建了 5 个类:
- HelloWatchExtension :我的想法是在这里制作字符串
- HelloWatchExtension 接收器:从广播接收器扩展
- HelloWatchExtension Service:用于 smartextension 广播服务
SRegistrationInformation:为了将它们注册到智能连接的智能扩展(我认为)我已经声明了 HelloWatchExtension 以接收意图
包 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); }
}
这些是我在文档中看到的类,但我不知道将接收器用于 HostApp 的意图?
编辑:
我仍然无法解决我的问题。我不知道自己犯了什么错误,正如 Marlin 所说,我确定了 3 点:1. 制作服务以在主机应用程序中注册您的扩展
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.. 在你的 AndroidManifest.xml 中声明一个 BroadcastReceiver *
...
<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. 在 onReceive() 方法中启动您的服务。
@Override
public void onReceive(Context context, final Intent intent) {
//Log.d(HelloWatchExtensionReceiver.TAG,"onReceive: " + intent.getAction());
intent.setClass(context, HelloWatchExtensionService.class);
context.startService(intent);
}