安裝Sony Add-on SDK後見< Android SDK >\sdk\add-ons\addon-sony_add-on_sdk_2_1-sony-16\samples\SmartExtensions目錄數組
ControlSample是Sony Add-on SDK中的一個Demo,能夠運行在Smart Watch2上安全
運行截圖以下app
咱們的目標機型是Smart Watch 2, 因此重點分析這個類。ide
SampleControlSmartWatch2(final String hostAppPackageName, final Context context, Handler handler) { super(context, hostAppPackageName); if (handler == null) { throw new IllegalArgumentException("handler == null"); } mHandler = handler; setupClickables(context); initializeMenus(); }
??爲何這裏要從外部傳入一個Handler, 雖然是由外部傳入的,但並未用於內外通訊!!!佈局
LayoutInflater inflater = (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.sample_control_2 , null); mLayout = (ControlViewGroup) parseLayout(layout);
注意:動畫
1.sample_control_2.xml中根元素的width爲220px, height爲176px
2.必須使用parseLayout方法把普通的View轉換成ControlViewGroup. (...爲何)
3.ConrtrolViewGroup.findViewById方法返回的是ControlView
4.ConrolView上能夠設置click監聽ui
public void onResume() { Bundle b1 = new Bundle(); b1.putInt(Control.Intents.EXTRA_LAYOUT_REFERENCE, R.id.sample_control_text_1); b1.putString(Control.Intents.EXTRA_TEXT, "1"); Bundle b2 = new Bundle(); b2.putInt(Control.Intents.EXTRA_LAYOUT_REFERENCE, R.id.sample_control_text_2); b2.putString(Control.Intents.EXTRA_TEXT, "2"); Bundle[] data = new Bundle[2]; data[0] = b1; data[1] = b2; showLayout(R.layout.sample_control_2, data); startAnimation(); }
showLayout接收Bundle數組,可用於更新佈局上的文字code
/** * @param resourceId The new resource to show. */ private void updateAnimation(int resourceId) { sendImage(R.id.animatedImage, resourceId); }
sendImage的意思跟setImage相似... (很無語)orm
/** * Start showing animation on control. */ private void startAnimation() { if (!mIsShowingAnimation) { mIsShowingAnimation = true; mAnimation = new Animation(); mAnimation.run(); } } /** * Stop showing animation on control. */ private void stopAnimation() { if (mIsShowingAnimation) { // Stop animation on accessory if (mAnimation != null) { mAnimation.stop(); mHandler.removeCallbacks(mAnimation); mAnimation = null; } mIsShowingAnimation = false; } }
這裏的的Animation是使用Handler來定時更新圖片模擬實現的xml
@Override public void onTouch(final ControlTouchEvent event) { if (event.getAction() == Control.Intents.TOUCH_ACTION_RELEASE) { } } @Override public void onObjectClick(final ControlObjectClickEvent event) { if (event.getLayoutReference() != -1) { mLayout.onClick(event.getLayoutReference()); } } @Override public void onKey(final int action, final int keyCode, final long timeStamp) { if (action == Control.Intents.KEY_ACTION_RELEASE && keyCode == Control.KeyCodes.KEYCODE_OPTIONS) { } else if (action == Control.Intents.KEY_ACTION_RELEASE && keyCode == Control.KeyCodes.KEYCODE_BACK) { } } @Override public void onMenuItemSelected(final int menuItem) { Log.d(SampleExtensionService.LOG_TAG, "onMenuItemSelected() - menu item " + menuItem); if (menuItem == MENU_ITEM_0) { clearDisplay(); } }
注意: onMenuItemSelected中調用clearDisplay(),猜想是清屏以隱藏菜單
private void toggleMenu() { if (mTextMenu) { showMenu(mMenuItemsIcons); } else { showMenu(mMenuItemsText); } mTextMenu = !mTextMenu; }
Extension安裝時必須向主應用註冊。實現如下方法以提供註冊信息
/** * Get the extension registration information. * * @return The registration configuration. */ @Override public ContentValues getExtensionRegistrationConfiguration() { String iconHostapp = ...; String iconExtension = ...; String iconExtension48 = ...; String iconExtensionBw = ...; ContentValues values = new ContentValues(); // 提供Extension的設置界面入口 values.put(Registration.ExtensionColumns.CONFIGURATION_ACTIVITY, SamplePreferenceActivity.class.getName()); values.put(Registration.ExtensionColumns.CONFIGURATION_TEXT, mContext.getString(R.string.configuration_text)); // 提供Extension的名字, 這個名字將顯示在SmartConnect的Extension列表中 values.put(Registration.ExtensionColumns.NAME, mContext.getString(R.string.extension_name)); // 提供Extension的key, 用於安全校驗 values.put(Registration.ExtensionColumns.EXTENSION_KEY, SampleExtensionService.EXTENSION_KEY); // 提供一些相關的圖片資源 values.put(Registration.ExtensionColumns.HOST_APP_ICON_URI, iconHostapp); values.put(Registration.ExtensionColumns.EXTENSION_ICON_URI, iconExtension); values.put(Registration.ExtensionColumns.EXTENSION_48PX_ICON_URI, iconExtension48); values.put(Registration.ExtensionColumns.EXTENSION_ICON_URI_BLACK_WHITE, iconExtensionBw); // 支持的Notification API版本 values.put(Registration.ExtensionColumns.NOTIFICATION_API_VERSION, getRequiredNotificationApiVersion()); // 提供當前Extension的包名, 必須惟一 values.put(Registration.ExtensionColumns.PACKAGE_NAME, mContext.getPackageName()); return values; }
注意,這個例子並不支持Notification(沒有使用Notification API),因此重寫的getRequiredNotificationApiVersion()方法以下:
@Override public int getRequiredNotificationApiVersion() { return 0; }
若是要使用Notification API,這裏就不能簡單地返回0了,而要返回你須要的Notification API版本。可參考父類RegistrationInformation中的getRequiredNotificationApiVersion()方法的註釋
/** * Get the required notifications API version * * @see Registration.ExtensionColumns#NOTIFICATION_API_VERSION * @see #getSourceRegistrationConfigurations * @see ExtensionService#onViewEvent * @see ExtensionService#onRefreshRequest * @return Required notification API version, or 0 if not supporting * notification. */ public abstract int getRequiredNotificationApiVersion();