私はAndroidアプリケーションでNotificationHelperを作成しました。これは、アプリ全体の通知を処理するためのものです。ヘルパークラスからPendingIntentを呼び出します。
と私はその作業を完全に罰金:-)そして、のフラグメントを言わせし
しかし、今の私の二つの方法(showNotification + stopNotification)を移動する場合、私は同じ2つのメソッドにアクセスしてみてください(方法はあります( '
をそして、それは、なぜ私は、ほぼ3時間のために今把握しようとしている??
は、それは次のようになります。私の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);
}
}
どこからshowNotificationを呼び出しますか? ヘルパークラスは、通常、静的メソッドで行われます。アプリケーションを拡張するのではなく、コンテキストをコンテキストに渡す必要があります。 – shtolik
このヘルパーは通知を処理するために掛かっているので、静的メソッドを使用しています... MainActivityとFragmentsの両方から呼び出しています –