android - 单击通知打开错误的 Activity

标签 android android-intent android-activity notifications

点击通知后,错误的 Activity 被反对 - 主要应用程序 Activity ,但不是我的目标 Activity - NearPhotoActivity。

创建通知:

    public static Notification createNotification(Location location) {
        NotificationChannel mChannel = new NotificationChannel(
                "3000", "notification_channel", NotificationManager.IMPORTANCE_LOW);
        notificationManager.createNotificationChannel(mChannel);
        return buildNotification("Notification Title",
                "Notification Body",
                true,
                R.drawable.ic_mr_button_connecting_00_dark,
                createPendingIntent(NearPhotoActivity.class, location),
                mChannel);
    }

创建 PendingIntent:

    private static PendingIntent createPendingIntent(Class destinationActivityClass, Location location) {
        Intent intent = new Intent(MyApplication.getAppContext(),
                destinationActivityClass);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("nearLocation", location);
        return PendingIntent
                .getActivity(MyApplication.getAppContext(),
                        0,
                        intent,
                        PendingIntent.FLAG_CANCEL_CURRENT);
    }

应该打开的 Activity :

@RequiresApi(api = Build.VERSION_CODES.O)
public class NearPhotoActivity extends AppCompatActivity {
    private ImageView nearPhotoImageView;
    private TextView locationDescriptionTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_near_photo);

        onNewIntent(getIntent());


        nearPhotoImageView = findViewById(R.id.nearPhotoImageView);

        locationDescriptionTextView = findViewById(R.id.locationDescriptionTextView);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        if(intent.getExtras().containsKey("nearLocation")) {
            Location location = (Location) intent.getExtras().getSerializable("nearLocation");
            //Address address = MyAppUtils.getLocation(location.getLatitude(),location.getLongitude());
            locationDescriptionTextView.setText("You are " + location.distanceTo(MyApplication.getCurrentLocation()) + " meters");
        }
    }
}

最佳答案

打开了错误的 Activity,因为您在应用程序启动时将 MainActivity 设置为 Mainfest 中的默认值。

您应该在 MainActivity 中添加 onNewIntent 函数,而不是在 NearPhotoActivity 中。

主 Activity

override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)
    receiveIntent(intent)
}

fun receiveIntent(intent: Intent?) {
   Location location = (Location) intent.getExtras().getSerializable("nearLocation");
   Intent i = newIntent(MainActivity.this,NearPhotoActivity.class)
   i.putExtra("location",location)
   startActivity(i)
}

然后使用getExtra 获取NearPhotoActivity 类中的location

关于android - 单击通知打开错误的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55664901/

相关文章:

android - 如何运行我的 Android 自定义构建类型?

android - 如何使用 Intents 将对象从一个 Android Activity 发送到另一个?

android - 一个软件包是否可以有多个启动 Intent ?

android - 是否需要覆盖 onSaveInstanceState 来保存私有(private)字段?

android - 在 Android 中隔离单元和编写测试

Android 使用自定义图 block 更改谷歌地图图 block

android - 最小化应用程序、更改语言并恢复调用 onCreate() 而不是 onResume()

android - 从dailymotion嵌入链接获取.m3u8链接

android - 从应用程序内部获取私钥

android - 发送邮件后如何再次移动MainActivity?