今回は取得したメソッドの使い方。

ClassLoader classLoader = ClassLoader.getSystemClassLoader();
Class<?> pmClass = classLoader.loadClass("android.content.pm.PackageManager");

// pmClass.newInstance();
PackageManager pm = getPackageManager();

Method method = pmClass.getMethod("getInstallerPackageName", String.class);
Object result = method.invoke(pm, "com.android.alarmclock");

メソッドを使用するにはクラスのインスタンスが必要なのですが、
PackageManagerの生成は面倒なので手抜きです(笑)

もう少し別の例を用意した方がよかったですね。。。



クラスの動的なインポート方法です。

以下の例は普通にインポートできるクラスですが、あえてクラスロード。


ClassLoader classLoader = ClassLoader.getSystemClassLoader();
Class<?> pmClass = null;
Method[] methods = null;
try {
pmClass = classLoader.loadClass("android.content.pm.PackageManager");
methods = pmClass.getMethods();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

getMethodsで全てのメソッドをとりあえず取得。

非公開メソッドも取得できます。

Androidでjarを作成してみよう。

jarをライブラリとして使用する。

「Androidな共有ライブラリ」で書いたように

Projectを右クリックし、プロパティ⇒Androidを選択。
Libraryの Is Libraryチェックボックスをチェック。

AndroidManifest.xmlの「Define an application tag in the AndroidManifest.xml」のチェックを外す。

あとはエクスポートでJAVAのJARを選択する。

あとはAndroidManifest.xmlを外して作成。

これでとりあえず作成できます。

次回はjarを動的にインポートする方法を書いてみます。


昨年末から始まった、「Androidアプリケーション技術者認定試験」というのを受けてきました。

OESF認定の試験となります。

試験はCBT方式で90分、4択って感じです。

普通に解けるかなと思ったら意外に難しかったです。

Androidフレームワークはかなり把握していないと大きなダメージが。。

けしてあなどってはいなかったのですが。。

とはいえ、なんとか合格しました。

今回はベーシック試験だったので、次はもっと上ですけね~


起動しているサービスの一覧を取得する方法です。

以下、サンプルです。

private void serviceList() {
ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> services = am.getRunningServices(Integer.MAX_VALUE);

try{
for(int i = 0; i < services.size(); i++){
RunningServiceInfo serviceInfo = services.get(i);
ComponentName serviceName = serviceInfo.service;
Log.i("Service","Service["+i+"]:" + serviceName.getClassName());
}

}catch(Exception e){
Log.i("Error",e.getMessage(),e);
}
}


起動しているサービスの一覧がログに出力されます。
いやー、久々の更新だ。

あまり有益な内容ではないが、通知バーになんか表示してみたくなりました。

以下、ソースです。
通知をクリック後のIntentにはとりあえず起動アクティビティを指定しています。

NotificationManager mManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notificate = new Notification(R.drawable.icon, "test", System.currentTimeMillis());
Intent notificateIntent = new Intent(this, xxxxxxxxxx.class);
notificateIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificateIntent, 0);
notificate.setLatestEventInfo(getApplicationContext(), "test", "テスト中です", contentIntent);

mManager.notify(R.string.app_name, notificate);

いじょうです。
Androidで画面が横向きかを判定する必要がある場合、、

Configuration config = getResources().getConfiguration();
if(config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(Configuration.ORIENTATION_PORTRAIT);
}

結構、役に立ちますね。これは。
Androidでもhostsを書き換えることができます。

めったにないかもしれませんがこんな感じです。

まずは端末からhostsをpullします。

adb remount

adb pull /system/etc/hosts ./

pullしたhostsを書き換えます。

※既存と同じ形式で書いてください。

書き換えたhostsをpushします。

adb push ./hosts /system/etc

以上で書き換えは完了です。

最後に適用させるために一応再起動してください。


よく考えたら、エミュレータからアプリを抜き出せば簡単なのでは、、
日本語入力できるし。

というわけで1.6で試してみる。

adb pull /system/app/OpenWnn.apk ./OpenWnn.apk
adb install -r OpenWnn.apk

簡単にできました。