Android targetSdkVersionについての説明

Android

Android targetSdkVersionについての説明

AndroidManifest.xmlの最初に書く

 <uses-sdk android:targetSdkVersion="4" android:minSdkVersion="4" />

というバージョンの宣言についてです。

minSdkVersionはアプリが動作する最低限のAPIレベルなんだろうなー、というのはすぐわかると思いますが、targetSdkVersionはなんなんだろう?というのをちゃんとしておきたいと思います。

targetSdkVersionはつまり、アプリがそのバージョンのためにデザインされて、テストもしてあるよ、というバージョンです。
その前後のバージョンでも動きます。
が、動作が違うようになります。

targetSdkVersionを高く設定していると、たとえば今これを書いている時点で(2014年8月4日)のAPIレベル19(Android 4.4 KITKAT)にすると、デフォルトの画面や動作がAndroid 4.4 KITKATのものになる、ということです。

例を上げますと、弊社でリリースしている「タクシー検索たくる タクシードライバーさん専用アプリ」で、

 <uses-sdk android:minSdkVersion="9" />

だけ宣言していた時のスクリーンショットが下記で
no_target_version_takuru.png;

 <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="19" />

上記のように宣言すると、同じ画面がこうなります。

target_version_takuru.png;

字が白くなったり、画面名称の表示部分がアイコンつきに変わったりしていますね。
つまり、動作している端末Android 4.4のデフォルトの画面で表示してくれているわけです。

しかし、これってやってほしい場合とそうでない場合がありますよね。

targetSdkVersionを宣言していないと

 <uses-sdk> tag should specify a target API level 
 (the highest verified version; when running on later versions, 
 compatibility behaviors may be enabled) with android:targetSdkVersion="?"

というエラーが出ます。

targetSdkVersionを最新のもので宣言していないと

Not targeting the latest versions of Android; compatibility modes apply. Consider testing and updating this version. Consult the android.os.Build.VERSION_CODES javadoc for details.

というエラーが出ます。

なので、このエラーメッセージにそそのかされて、最新バージョンを書いていたりすると、デザイン面やプログラムの動作面で思わぬ悪影響が出てしまったりします。
気をつけましょう!

ちなみに、
「Android OSのバージョンはわかるけど、APIレベルって何?この数字?」
という方は、下記のAndroid Developpersのサイトを見てみてください。

http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels

さらに、ちなみになのですが、文字がデフォルトで白になってしまったり、アクションバーのデザインが変更になってしまうのは、テーマの変更で

 <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Light" >

などとやれば治ります。

しかし、これは今度デフォルトが白背景、黒文字になるので、そうでない画面はまたアクティビティごとにテーマを変更したりしないといけません。
ちなみに、黒背景白文字は下記のような感じです。

 <activity
            android:name=".SettingActivity"
            android:label="@string/setting"
            android:theme="@android:style/Theme.Black"  >

Android rowspan のように実装する

Android

Android rowspan のように実装する

htmlで言うところのrowspanという属性のように、セルに縦ぶち抜きを指定するようなレイアウトタグや属性はありません。

LinearLayoutを利用して、rowspanのように実装します。

             <TableLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_margin="5dip"
                android:orientation="vertical" >
              <!--中略--> 
                 <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical" >
 
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/indispensable"
                        android:textColor="@color/red_text" >
                    </TextView>
 
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/street_address"
                        android:textColor="@color/black_text" >
                    </TextView>
 
                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="vertical" >
 
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dip"
                            android:text="@string/street_address_notice"
                            android:textColor="@color/green_text"
                            android:textSize="12sp" >
                        </TextView>
 
                        <EditText
                            android:id="@+id/street_address"
                            android:layout_width="230dip"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dip"
                            android:inputType="text" >
                        </EditText>
                    </LinearLayout>
                </TableRow>
             </TableLayout>

Android receiverに値を渡す

Android

Android receiverに値を渡す

常駐的にアプリを動かしたい時があります。
たとえばアラーム機能とか、場所が変わったりした時に動作させる、などです。

その時、Intentとしてレシーバーを登録しますが、レシーバーにActivityから値を渡したいな、という時があると思います。

下記はサンプルです。

 //呼び出し元のアクティビティ
 ChangedReceiver changeReceiver = new ChangedReceiver(status);
 Intent intent = new Intent(this, ChangedReceiver.class);
 //statusというStringにhogehoge が入ってます
 intent.putExtra(status, "hogehoge"); 
 //呼び出されるレシーバー
 public void onReceive(Context context, Intent intent) {
   //Intent登録時に呼ばれたステータス、hoghegeを取得
   String status = intent.getStringExtra("status");
 }

Android notification 青いLEDを点滅させるサンプル

Android

Android notification 通知で青いLEDを0.1秒ごとに点滅させるサンプル

notificationとは、Androidアプリのシステムトレイと呼ばれる上部の部分にアイコンを表示させたり、する機能です。
新しいインテントを立ち上げる、なども含まれています。
defaultsを使ってもLEDが点滅しなかったので、青いLEDを0.1秒ごとに点滅させる、というふうに決め打ちしました。

 private static void generateNotification(Context context, String message) {
    	
    	//システムトレイに通知するアイコン
        int icon = R.drawable.ic_stat_gcm;
        long when = System.currentTimeMillis();
 
        Notification notification = new Notification(icon, message, when);
        String title = context.getString(R.string.app_name);
        
        //ステータスバーをクリックした時に立ち上がるアクティビティ
        Intent notificationIntent = new Intent(context, OfferDisplayActivity.class);
        
       notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        
        PendingIntent intent =
                PendingIntent.getActivity(context, 0, notificationIntent, 0);
        
        notification.setLatestEventInfo(context, title, message, intent);
 
        //通知の種類 音 バイブにしている時は鳴らない 
        notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE ;
        notification.flags =  Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL ; 
        notification.ledOnMS = 100;
        notification.ledOffMS = 100;
        notification.ledARGB = Color.BLUE;
 
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
 
        notificationManager.notify(0, notification);
     
 
        
    }

Android new JSONObjectが失敗する

Android

Android new JSONObjectが失敗する

Webサーバーと通信して、結果をAndroid端末の方で利用する、という時にJSONを使って値をやり取りすることが多いと思います。

 result = exec_post("http://hogehoge.com");
 jArray = new JSONObject(result);

なぜかnew JSONObjectがうまくいかない、ということが起こりました…。

原因はサーバー側で出力する形式も、JSONのオブジェクト形式にしておかないといけないかったんですね(^_^;

つまり、サーバー側の出力が

 [{"name":"hogehoge","id":"2"}]

のような[]で始まって終わる配列形式のJSONでは失敗し、

 {"name":"hogehoge","id":"2"}

という{}で始まって終わるオブジェクト形式のJSONでないとダメということです。

だってnew JSONObject、ですもんね…

Android Developpers
http://developer.android.com/reference/org/json/JSONObject.html

Android JSON形式のデータをWebサーバーから取得