从另一个包启动通知 activity

Launch notification activity from another package

我想将意图设置为我的 MainActivity.class,它与我的 Notification.class 在不同的包中。我尝试使用 ComponentName 但仍然给我 addParentStack(componentName) 和 NotificationManager 行的空指针异常。 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference

package com.workdemos.preference;

public class Notification extends AppCompatActivity {

    public void pushNotification() {
        ComponentName componentName = new ComponentName("com.workdemos.user", "MainActivity");

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("New News")
                .setContentText("new news found in your preferred city");

        Intent resultIntent = new Intent();
        resultIntent.setComponent(componentName);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(componentName);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(resultPendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, builder.build());
    }

}

更新

我尝试从调用者 class 发送上下文,它在 MainActivity.class.

的同一个包中
 public void pushNotification(Context context) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("New News")
            .setContentText("new news found in your preferred city");

    Intent resultIntent = new Intent();
    resultIntent.setComponent(new ComponentName(context, MainActivity.class));
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(getBaseContext());
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(resultPendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, builder.build());
}

这样它通过了之前给我错误的行。但是没有通过addParentStack(MainActivity.class)。它仍然在该行给我同样的错误。

ComponentName 有一个以上下文和 class 作为参数的构造函数。您可以使用它来获取组件名称。

ComponentName componentName = new ComponentName(getContext(), MainActivity.class);

我找到了一种方法来完成这项工作,方法是创建一个构造函数来设置在实例启动行传递给它的上下文。

public class Notification {

    private Context context;

    public Notification(Context context) {
        this.context = context;
    }

    public void pushNotification() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("New News")
                .setContentText("new news found in your preferred city");

        Intent resultIntent = new Intent();
        resultIntent.setComponent(new ComponentName(context, MainActivity.class));

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(new ComponentName(context, MainActivity.class));
        stackBuilder.addNextIntent(resultIntent);

        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(resultPendingIntent);

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, builder.build());
    }

}