GDD11JP: Android問題答案

AIDL (Android Interface Description Language) 使ったことなかったので、AIDL使い方のお勉強がメイン。

UI作るのが面倒だったので、結果をログとして出すというテキトー仕様です。パッケージ名は、よく分からなかったのでとりあえずサービス側に合わせました…

AIDL: src/com/google/android/apps/gddquiz/IQuizService.aidl

package com.google.android.apps.gddquiz;

interface IQuizService {
  String getCode();
}

アクティビティ: src/com/google/android/apps/gddquiz/MainActivity.java

package com.google.android.apps.gddquiz;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class MainActivity extends Activity {
    private IQuizService quiz;
	
    private ServiceConnection conn = new ServiceConnection() {
        public void onServiceConnected(ComponentName name, IBinder service) {
            quiz = IQuizService.Stub.asInterface(service);
            try {
	        Log.v("debug", "code = " + quiz.getCode());
            } catch (RemoteException e) {
                Log.v("error", "failed to call remote method");
            }
        }
	public void onServiceDisconnected(ComponentName name) {
            quiz = null;
        }
    };
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent intent = new Intent("com.google.android.apps.gddquiz.IQuizService");
        bindService(intent, conn, Context.BIND_AUTO_CREATE);
    }
}