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サーバーから取得

Android logcatのログをSDカードなどに出力する

Android

Android logcatのログをSDカードなどに出力する

ANR解析のためや、いろんなことのためにlogcatのログを取り出したいことはあると思いますが、

 /data/anr/traces.txt

などに出力されたものを

 adb cat /data/anr/traces.txt

なんて面倒ですし、エンジニアではない人から渡してもらうのに不便ですよねっ。

というわけで、普通の人でもファイルマネージャーでアクセスできる場所にlogcatを出力しちゃいましょう。

Google Playで配布しないアプリにクラッシュログ機能を実装するで作りましたMyApplication.javaなどに次のように書きましょう。

 @Override
 public void onCreate() {
      super.onCreate();
      String LOGDIR = Environment.getExternalStorageDirectory().getPath() + "/smart.location.edison/";
      String[] cmd = new String[] { "logcat", "-f", LOGDIR+"logcat", "-v", "time", "ActivityManager:W", "myapp:D" };
      try {
          proc = Runtime.getRuntime().exec(cmd);
      } catch (IOException e1) {
          Log.e(TAG, "ログ出力時エラー"+e1);
      }
 }