2016-08-24 23 views
0

我有一個自定義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?)

回答

0

MainQuestion - 您將在創建時將它傳遞給.class變量。 Foo.class實際上是一個Class類型的變量。雖然你認爲一個應用程序有一個「主要活動」,並且這是它希望所有通知都去的地方,但我發現很少有這種情況。

旁邊的問題 - 我不認爲我會假設所有的通知都想去一個活動。事實上,我甚至不會假設所有通知都沒有額外的參數,他們需要捆綁到意圖中。我會讓sendNotification將一個意圖作爲參數。

+0

什麼會有所作爲?當我假設我的應用程序具有MainActivity(主要功能)和第二個SettingsActivity(我可以進行一些配置)的簡單情況時。當我點擊兩個通知並將每個通知作爲參數時,會有什麼不同?他們稱這個應用程序與相應的活動集中在一起? –