Android Wear 用のウォッチフェイスを作成しました。コードは次のとおりです。
ComapnionConfigActivity (モバイル モジュール内):
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.Wearable;
public class CompanionConfigActivity extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_companion_config);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_watch_config, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
モバイル モジュールのマニフェスト:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.agile.mywatchface" >
<uses-permission android:name="com.google.android.permission.PROVIDE_BACKGROUND" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="@drawable/square"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".CompanionConfigActivity"
android:label="@string/title_activity_watch_config" >
<intent-filter>
<action android:name="com.example.agile.mywatchface.MAIN" />
<category android:name= "com.google.android.wearable.watchface.category.COMPANION_CONFIGURATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
MyWatchfaceService: (watchface を描画するための wear モジュール内のサービス)
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.wearable.watchface.CanvasWatchFaceService;
import android.view.SurfaceHolder;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
/**
*
* Created by agile on 3/5/2015.
*/
public class MyWatchfaceService extends CanvasWatchFaceService{
Calendar c;
int hrs,min,sec,width,height;
Timer timer;
TimerTask timerTask;
Paint paint;
@Override
public Engine onCreateEngine() {
return new MyEngine();
}
class MyEngine extends CanvasWatchFaceService.Engine {
@Override
public void onCreate(SurfaceHolder holder) {
super.onCreate(holder);
timer = new Timer();
timerTask = new MyTimerTask();
c = Calendar.getInstance();
paint = new Paint();
paint.setColor(Color.BLUE);
paint.setTextSize(18);
}
@Override
public void onPropertiesChanged(Bundle properties) {
super.onPropertiesChanged(properties);
}
@Override
public void onTimeTick() {
super.onTimeTick();
}
@Override
public void onDraw(Canvas canvas, Rect bounds) {
updateTimeOnEachSecond();
width = bounds.width();
height = bounds.height();
canvas.drawText(String.valueOf(hrs)+":"+String.valueOf(min)+":"+String.valueOf(sec),width/2,height/2,paint);
}
@Override
public void onVisibilityChanged(boolean visible) {
super.onVisibilityChanged(visible);
}
}
public void updateTimeOnEachSecond() {
timer.scheduleAtFixedRate(timerTask, 0, 1000);
}
class MyTimerTask extends TimerTask {
public void run(){
hrs = c.get(Calendar.HOUR_OF_DAY);
min = c.get(Calendar.MINUTE);
sec = c.get(Calendar.SECOND);
}
}
}
摩耗モジュールのマニフェスト:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.agile.mywatchface" >
<uses-feature android:name="android.hardware.type.watch" />
<uses-permission android:name="com.google.android.permission.PROVIDE_BACKGROUND" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="@drawable/square"
android:label="@string/app_name"
>
<service
android:name=".MyWatchfaceService"
android:label="@string/app_name"
android:permission="android.permission.BIND_WALLPAPER"
android:allowEmbedded="true"
>
<!-- companion configuration activity -->
<meta-data
android:name="android.service.wallpaper"
android:resource="@xml/watch_face" />
<meta-data
android:name="com.example.agile.mywatchface.preview"
android:resource="@drawable/square" />
<meta-data
android:name="com.example.agile.mywatchface.preview_circular"
android:resource="@drawable/square" />
<meta-data
android:name="com.google.android.wearable.watchface.companionConfigurationAction"
android:value="com.example.agile.mywatchface.MAIN" />
<!-- wearable configuration activity -->
<meta-data
android:name="com.google.android.wearable.watchface.wearableConfigurationAction"
android:value="com.example.agile.mywatchface.MAIN" />
<intent-filter>
<action android:name="android.service.wallpaper.WallpaperService" />
<category android:name="com.example.agile.mywatchface.category.WATCH_FACE" />
</intent-filter>
</service>
<activity
android:name=".WearConfigActivity"
android:label="@string/title_activity_wear_config"
android:allowEmbedded="true">
<intent-filter>
<action android:name="com.example.agile.mywatchface.CONFIG_DIGITAL" />
<category android:name=
"com.google.android.wearable.watchface.category.WEARABLE_CONFIGURATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="com.google.android.wearable.watchface.wearableConfigurationAction"
android:value="com.example.agile.mywatchface.MAIN" />
</activity>
</application>
</manifest>
ウォッチフェイスを作成するための公式ガイドに従いました。しかし、アプリをインストールすると、コンパニオン アプリに表示されません。問題はマニフェストまたはコンパニオン構成アクティビティにあるようです。1日以上試しましたが、特定できませんでした。誰でも私を助けることができますか?