2017-02-05 2 views
1

私はAndroidアプリケーションでNotificationHelperを作成しました。これは、アプリ全体の通知を処理するためのものです。ヘルパークラスからPendingIntentを呼び出します。

と私はその作業を完全に罰金:-)そして、のフラグメントを言わせし

しかし、今の私の二つの方法(showNotification + stopNotification)を移動する場合、私は同じ2つのメソッドにアクセスしてみてください(方法はあります( '

をそして、それは、なぜ私は、ほぼ3時間のために今把握しようとしている??

exception from log.cat

は、それは次のようになります。私のNotificationHandlerから)同じ、それから私は、この例外を取得します! eこの行のgetApplicationContext()には、次の行が含まれます。

PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext()、0、myIntent、Intent.FILL_IN_ACTION);

===ここに私のNotificationHandlerは===だ

public class NoteHandler extends Application { 



/** 
* Empty constructor 
*/ 
public NoteHandler() { 

} 

/** 
* Turning Notification ON 
*/ 
public void showNotification() { 



    Intent myIntent = new Intent(this, MainActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, myIntent, Intent.FILL_IN_ACTION); 

    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(getApplicationContext()) 
        // Setting LIGHTS and RINGTONE 
        .setLights(Color.WHITE, 300, 100) 
        //.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) 
        // Setting the ICONS 
        //.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.btn_switch_flash_on)) 
        .setSmallIcon(R.mipmap.ic_launcher) 
        // Setting the CONTENT 
        .setContentTitle(getString(R.string.app_name)) 
        .setContentText(getString(R.string.app_notification_flash)) 
        // Setting the INTENT 
        .setContentIntent(pendingIntent) 
        .setOngoing(true); 

    // Setting the color of SmallIconBackground (only for Android API 21 and above...) 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
     mBuilder.setColor(Color.parseColor("#6b394c")); 
    } 

    // Setting Priority to MAX (only for Android API 16 and above...) 
    if (android.os.Build.VERSION.SDK_INT >= 16) { 
     mBuilder.setPriority(Notification.PRIORITY_MAX); 
    } 

    // Sets an ID for the notification 
    int mNotificationId = 1; 
    // Gets an instance of the NotificationManager service 
    NotificationManager mNotifyMgr = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE); 
    // Builds the notification and issues it. 
    mNotifyMgr.notify(mNotificationId, mBuilder.build()); 
} 

/** 
* Turning Notification OFF 
*/ 
public void stopNotification() { 
    int mNotificationId = 1; 
    NotificationManager mNotifyMgr = 
      (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE); 
    mNotifyMgr.cancel(mNotificationId); 
} 

}

+1

どこからshowNotificationを呼び出しますか? ヘルパークラスは、通常、静的メソッドで行われます。アプリケーションを拡張するのではなく、コンテキストをコンテキストに渡す必要があります。 – shtolik

+0

このヘルパーは通知を処理するために掛かっているので、静的メソッドを使用しています... MainActivityとFragmentsの両方から呼び出しています –

答えて

0

のみ静的メソッドを含むヘルパークラスを作成します。 Applicationクラスからこれを削除します。静的メソッドはContextにアクセスする必要があるので、呼び出すときにパラメーターとして渡してください。例:

public static void showNotification(Context context) { 
    Intent myIntent = new Intent(context, MainActivity.class); 
    PendingIntent pendingIntent = 
     PendingIntent.getActivity(context, 0, myIntent, Intent.FILL_IN_ACTION); 
    ... 
} 

context変数にあなたがContextを必要とするたびに使用してください。 ActivitythisのメソッドをContextと呼び、FragmentgetActivity()Contextとして使用できます。

+0

WOWありがとうDavid !!私は今、方法に文脈で渡すことを忘れたことを見る...生地! –

+0

私は思考の生地を持っていますか?何らかの理由で、なぜヘルパークラスが常に静的であるべきか? –

+0

'static'は** must **ではありませんが、通常、どこからでも呼び出せるヘルパークラスにメソッドを集めます。そのため、クラスにはどのような状態(メンバ変数)も必要ないので、方法は「静的」であり得る。それは単なるベストプラクティスです。 –

関連する問題