1

我正在尝试在 android wear 应用程序中创建一个自定义卡(此卡由 WearableListenerService 创建),我的问题是当卡出现并且我的应用程序关闭时,卡上的按钮不起作用,但是当应用程序运行时按钮效果很好。我最近说要开发到android,但我没有解决问题,有人可以帮助我吗?非常感谢

  1. 服务

 public class ListenerService extends WearableListenerService {
    private static final String _MESSAGE_PATH = "/message_path";

    private static final String _NOTIFICATION_NUMBER = "/notificationNumber";

    private static Context _context;

    public void onMessageReceived(MessageEvent messageEvent) {

        switch(messageEvent.getPath()){
            case _MESSAGE_PATH:
                break;

            case _NOTIFICATION_NUMBER:
                _context = getBaseContext();
                new Thread(tCards).start();
                break;
            default:
                super.onMessageReceived(messageEvent);
                break;
        }
    }

    private Runnable tCards = new Runnable(){
        public void run() {
            Intent _Card = new Intent();
            _Card.setClass(map, CardCall.class);
            _Card.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            _context.startActivity(_Card);
        }
    };
}

  1. 卡类

public class CardCall extends Activity {

    ClientAction client = ClientAction.getInstance();
    BoxInsetLayout _boxBK;
    ImageButton imgCancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cards_main);

        CardScrollView cardScrollView =
                (CardScrollView) findViewById(R.id.card_scroll_view);
        cardScrollView.setCardGravity(Gravity.BOTTOM);

        imgCancel = (ImageButton) findViewById(R.id.btnStopSong);
        _boxBK = (BoxInsetLayout) findViewById(R.id.background);
        _boxBK.setBackgroundColor(Color.BLUE);

        imgCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TaskStop().execute();
            }
        });
    }

    private class TaskStop extends AsyncTask<Void, Void, Void>
    {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected Void doInBackground(Void... params) {
            client.StopSong("");
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

        }
    }
}

  1. 卡片布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.wearable.view.BoxInsetLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/background"
        android:layout_height="match_parent"
        android:layout_width="match_parent">

        <android.support.wearable.view.CardScrollView
            android:id="@+id/card_scroll_view"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            app:layout_box="bottom">

            <android.support.wearable.view.CardFrame
                android:layout_height="wrap_content"
                android:layout_width="fill_parent">

                <LinearLayout
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    android:orientation="vertical"
                    android:paddingLeft="5dp">

                    <TextView
                        android:fontFamily="sans-serif-light"
                        android:layout_height="wrap_content"
                        android:layout_width="100dp"
                        android:text="Text titulo"
                        android:textColor="@color/black"
                        android:textSize="20sp"/>
                    <TextView
                        android:fontFamily="sans-serif-light"
                        android:layout_height="wrap_content"
                        android:text="textDescricao"
                        android:textColor="@color/black"
                        android:layout_width="100dp"
                        android:textSize="14sp"/>


                    <ImageButton
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:id="@+id/btnStopSong"
                        android:src="@drawable/ic_end_white_36dp"
                        android:background="@drawable/button_round"
                        android:layout_gravity="bottom" />

                </LinearLayout>
            </android.support.wearable.view.CardFrame>
        </android.support.wearable.view.CardScrollView>
    </android.support.wearable.view.BoxInsetLayout>

</RelativeLayout>

4

0 回答 0