0

我正在对 GoogleTV 和 Android 平板电脑进行一些调查。我已经设法制作了一个 android 应用程序,它可以从主 Activity 向谷歌电视发送控制消息,我想做的是从主 Activity 启动一个新活动,并继续使用 AnymoteClientService 服务和新活动。在我的主要活动中,我得到了一个 anymoteSender 句柄,用于将 KeyEvent 消息发送到谷歌电视,如何将其传输到新活动(SlidepuzzleActivity)?我可以再次实例化它,但这意味着必须再次经历整个配对过程。

从下面的代码中,您将看到我的 SlidepuzzleActivity 类中有一个 anymoteSender,这将引发错误,但说明了我需要在哪里重用该变量。

代码:MainActivity.java:

package uk.co.myapp.gtvremote;
//imports removed for paste

public class MainActivity extends Activity implements ClientListener{

    private AnymoteSender anymoteSender;
    private TextView statusText;
    protected AnymoteClientService mAnymoteClientService;
    private static String statusPrefix = "Status: ";
    private Context mContext;
    private ProgressBar progressBar;

    private Handler handler;
    private TouchHandler touchPadHandler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        progressBar = (ProgressBar) findViewById(R.id.a_progressbar);
        progressBar.setVisibility(View.VISIBLE);

        mContext = this;



        ImageButton upArrowButton = (ImageButton) findViewById(R.id.upArrow);
        ImageButton leftArrowButton = (ImageButton) findViewById(R.id.leftArrow);
        ImageButton centreButton = (ImageButton) findViewById(R.id.centreButton);
        ImageButton rightArrowButton = (ImageButton) findViewById(R.id.rightArrow);
        ImageButton downArrowButton = (ImageButton) findViewById(R.id.downArrow);
        ImageButton backButton = (ImageButton) findViewById(R.id.backButton);
        ImageButton homeButton = (ImageButton) findViewById(R.id.homeButton);
        Button testButton = (Button) findViewById(R.id.testButton);

        upArrowButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyEvent(KeyEvent.KEYCODE_DPAD_UP);
            }
        });

        leftArrowButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyEvent(KeyEvent.KEYCODE_DPAD_LEFT);
            }
        });

        centreButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyEvent(KeyEvent.KEYCODE_DPAD_CENTER);
            }
        });

        rightArrowButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyEvent(KeyEvent.KEYCODE_DPAD_RIGHT);
            }
        });

        downArrowButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyEvent(KeyEvent.KEYCODE_DPAD_DOWN);
            }
        });

        backButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                sendKeyEvent(KeyEvent.KEYCODE_BACK);

            }
        });

        homeButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                sendKeyEvent(KeyEvent.KEYCODE_HOME);

            }
        });

        testButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent myIntent = new Intent(MainActivity.this, SlidepuzzleActivity.class);
                MainActivity.this.startActivity(myIntent);
            }
        });

        handler = new Handler();

        // Bind to the AnymoteClientService
        Intent intent = new Intent(mContext, AnymoteClientService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        statusText = (TextView) findViewById(R.id.statusText);


    }

    /** Defines callbacks for service binding, passed to bindService() */
    private ServiceConnection mConnection = new ServiceConnection() {
        /*
         * ServiceConnection listener methods.
         */
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

            mAnymoteClientService = ((AnymoteClientService.AnymoteClientServiceBinder) service)
                    .getService();
            mAnymoteClientService.attachClientListener(MainActivity.this);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mAnymoteClientService.detachClientListener(MainActivity.this);
            mAnymoteClientService = null;   
        }

    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onConnected(AnymoteSender anymoteSender) {
        if (anymoteSender != null) {
            // Send events to Google TV using anymoteSender.
            // save handle to the anymoteSender instance.
            this.anymoteSender = anymoteSender;
            attachTouchListnertoTouchPad();

        } else {
            statusText.setText(statusPrefix + "Connection attempt failed, cant find send handler");
            //attempt to connect again?
            //attemptToConnect();
        }

        // Hide the progressBar once connection to Google TV is established. Make the text display appropriately
        handler.post(new Runnable() {
            public void run() {
                progressBar.setVisibility(View.INVISIBLE);
                statusText.setText(statusPrefix + "Connected to GoogleTV");

            }
        });
    }

    @Override
    public void onDisconnected() {
        // show message to tell the user about disconnection.
        statusText.setText(statusPrefix + "Disconnected");
        // Try to connect again if needed. This may be need to be done via button
        attemptToConnect();
        this.anymoteSender = null;

    }

    @Override
    public void onConnectionError() {
        // show message to tell the user about disconnection.
        statusText.setText(statusPrefix + "Connection error encountered");
        // Try to connect again if needed.
        attemptToConnect();
        this.anymoteSender = null;

    }

    @Override
    protected void onDestroy() {
        if (mAnymoteClientService != null) {
            mAnymoteClientService.detachClientListener(this);
        }
        unbindService(mConnection);
        super.onDestroy();
    }

    private void attachTouchListnertoTouchPad()
    {
        // Attach touch handler to the touchpad view
        touchPadHandler = new TouchHandler(
                findViewById(R.id.touchPad), Mode.POINTER_MULTITOUCH, anymoteSender);
    }

    public void attemptToConnect()
    {
        //stub to invoke connection attempt
    }

    private void sendKeyEvent(final int keyEvent) {
        // create new Thread to avoid network operations on UI Thread
        if (anymoteSender == null) {
            Toast.makeText(MainActivity.this, "Waiting for connection",
                    Toast.LENGTH_LONG).show();
            return;
        }
        anymoteSender.sendKeyPress(keyEvent);
    }
}

SlidepuzzleActivity.java:

package uk.co.myapp.gtvremote;
//imports removed for paste

public class SlidepuzzleActivity extends Activity implements ClientListener{

    private AnymoteClientService mAnymoteClientService;
    private Context mContext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.slidepuzzle);
        mContext = this;
        ImageButton piece1x1 = (ImageButton) findViewById(R.id.piece1x1);
        ImageButton piece1x2 = (ImageButton) findViewById(R.id.piece1x2);
        ImageButton piece1x3 = (ImageButton) findViewById(R.id.piece1x3);
        ImageButton piece2x1 = (ImageButton) findViewById(R.id.piece2x1);
        ImageButton piece2x2 = (ImageButton) findViewById(R.id.piece2x2);
        ImageButton piece2x3 = (ImageButton) findViewById(R.id.piece2x3);
        ImageButton piece3x1 = (ImageButton) findViewById(R.id.piece3x1);
        ImageButton piece3x2 = (ImageButton) findViewById(R.id.piece3x2);
        ImageButton piece3x3 = (ImageButton) findViewById(R.id.piece3x3);

        Intent intent = new Intent(mContext, AnymoteClientService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

        piece1x1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {


            }
        });
    }

    private ServiceConnection mConnection = new ServiceConnection() {


        /*
         * ServiceConnection listener methods.
         */
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

            mAnymoteClientService = ((AnymoteClientService.AnymoteClientServiceBinder) service)
                    .getService();
            mAnymoteClientService.attachClientListener(SlidepuzzleActivity.this);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mAnymoteClientService.detachClientListener(SlidepuzzleActivity.this);
            mAnymoteClientService = null;   
        }

    };

    private void sendKeyEvent(final int keyEvent) {
        // create new Thread to avoid network operations on UI Thread
        if (anymoteSender == null) {
            Toast.makeText(SlidepuzzleActivity.this, "Waiting for connection",
                    Toast.LENGTH_LONG).show();
            return;
        }
        anymoteSender.sendKeyPress(keyEvent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.slidepuzzle, menu);
        return true;
    }

    @Override
    public void onConnected(AnymoteSender anymoteSender) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onDisconnected() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onConnectionError() {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onDestroy() {
        if (mAnymoteClientService != null) {
            mAnymoteClientService.detachClientListener(this);
        }
        unbindService(mConnection);
        super.onDestroy();
    }


}
4

1 回答 1

2

刚刚为此发布了 AnymoteLibrary 的更新。

在您的 MainActivity 中,为 AnymoteClientService 调用 bindService()(已经)和 startService()。调用 startService() 背后的原因是保留服务及其 anymoteSender 实例,以便同一应用程序中的其他 Activity 可以使用它。

在第二个 Activity 中,实现 ClientListener(如果要获取 onDisconnected() 回调)并绑定到服务和 attachClientListener()。要获取 AnymoteSender 实例,请调用 AnymoteClientService.getAnymoteSender()。请注意,如果与 Google TV 的连接丢失,它可能会返回 null。

当所有 Activity 使用 AnymoteSender 完成后,记得为 AnymoteClientService 调用 stopService()。

于 2012-07-11T22:43:18.300 回答