单击通知会打开错误的活动

时间:2019-04-13 11:22:07

标签: android android-intent android-activity notifications

点击通知后,活动(主应用程序活动)处于错误状态,而我的目标活动(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);
    }

应打开的活动:

@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");
        }
    }
}

1 个答案:

答案 0 :(得分:1)

打开错误的活动是因为启动应用程序时,您在$item = ItemPR::where('item_code', '=', $item_code)->get(); foreach($item as $data){ $items = ItemPR::where('pr_number', '=', $data->item_code)->update(['receive' => 'received']); } return redirect()->back(); 中将MainActivity设置为默认值。

您应该在Mainfest中添加onNewIntent函数,但不能在MainActivity中添加

MainActivity

NearPhotoActivity

然后使用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类中的location