2012年1月31日火曜日

RemoteViewsの使い方

別プロセス上で表示可能となる階層的なViewを記述するクラス
Viewは、レイアウトリソースファイルを指定してインフレートする。

// 生成
RemoteViews view = new RemoteViews(パッケージ名, レイアウトID)

// 以下の用に、指定したVIEWに対してリソースをセットする
view.setImageViewResource(VIEW_ID, リソースID);
view.setTextViewText(VIEW_ID, "text data");
view.setProgressBar(VIEW_ID, 100, progress, false);

// またPendingIntentをセットして、ViewからIntent発行をしたり出来るらしい★未検証
.setOnClickPendingIntent(VIEW_ID, pendingIntent);

このRemoteViews を用いて、Notificationクラスの、contentViewにセットすることで
独自のレイアウトをNotificationに表示させることが出来るようです。

AIDLによる Service ⇔ Activity間通信


●AIDLとは?
→android上でプロセス間通信を行うためのもの
 Service側に実装されたコールバック用のインターフェースを
 Activityが受け取ることで、Serviceの処理を呼び出すことが可能
 逆に ServiceからActivityのメソッドも呼び出せるらしい? ★ 要調査
 最初のテンプレートさえ覚えておけば、使い方は特に問題なさそう

1.AIDLファイルの作成
拡張子 .aidl のファイルを作成し、その中に下記のようにインターフェースを書く
javaソースと同じフォルダに置く

 
interface IServiceMethod
{
void CallServiceMethod();
}

2.サービスの実装

class MyService extends Service implements IServiceMethod
{
    @Override
    public IBinder onBind(Intent intent) {
        if(IServiceMethod.class.getName().equals(intent.getAction())){
            // IServiceMethodのインスタンスを返す
            return isvc;
        }
        return null;
    }

private IServiceMethod.Stub isvc = new IServiceMethod.Stub()
{
public void CallServiceMethod() throws RemoteException {
// Service側で実行する処理を実装
}
};
}

ビルドすると gen フォルダに自動生成された IServiceMethod.javaが出来る。
中身を見ると、Stub という名前の内部抽象クラスがあり、.aidl ファイルで宣言したメソッドが定義される。
このインターフェースを実装した Stabクラスのインスタンスを bind時に返せばOK。


3.Activityの実装
 
public class IPCMain extends Activity {

IServiceMethod aidl = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // MyServiceにbindする
        Intent i = new Intent(this, android.study.kai.MyService.class);
        bindService(i, serviceConnection, BIND_AUTO_CREATE);
    }

    @Override
    public void onDestroy()
    {
    super.onDestroy();
    // MyServiceからアンバイド
    unbindService(serviceConnection);
    }

    private ServiceConnection serviceConnection = new ServiceConnection()
    {
    // サービスとの接続時に呼ばれる
        public void onServiceConnected(ComponentName name, IBinder ibinder)
        {
        // Ibinder インターフェースから、AIDLのインターフェースにキャストするメソッドらしい
        aidl = IServiceMethod.Stub.asInterface(ibinder);
        }

    // サービスとの切断時に呼ばれる
        public void onServiceDisconnected(ComponentName name)
        {
        aidl = null;
        }
    };
}

bindServiceと、unbindServiceを呼び出し、サービスと接続/切断する
Serviceのインターフェースは、bind成功時に呼ばれる
インターフェースから取得できる

●bindService
bindService(i, serviceConnection, BIND_AUTO_CREATE);

引数1: インデント。サービスのクラスを指定する?
引数2: ServiceConnectionの実装を指定する
    ServiceConnectionは、サービス接続、切断時に呼ばれる
    コールバック用のインターフェースである。
    接続時に、onBindで返したサービス側のインターフェースを取得できる
引数3: BIND_AUTO_CREATEを指定することでServiceが未起動の時に起動される

●unbindService
unbindService(serviceConnection);
引数1:ServiceConnectionの実装を指定する

2012年1月29日日曜日

Serviceの開始、停止

Activityからのサービスの開始と停止の指示。

 

// サービスを開始する
Intent i = new Intent(this, android2.study.yohei.MyService.class);
startService(i);

// サービスの停止する
Intent i = new Intent(this, android2.study.yohei.MyService.class);
stopService(i);
インテントを生成して、それを引数に起動するだけなので 意外とシンプルです。

Intentでの値の受け渡し方


Intentクラスに、値を受け渡すためのメソッドが用意されているので
これを用います。

★Set
 
i.putExtra("tag1", mConfig.mIpAddress);
i.putExtra("tag2", mConfig.mPortNum);
i.putExtra("tag3", mConfig.10080);

★Get
 
Intent i = this.getIntent();
boolean enable = i.getBooleanExtra("tag1", false);
String ipAddress = i.getStringExtra("tag2");
int portNum = i.getIntExtra("tag3", 10080);
上記の他にもいろいろあります。 ※Serviceの場合は、getIntent()がないのですが、onStartメソッドの引数で取得できます。

Notification

1.NotificationManager を取得する
 
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
2.Notificationオブジェクト生成
 
Notification notification = new Notification(
android.R.drawable.btn_default,
"通知情報が届きました",
System.currentTimeMillis());

・Notificationクラス
→通知情報のコンテキスト
 サウンド、バイブレーション、LEDを用いてアラートする事も
 引数1 icon 一覧に表示するアイコンID
 引数2 text 表示するテキスト
 引数3 表示する時間

3, PendingIntentを用意
 
Intent intent = new Intent(Intent.ACTION_VIEW);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
・PendingIntent
Intentを、タイミングを指定して発行することができるIntent。
時間を指定したり、イベント発生時に発行する。

4.通知情報の設定
 
notification.setLatestEventInfo(
getApplicationContext(),
"アプリ名",
"通知情報の説明文",
contentIntent);

5.一旦、Notificationを削除
 
nm.cancel(id)
6.Manager経由でNotify発行
 
nm.notify(R.string.app_name, notification);

Failed to install ----.apk on device

USBデバッグでデバイスを繋いだ状態で、apkをインストール出来なかった
症状が発生したのでメモ。

ググったところMP3をパッケージに含めたために
発生した転送のタイムアウトエラーでした。

「ウィンドウ」→「設定」→「Android」→「DDMS」→「ADB 接続タイム・アウト」
の時間を変更することで、問題解決。