0

我已经为 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:(磨损模块中用于绘制表盘的服务)

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>

我已按照官方指南创建表盘。但是,当我安装该应用程序时,它没有显示在配套应用程序中。似乎问题出在清单或伴随配置活动中。我尝试了一天多,但无法识别它。谁能帮我吗?

4

1 回答 1

1

在您的可穿戴清单中,您有以下行:

<category android:name="com.example.agile.mywatchface.category.WATCH_FACE" />

这是不正确的,你需要使用

<category android:name="com.google.android.wearable.watchface.category.WATCH_FACE" />

Android Wear 在过滤器中查找具有此类别的服务,如果您使用自定义类别,它将忽略您的服务。

于 2015-03-07T22:00:21.450 回答