スマートウォッチ開発初心者です。hostapp から smartextension に文字列を送信したいと考えています。アンドロイドが提供するチュートリアルに基づいて、文字列を取得して意図的に別のアクティビティに送信できる簡単なアプリケーションを作成しました。
今、私はそれを拡張して、スマートウォッチと通信できるようにしたいと考えています。アプリケーション インテントHostapp. 私は 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);
}
}
この質問からそれらのコードを取得しました。
この部分では、アプリケーションの Service クラスを作成する必要がありますか?
別のアプリケーションでは、5 つのクラスを作成したクラスがあります。
- HelloWatchExtension : 私の考えは、ここで文字列を作成することです
- HelloWatchExtension レシーバー: ブロードキャスト レシーバーから拡張
- HelloWatchExtension Service: smartextension Broadcastservice 用
SRegistrationInformation: smartextension を smart connect に登録するために (私は思う) インテントを受け取るために 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 から目的のためにレシーバーを配置する方法がわかりませんか?
編集:
私はまだ私の問題を解決できませんでした。マーリンが言ったように、私は 3 つのポイントを決定します: 1. ホスト アプリに拡張機能を登録する Service を作成します。
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);
}