从助手 class 调用 PendingIntent

Call PendingIntent from helper class

我在我的 Android 应用程序中创建了一个 NotificationHelper,用于在整个应用程序中处理我的通知。

如果我移动我的两个方法 (showNotification + stopNotification) 比方说一个片段,那么它工作得很好:-)

但是那一刻,我尝试从我的 NotificationHandler 访问相同的两个方法(方法是相同的),然后我得到这个异常:'(

而且我现在已经尝试了将近 3 个小时才弄明白这是为什么??!

exception from log.cat

看起来错误与此行中的 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);
}

}

创建一个仅包含静态方法的助手 class。从您的 Application class 中删除它。由于静态方法需要访问 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 变量。您的 Activity 应该使用 this 作为 Context 调用方法,而您的 Fragment 可以使用 getActivity() 作为 Context.