2012年5月8日火曜日

AlertDialogの使い方(基本)



■AlertDialog

AlertDialogは、コンストラクタがprivateになっており 直接 new 出来ないので注意!
AlertDialog.Builder経由で、new してやる必要がある

●通常のダイアログ表示
// ダイアログの表示
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setTitle("TEST");
dlg.setMessage("Hello, World!");
dlg.show();

●選択ダイアログの表示
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setIcon(R.drawable.icon);
dlg.setTitle("Select Dialog");
dlg.setMessage("選択してください");
dlg.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        /* YES 選択時の処理 */
    }
});
dlg.setNegativeButton("No", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int whichButton) {
       /* NO 選択時の処理 */
   }
});
dlg.show();

他にも、リストや複数の選択肢、プログレスバーなどを扱うことができる

■参考アドレス
http://www.hakkaku.net/articles/20090924-581
http://wikiwiki.jp/android/?UI%A

2012年5月7日月曜日

android ファイル操作の種類



通常のファイル格納領域
アクセス許可さえあれば、javaのファイルアクセスAPIを用いてアクセス可能
例)Fileクラスのdelete()など

※注 sdcard へのアクセスはマニフェストにパーミッションの追記が必要
(パスは、/sdcard, /mnt/sdcard/など。端末依存?)
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
★sdcardだけでなく、内蔵diskへのアクセスも↑が必要だった★

■アプリケーション配下のファイル格納領域
android側で独自に定義されたAPIを用いる
openFileOutput(ファイル名, モード)/openFileInput(ファイル名)/deleteFile(ファイル名)

・「data」デレクトリ
/data/data/パッケージ名/files
アプリケーション専用のデータ領域
/data/data/パッケージ名/database
データベース
/data/data/パッケージ名/shared_pref
Preferenceを格納

・「assets」デレクトリ
読み込み専用
assetsディレクトリを作成して保管したファイルを読み込む事が可能
ResourcesクラスのopenRawResource(R.raw.ファイル名)や
例)AssetManager.oepn(ファイル名)など

・「raw」デレクトリ
読み込み専用
リソースIDを指定してのファイル読み込みが可能
例)getString(リソースID)や、ResourcesクラスのgetString(リソースID)など

■参考

2012年3月19日月曜日

Notificationのフラグによる動作の設定


Notificationクラスのflagフィールドに代入することで、下記設定が可能
フラグは OR で代入して、複数設定することも出来る。

Notification.FLAG_AUTO_CANCEL
 しばらくすると自動的に通知が消える。ポップアップでお知らせするために使える

Notification.FLAG_INSISTENT
 通知ウィンドウを開くか、キャンセルされるまで、音が鳴り続ける。かなりうざそう

Notification.FLAG_ONGOING_EVENT
 通常の通知ウィンドウの上位のバーの部分に表示する。常に常駐するアプリに使える。

Notification.FLAG_NO_CLEAR
 通知をクリアを押してもクリアできないようにする FLAG_ACTIVITY_NEW_TASK ?

2012年2月20日月曜日

Preferenceの使い方 2

preferences.xmlを使わずに直接書き込みを行う方法

 
     SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
     Editor e = pref.edit();
     e.putString("ip", "保存するテキスト");
     e.putString("path",  "保存するテキスト" );
     e.commit();
こんな感じ。

2012年2月9日木曜日

PendingIntent

RemoteViewやNotificationクラスに用いるPendingIntentというもの
よく理解出来ていないため少し整理してみます。

これはタイミングを指定して発行することができるIntentのようです。
new したIntentを、直ちに発行するのではなく、ボタンを押されたタイミングで発行する等に
利用するようです。
引数でIntentのインスタンスを受けとって包むので、Wrapper的な役割ですかね。

PendingIntentは、以下のstatic メソッドから取得できる
どこに対して発行するかによって、作り方が違う

getActivity(Context, int, Intent, int)
→Activityへ投げる
getBroadcast(Context, int, Intent, int)
→ブロードキャストへ投げる
getService(Context, int, Intent, int);
→Serviceへ投げる

例えば、Notificationに使う場合は、
通知バーをタップしたタイミングでIntentが発行されるという感じのようです。

PendingIntent自身はOSが管理しているものなので
PendingIntentを起動した元のActivityが死んでも、それはOSによって残り続け
通知バーをタップしたタイミングで、Intentが投げられ、
元のAcitivyが起動されるという感じのようです(自信なし)

■参考にさせて頂いたサイト:
http://y-anz-m.blogspot.com/2011/07/androidappwidget-pendingintent-putextra.html

2012年2月6日月曜日

ProgressBarの使い方


プログレスバー(進捗)の表示には、ProgressBarウィジェットを用いる。

スタイルの指定(XMLレイアウト)
android:progressBarStyle        通常の円
android:progressBarStyleHorizontal   水平方向のバー
android:progressBarStyleLarge      大きい円
android:progressBarStyleSmall      小さい円

StyleHorizontal 使用時に限り、進捗状況の表示が可能になる。

メソッド:
setMax() プログレスバーの最大値の指定。上限はint型の範囲。
setProgress() 現在の進捗の値を設定(濃い色で表示される)
setSecondaryProgress() セカンダリの値の指定(薄い色で表示される)

使用例)
ProgressBar progress = (ProgressBar) findViewById(プログレスバーWidgetのID);
progress.setMax(100);
progress.setProgress(20);
progress.setSecondaryProgress(70);

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 接続タイム・アウト」
の時間を変更することで、問題解決。