カスタマイズされたMyNotificationManager
私のアプリ(package com.ralfi.myapp.mynotifier;
)はこれまでのところ動作します。私はこのmynotifier
パッケージを単一の* .aarライブラリとして展開したいと思います。これは私の他のすべてのアプリケーションにインポートして使用することができます。Android用(NotificationManager)ライブラリをモジュール化する方法は?
package com.ralfi.mynotifiier;
class MyNotificationManager {
Context context;
NotificationManager notificationManager;
public class MyNotificationManager(Context context) {
this.context = context;
mNotificationManager = (NotificationManager) this.context.getSystemService(Context.NOTIFICATION_SERVICE);
}
//..
public void sendNotification(NotificationCompat.Builder builder) {
Intent notificationIntent = new Intent(this.context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this.context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent notificationPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mNotificationManager.notify(0, builder.build());
}
}
私はAndroidアプリケーションプロジェクトでそれをインスタンス化し、そこにある他のクラスのist static availabeを作成します。
import com.ralfi.mynotifiier.MyNotificationManager; // e.g.: delivered in a *.aar
class MyRootApplication
//..
private static MyNotificationManager myNotificationManager;
//..
void onCreate(){
myNotificationManager = new MyNotificationManager(context);
//..
}
public static AppNotificationManager getAppNotificationManager() {
return appNotificationManager;
}
}
主な質問:あなたがMyNotificationManager.sendNotification()
で見ることができるように私が意図コンストラクタとX.addParentStack()メソッドのパラメータとしてMainActivity.class
を必要としています。 * .aarライブラリとしてMyNotificationManagerを配布しようとすると、Android AppプロジェクトでこのMainActivityが呼び出されるのは分かりません。これを一般的にするにはどうすればよいですか?
サイド質問:それはMyRootApplication.getAppNotificationManager().sendNotification(...)
を呼び出すことによって、最終プロジェクトのどのクラスを使用する適切な方法ですか?多分もっと良いデザインパターンがありますか? (Dependecy Injection?)
違いは何ですか?私は私のアプリがMainActivity(主な機能)とsencond SettingsActivity(私はいくつかの設定を行うことができます)を持っているという単純なケースを想定します。私は2つの通知をそれぞれパラメータとしてタップすると、何が違いますか?彼らは、対応する活動を集中して設定してアプリを呼び出す? –