Androidアプリ間での通信方法例
目的
AndroidアプリAとBがあるとして、
AからBのServiceを起動し、BのServiceでの処理結果をAで受け取るための方法です。
概要
A: BのServiceをintentに指定して投げます。BroadcastReceiverのonReceiveにてintentからExtraを受け取ります。
B: AのBroadcastReceiverをintentに指定して、Serviceの処理結果をputExtraしてbroadcastします。
実装例
Aサイド
AndroidManifest.xml
<manifest package="com.sample.A"> <receiver android:name=".AsampleBroadcastReceiver" android:exported="true" /> </manifest>
Aのサービスを起動させる
Intent intent = new Intent(); intent.setClassName("com.sample.B", "com.sample.B.BsampleService"); startService(intent);
Bからのbroadcastを受け取る
AsampleBroadcastReceiver.java
public class AsampleBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); int hoge = bundle.getInt("key"); // キーを指定してint型の値を受け取る } }
Pythonで超簡単なWebサーバーの立て方
ドキュメントルートにしたいディレクトリへ移動して
$ python -m SimpleHTTPServer ポート番号
でwebサーバーが立ち上がる。
http://localhost:ポート番号
でアクセス可能。
C言語の文字列比較
C言語では「文字列」を扱う変数はない。
「文字」の集合として扱っているため、
文字列を比較する際はループで1文字ずつ比較しなくてはならない。
比較できない例
if (str == "hogehoge"){ printf("合致!"); }else{ printf("合致しない..."); }
比較できる例
int compare_string(char str1[255], char str2[255]){ int i, len = strlen(str1); for(i=0;i<len;i++){ if(str1[i]!=str2[i]) return 0; } return 1; } if (compare_string(str1, "hogehoge")){ printf("合致!"); }else{ printf("合致しない..."); }
numpy で 配列の次元数を取得する
メモ: numpy で配列の次元数を取得する
import numpy A = numpy.array([1, 2, 3]) numpy.ndim(A) >> 1 A.ndim >> 1
の2種類ある。