我有一個自定義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靜態速效任何其他類在那裏。
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()
我需要的MainActivity.class
作爲意向構造函數和X.addParentStack()方法的參數。當我打算將MyNotificationManager作爲* .aar庫提供時,我不知道該MainActivity會在Android App項目中被調用?我怎樣才能使這個通用?
方面問題:在最終的項目中,通過調用MyRootApplication.getAppNotificationManager().sendNotification(...)
是否可以正確使用它的任何類?有沒有更好的設計模式? (Dependecy Injection?)
什麼會有所作爲?當我假設我的應用程序具有MainActivity(主要功能)和第二個SettingsActivity(我可以進行一些配置)的簡單情況時。當我點擊兩個通知並將每個通知作爲參數時,會有什麼不同?他們稱這個應用程序與相應的活動集中在一起? –