2013年4月27日土曜日

実験:configuration changeによるActivityの再起動を、コードで実現できるのか

実験:configuration changeによるActivityの再起動過程を、コードで再現できるのか

結論:私がやった限りでは、できなさそうである。

configuration changeが発生すると、Activityが再起動される途中で、onSaveInstanceStateメソッドが呼ばれる。
しかしながら、このメソッドはOSが呼び出すのであって、プログラマがコードで呼び出すことはできない。

onConfigurationChangedメソッド内で、Intentに与えるフラグを操作して、新たにActivityを起動してやれば、onSaveInstanceStateメソッドが呼ばれる。
しかし、この場合、旧Activityのdestroyが、新ActivityのonCreateの後に発生する。この挙動は、私の希望ではない。
finishメソッドを使うと、onSaveInstanceStateメソッドは呼ばれない。

onSaveInstanceStateメソッドは、次のとおり書かれてある。
This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state.
しかしながら、実際には、killされない時にも呼ばれる場合がある。
onSaveInstanceStateメソッド内では、killされることを前提にコードを書いてはいけない。データを保存することに徹した方が良い。

端末がスリープ状態にある時に、Eclipseからデバッグ目的でアプリを起動すると、onSaveInstanceStateメソッド&onDestroy()が繰り返し呼ばれる。この問題に対して、AndroidManifest.xmlにおいてandroid:launchMode="singleTop"を施してみたが、効果は無かった。

Android Configuration Changeに書かれてあるように、onDestroy()の中でgetChangingConfigurations()を使うことも役に立ちそうである。

    @Override
    protected void onDestroy(){
        int iConfig = getChangingConfigurations();
        iConfig &= ActivityInfo.CONFIG_ORIENTATION;
        if(iConfig>0){
            Log.d(TAG, "onDestroy() : ConfigurationChange");
        }

この話題はこれ以上つっこむことなく、ここらで引き揚げることにする。

参考
Android: onSaveInstanceState not being called from activity

2013年4月23日火曜日

RadioButtonの背景色を変更する方法

RadioButtonの背景色を変更する方法

RadioButtonの背景色を変更すると、RadioButtonの形状が崩れることが指摘されています。
Android RadioButton の画像とテキストの間隔を広げる
私は、形状崩れをなんとかしたいと思います。
なお、この現象は少なくともAndroid 4.2.2では発生しません。2.2及び2.3では発生します。

解決方法 その1
xmlにおいて背景色を設定した場合に、形状崩れが発生するのです。実行時に背景色を設定すれば問題は発生しません。

解決方法 その2
RadioGroupを使用せず、javaのコードにおいて、RadioButtonに対する押し下げをonClickメソッドで補足し、他のRadioButtonをsetChecked(false)してやるのです。なお、個々のRadioButtonの上位に個別にLinearLayout等を配置し、このLinearLayoutに対して背景色を設定します。

2013年4月16日火曜日

自分のアプリをZ順の最上位にする

自分のアプリをZ順の最上位にする

自分のアプリのActivityが、画面の前面に無い場合、つまりZ順における最上位では無い場合、詳しく言えば利用者の視点で端末の画面を見た時に他のアプリが表示されていて自分のアプリが見えない場合、自分のアプリのActivityを前面に出してやるにはどうするのか。

自分のアプリのActivityが稼働していない状態であれば、Intentを使えば良いことはわかります。次のとおりです。

Intent it;
it = new Intent(this, MainActivity.class);
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(it);//Activityを起動し,上にしてやる

しかしながら、他のアプリが前面に出ていて、自分のアプリのActivityがバックグラウンドにある場合、つまり自分のActivityが生きておりかつ、後ろに隠れている場合、自分のアプリのActivityを前面に出す方法はどうすれば良いでしょうか。
多くのサイトではIntentを使うように案内されています。
確かに、Intentすれば、前面には出ますが、IntentされたActivityは新規に作成されてしまうのです。onCreateメソッドが実行されてしまい、Activityが複数存在することになってしまいます。

このため、何か違うような気がしたのでrequestFocus()やbringToFront()を試しましたがうまくいきませんでした。次のサイトで議論があるように、getWindow().setActive()でもダメなようです。
ブロードキャストの受信で、自分自身を前面に表示させる方法について

結論は、やっぱり、Intentを使うのです。コードは次のとおりです。Serviceにおいて実行させます。

Intent it;
it = new Intent(this, MainActivity.class);//ここでのthisはServiceです。
it.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(it);//Activityを上にしてやる

これを使えば、ActivityのonCreateメソッドは実行されず、複数のActivityが生成されることなく、自分のアプリをZ順の最上位にすることができます。Intent.FLAG_ACTIVITY_NEW_TASKがあるため、新規に作成されてしまうように思えますが、新規には作成されませんし、これが無いと実行時にエラーになります。
最近の開発環境は良くなっており、エラーになった場合、このIntent.FLAG_ACTIVITY_NEW_TASKを設定しなさいという趣旨のメッセージがログに表示されます。

過去の私の記事において、Intent.FLAG_ACTIVITY_SINGLE_TOPは使う機会が無いという趣旨の記述をしてしまいました。むむむ! 起動モード
そうでもないということですね。

2013年4月15日月曜日

SimpleAdapter : レコード毎に色を変えて表示する

SimpleAdapter : レコード毎に色を変えて表示する

文字列だけで構成されるレコードを一覧にして表示させた場合、レコード間の区切りが見分けにくくなります。
その対処方法として、 レコード毎に色を変えて表示するとか、レコード間の幅を大きくするという方法があります。
javaのコードの概要は下記のとおりです。概要を示したものであるため、このママでは動作しません(見れば分かる)
色分けのフラグをレコード毎に設定し、そのフラグに基づいて背景色を変更すること、そして、背景色の設定にはアルファ値を適当な値で設定することがポイントです。アルファ値を0xffに設定した場合、利用者がそのレコードを選択しても「選択したことを示す背景色」が表示されません。

    ArrayList<Map<String, Object>> al;//List表示用データ
        al = new ArrayList<Map<String, Object>>();
        
            b = cs.moveToLast();
            int i = 0;
            while(b==true){
                String s;
                Map<String, Object> m;
                m = new HashMap<String, Object>();
                s = cs.getString(cs.getColumnIndex("Keyword"));
                m.put("Keyword", s);
                m.put("Background", i%2);//ゼロか1を代入する
                al.add(m);
                i++;
                b = cs.moveToPrevious();
            }
        }
        cs.close();
        
        String sFrom[] = {"Keyword", "Background"};
        int iTo[] = new int[]{ R.id.Keyword_Text, R.id.Layout_Keyword_Background };
        
        SimpleAdapter sa;
        SimpleAdapter.ViewBinder savb;
        savb = new SimpleAdapter.ViewBinder(){
            @Override
            public boolean setViewValue(View v, Object data, String s){
                if(v.getId()==R.id.Layout_Keyword_Background){
                    int i = (Integer)data;
                    if(i==1) v.setBackgroundColor(0x60ffdddd);
                    else v.setBackgroundColor(0x00ffffff);
                    return true;
                }
                return false;
            }
        };
        
        sa = new SimpleAdapter(mainActivity, al, R.layout.list_keyword, sFrom, iTo);
        sa.setViewBinder(savb);
        ListView lv;
        lv = (ListView)mainActivity.findViewById(R.id.ListKeyword);
        lv.setOnItemClickListener(this);
        lv.setAdapter(sa);
        lv.setDividerHeight(10);

list_keyword.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Layout_Keyword_Background"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    >
    
    <TextView
        android:id="@+id/Keyword_Text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textIsSelectable="false"
        >
    </TextView>
</LinearLayout>

面白い動画を見つけました
メ~テレ ぬいぐるみのラパン

2013年3月29日金曜日

YouTubeInitializationResult

YouTubeInitializationResult

YouTubeInitializationResultは、onInitializationFailureメソッドの第二引数として添付されてくる。
インストールされているYouTubeアプリのバージョンが古い場合に有益な振る舞いをしてくれる。
getErrorDialogメソッドには、引数にDialogInterface.OnCancelListenerが存在しない版もある。この版の場合、setOnCancelListenerメソッドでリスナを追加するのであろうか。
(めったにいないであろう、私のような)利用者が「戻る」キーを押し下げることも有りうるのであるから、OnCancelListenerの実装は必須では無いだろうか。

YouTubeアプリのバージョンが古い場合、「YouTubeアプリを更新」画面が表示される。そして「YouTubeアプリを更新」ボタンを押し下げると、onSaveInstanceStateメソッドが呼び出される。このタイミングで呼び出されるのであるから、onSaveInstanceStateメソッド内に記載する各種オブジェクトの初期化状況について注意を払い、NullPointerExceptionが発生しないようにする必要がある。

「YouTubeアプリを更新」ボタンの押し下げ後、画面はGoogle PlayのYouTubeアプリ更新画面になる。この画面で「戻る」キーを押し下げると、onActivityResultメソッドが呼び出される。この場合、このメソッドの第2引数はRESULT_OKではない。

Google PlayのYouTubeアプリ更新画面で、適切に更新を実行した後、onActivityResultメソッド内で、YouTubePlayerView#initializeメソッドを実行するように仕込んでいたが、原因不明のエラーが発生してしまった。YouTubePlayerView#initializeメソッドとは別の要因のエラーかもしれない。もはや当方ではYouTubeアプリの更新実験はできない。

static final int ID_Error_requestCode_YouTubeAppDialog = 24;
private YouTubePlayerView ytView;
Dialog DlgYouTubeAPI;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ytView = (YouTubePlayerView)findViewById(R.id.youtube_view);
    ytView.initialize(DEVELOPER_KEY, this);
    (以下省略)

@Override
public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1){
    if(arg1.isUserRecoverableError()==true){
        //インストールされているYouTubeアプリのバージョンが古い場合
        DlgYouTubeAPI = arg1.getErrorDialog(this, ID_Error_requestCode_YouTubeAppDialog, this);
        DlgYouTubeAPI.show();//新版のインストールを促すダイアログが表示される。
    }
    else{//Recoverableでは無いエラーも存在しえる。
    (以下省略)

@Override
public void onCancel(DialogInterface arg0){
    if(arg0==DlgYouTubeAPI){//新版のインストールを促すダイアログ表示時に、「戻る」キーを押し下げた場合、ここに来る。
        finish();
        return;
    }(以下省略)

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    switch(requestCode){
    case ID_Error_requestCode_YouTubeAppDialog:
        if(resultCode==RESULT_OK){
            //ここに何を書けば良いのだ?
            finish();//取り敢えず、再起動をしていただこうかね?
        }
        else{//Google PlayのYouTubeアプリ更新画面で「戻る」キーの押し下げ。
            finish();
        }
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

2013年3月3日日曜日

has leaked ServiceConnection com.google.android.youtube.player.internal.r

has leaked ServiceConnection com.google.android.youtube.player.internal.r

インターネットに接続しない環境下で、YouTubePlayerView.initialize()を実行すると、onInitializationFailureメソッドで結果を受け取ることになる。そして「戻る」キーを押し下げてアプリを終了させると下記のエラーが出る。むむむ!
E/ActivityThread(7188): Activity jp.Androyer.xxx.MainActivity has leaked ServiceConnection com.google.android.youtube.player.internal.r$e@41babb20 that was originally bound here
E/ActivityThread(7188): android.app.ServiceConnectionLeaked: Activity jp.Androyer.xxx.MainActivity has leaked ServiceConnection com.google.android.youtube.player.internal.r$e@41babb20 that was originally bound here
Leak while switching activity with YouTubeThumbnailViewsに似たようなエラーがあるが、回答はonInitializationSuccessメソッドで結果を受け取ることを前提にしている。
この回答から察するに、おそらく、エラーになったにも関わらず、YouTubePlayerを生成してしまい、かつYouTubePlayerをrelease()していないからだと思われます。

インターネットへの接続状態判断であれば、別の方法でできるので問題はない。しかし、他の原因でエラーが発生した場合、どうなるのか不安になる。

2013年2月23日土曜日

実験:getDrawingCache()により取得するBitmapには子Viewも含まれるのか

実験:getDrawingCache()により取得するBitmapには子Viewも含まれるのか

結論:含まれる

親View{
 子View
 子View
 ...


上記関係にあるViewにおいて、親ViewでgetDrawingCache()して取得したBitmapには子Viewの画像も描かれている。
実験に使ったコードは下記のとおり。

public class MainActivity extends Activity
    implements
    OnClickListener
    {
    Button btn1, btn2;
    Bitmap bm;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button)findViewById(R.id.Button1);
        btn1.setOnClickListener(this);
        btn2 = (Button)findViewById(R.id.Button2);
        btn2.setOnClickListener(this);
        bm = null;
    }

    @Override
    protected void onDestroy() {
        if(bm!=null) bm.recycle();
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        if(bm!=null){
            bm.recycle();
            bm = null;
        }
     
        if(v==btn1) v = (RelativeLayout)findViewById(R.id.RelativeLayout);
        else{
            if(v==btn2) v = (TextView)findViewById(R.id.TextView);
            else v = null;
        }
        if(v!=null){
            v.setDrawingCacheEnabled(true);
            bm = v.getDrawingCache().copy(Bitmap.Config.ARGB_8888, false);
            v.setDrawingCacheEnabled(false);
            ImageView iv = (ImageView)findViewById(R.id.Image2);
            iv.setImageBitmap(bm);
        }
    }
}

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >
    <RelativeLayout
        android:id="@+id/RelativeLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#9999ff"
        >
        <ImageView
            android:id="@+id/Image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:contentDescription="@string/app_name"
            android:background="#99ff99"
            >
        </ImageView>
        <Button
            android:id="@+id/Button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/Image"
            android:text="@string/Button1" 
            >
        </Button>
        <TextView
            android:id="@+id/TextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/Image"
            android:layout_below="@id/Button1"
            android:gravity="center"
            android:text="@string/hello_world" 
            android:background="#ff9999"
        />
    </RelativeLayout>
    <Button
        android:id="@+id/Button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Button2" 
        >
    </Button>
    <ImageView
        android:id="@+id/Image2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher"
        android:contentDescription="@string/app_name"
        >
    </ImageView>
</LinearLayout>