カレログチェッカーの作り方

カレログチェッカーをつくるには

下記の説明だけではよく分からない方は、「基礎から学ぶAndroidアプリ開発」をどうぞ

0.AndroidSDKをインストール
1.Eclipseを起動する。
2.新規Androidアプリ作成(この時CreateActivityチェックはつけない)
3.新規クラス「KarelogReceiver」を作成(SuperクラスはBroadcastReceiver)
4.「KarelogReceiver」のonReceiveメソッドの作成
ACTION_POWER_CONNECTEDのインテントを受け取ったらアプリケーションを検索してノーティフィケーションを表示するだけです。
ACTION_DISMISS_NOTIFICATIONというのは、ノーティフィケーションをタッチされたときに発行するインテントで、ノーティフィケーションを消去するだけの処理を行います。

public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action != null){
if(action.equals(Intent.ACTION_POWER_CONNECTED)){
String foundApp = checkPackages(context);
if(foundApp != null){
showNotification(context,foundApp);
}
}else if(action.equals(ACTION_DISMISS_NOTIFICATION)){
NotificationManager manager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(NOTIFICATION_ID);
}
}
}
5.checkPackagesの作成
パッケージリストを順番に捜してみる処理です。
先にアプリケーションのリストを取り出してから、検索した方が効率がいいですが、チェックする数が多くはないので、ここでは手を抜いています。

public static String checkPackages(Context context){
String foundApp = null;
for(int i=0;i<checkPackages.length;i++){
foundApp = findApplication(context,checkPackages[i]);
if(foundApp != null){
break;
}
}
return foundApp;
}
6.findApplicationの作成
引数にマッチするアプリケーションがインストールされていれば、パッケージ名を返します。
最初に見つかった一つだけを返すので、複数検出したい場合は、リターン値をListとかに変える必要があります。
引数のパッケージ名は先頭一致なので、ドメイン名の部分だけにしておくと同じベンダーのアプリがすべてヒットします。

public static String findApplication(Context context,String packageName){
PackageManager packageManager = context.getPackageManager();
List<ApplicationInfo> applicationInfoList =
packageManager.getInstalledApplications(0);
for(ApplicationInfo info : applicationInfoList){
if(info.packageName.length()>=packageName.length()){
if(info.packageName.substring(0, packageName.length()).equals(packageName)){
return info.packageName;
}
}
}
return null;
}
7.showNotificationの作成
ノーティフィケーションの表示を行います。書籍のCHAPTER05で説明しているのと同じ処理です。

public static void showNotification(Context context,String foundApp){
long time = Calendar.getInstance().getTimeInMillis();
String message = “Found:”+foundApp;
Notification notification=new Notification(R.drawable.icon,message,time);
Intent intent = new Intent(context,KarelogReceiver.class);
intent.setAction(ACTION_DISMISS_NOTIFICATION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context,context.getString(R.string.notify_title),context.getString(R.string.notify_message)+foundApp,pendingIntent);
NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID,notification);
}
8.各種定数など
各種定数の定義です。NOTIFICATION_IDは適当な値です。別に何でもいいです。
checkPackagesはArrayにしているので、複数のパッケージをチェックすることもできます。
先にあるとおりドメイン名だけにしておくと同じベンダーのすべてのアプリをチェックすることができます。

public static int NOTIFICATION_ID = 1020;
public static String[] checkPackages = {“jp.karelog.gpsmanager”};
public static final String ACTION_DISMISS_NOTIFICATION = “be.watana.karelogchecker.service.dismiss”;
9.Manifestの修正
receiverだけのアプリなのでActivityはありません。

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”be.watana.karelogchecker” android:versionCode=”1″
android:versionName=”1.0″>
<uses-sdk android:minSdkVersion=”8″ />
<application android:icon=”@drawable/icon” android:label=”@string/app_name”>
<receiver android:name=”.KarelogReceiver”>
<intent-filter>
<action android:name=”android.intent.action.ACTION_POWER_CONNECTED” />
<category android:name=”android.intent.category.DEFAULT” />
</intent-filter>
</receiver>
</application>
</manifest>
10.あとはImportするクラスを調整して 細かいエラーを直せばOKです。
stringsリソースが必要なので適当なものを追加する必要があります。
Eclipseのエラーを見れば何が必要かはわかるので、適当に作成します。
Layoutリソースなどは不要なのでlayoutフォルダーごと削除してしまっても問題有りません。
日本語と英語に対応するには、CHAPTER04の最後に有るとおりvalue-jaディレクトリを作って日本語用strings.xmlを作成します。

書籍に乗っていない情報はfindApplicationの中のPackageManagerを使う部分だけだと思います。
チェックするパッケージ名を変えるだけで簡単に「なんとかチェッカー」になります。

Eclipseプロジェクトのアーカイブは KarelogChecker.zip

ライセンスはApache License 2.0です。
このプログラムはAS IS(あるがまま)で提供されます。動作することは保証しません。
このコードを使ったこと、使わなかったことによる結果については一切の責任を負いません。
ソースコードについての質問には基本的に回答しません。
ソースには上記のコード以外に、テストのコードや定期実行、起動時実行などを試したものが含まれています。
使用する際にはかならずパッケージ名の変更をしてください。