2016-08-24 4 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 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?)

答えて

0

MainQuestion - 作成時に.class変数に渡します。 Foo.classは実際にはClass型の変数です。アプリが「主要な活動」を持っていると仮定しているにもかかわらず、それがすべての通知を希望する場所です。

サイド質問 - すべての通知が1つのアクティビティに行きたいとは思いません。実際、私は、すべての通知に、意図に束縛される必要のある余分なパラメータがないとも仮定しません。私はsendNotificationをインテントをパラメータとして使用します。

+0

違いは何ですか?私は私のアプリがMainActivity(主な機能)とsencond SettingsActivity(私はいくつかの設定を行うことができます)を持っているという単純なケースを想定します。私は2つの通知をそれぞれパラメータとしてタップすると、何が違いますか?彼らは、対応する活動を集中して設定してアプリを呼び出す? –

関連する問題