2013年9月29日日曜日

androidカスタムダイアログの作り方

androidでカスタムダイアログを作った例です。

○ソースコード




        LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
        final View viewSettingDialog = inflater.inflate(R.layout.setting_dialog, (ViewGroup)findViewById(R.id.layout_root));
        ((CheckBox)viewSettingDialog.findViewById(R.id.checkBoxVibrator)).setChecked(true);
        ((SeekBar)viewSettingDialog.findViewById(R.id.seekBarVolume)).setProgress(50);
        
        //
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(viewSettingDialog);
        builder.setPositiveButton("OK", new OnClickListener() {
            public void onClick(DialogInterface dialog, int which) { // OKボタンクリック処理
                boolean valVib = ((CheckBox)viewSettingDialog.findViewById(R.id.checkBoxVibrator)).isChecked();
                int valVol = ((SeekBar)viewSettingDialog.findViewById(R.id.seekBarVolume)).getProgress();
            }
        });
        builder.setNegativeButton("Cancel", new OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // Cancel ボタンクリック処理
            }
        });
        builder.create().show();



○setting_dialog.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layout_root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="10dp" >
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/option_volume" />
        <SeekBar
            android:id="@+id/seekBarVolume"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="100"
            android:padding="16dp"
            android:progress="50" />
        <CheckBox
            android:id="@+id/checkBoxVibrator"
            android:layout_width="308dp"
            android:layout_height="wrap_content"
            android:text="@string/option_vibrator" />
    </LinearLayout>




画面はeclipseでデザインできるので便利です。
その際に「LinearLayout」にID「layout_root」を付けておき、
その「layout_root」を「R.id.layout_root」と
もう一つ、「R.layout.setting_dialog」でinflateでViewで読み込んで表示します。

eclipseでデザインできるのが便利ですねえ。



2013年9月22日日曜日

java文字列から指定した部分を取得する

java文字列から指定した部分を取得する例です。

String val;
val = "abcde":
val = val.substring(1, 3);

これで、「bc」が取り出せます。


substringの引数
 ゼロ始まりの数字
 1つ目が、開始位置
 2つ目が、終了位置の -1



-1が直感的ではないように思いますが、このような仕様でした。

C#のSubstringやVisual Basic ApplicationのMid$の様なものですね。

2013年9月21日土曜日

Androidで強制終了されにくいサービスを作る

Androidでサービスを作ると、OSによって強制終了されてしまうがなるべく強制終了されにくいサービスを作る方法


○AndroidManifest.xmlへ記述

    <application ~

        <service android:name="jp.blogspot.stella_house_software.sampleservice.MainService" />


○Activityからサービスのインテントを起動する

Intent intent = new Intent();
intent.setClass(getApplicationContext(), MainService.class);
startService(intent);


○サービス本体

package jp.blogspot.stella_house_software.sampleservice;

import android.support.v4.app.NotificationCompat;

public class MainService extends Service  {



@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("tag_debug", "[onStartCommand]");

//Notification notification = new Notification.Builder(this)
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("content title")
.setContentText("content text")
.setSmallIcon(R.drawable.ic_launcher)
.build();

NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, notification);

startForeground(1,notification);

return START_STICKY;
}
}

ポイントは、
・startForeground(1,notification);を入れる。
・サービスの onStartCommand を START_STICKY で returnする。