从帮助程序类

时间:2017-02-05 09:36:05

标签: android android-pendingintent

我在我的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);
}

}

1 个答案:

答案 0 :(得分: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变量。您的Activity应使用this作为Context来调用该方法,而Fragment可以使用getActivity()作为Context