【问题标题】:Launch notification activity from another package从另一个包启动通知活动
【发布时间】:2017-04-14 06:08:55
【问题描述】:

我想将意图设置为我的 MainActivity.class,它与我的 Notification.class 位于不同的包中。我尝试使用 ComponentName 但仍然给我 addParentStack(componentName) 和 NotificationManager 行的 NULL 指针异常。 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());
    }

}

更新

我尝试从 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("com.workdemos.user", "com.workdemos.user.MainActivity"); 一样更改。在 MainActivity 之前添加您的完整路径。
  • 我做到了。还是一样。
  • "com.workdemos.user"是你的应用包名吗?
  • 应用程序包是 com.workdemos 然后我有多个包(用户、首选项、模型、新闻)。用户中的 MainActivity 和首选项中的 Notification

标签: android push-notification android-notifications


【解决方案1】:

ComponentName 有一个将上下文和类作为参数的构造函数。您可以使用它来获取 ComponentName。

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

【讨论】:

  • 我将 ComponentName 与包和类一起使用,因为这个使用上下文的方法不起作用
  • 您在使用这种方法时遇到了什么问题?
  • getContext() 给了我错误,所以我使用了 getBaseContext() 这给了我 (java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName() ' 在您建议的这一行中出现空对象引用)错误
【解决方案2】:

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

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());
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多