Android 单击通知时打开了错误的活动
在点击通知后,错误的活动被阻止-主要应用程序活动,但不是我的目标活动-NearPhotoActivity 创建通知:Android 单击通知时打开了错误的活动,android,android-intent,android-activity,notifications,Android,Android Intent,Android Activity,Notifications,在点击通知后,错误的活动被阻止-主要应用程序活动,但不是我的目标活动-NearPhotoActivity 创建通知: public static Notification createNotification(Location location) { NotificationChannel mChannel = new NotificationChannel( "3000", "notification_channel", Notifica
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);
}
创建挂起内容:
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");
}
}
}
启动应用程序时,您在
Mainfest
中将MainActivity
设置为默认值,因此打开了错误的活动
您应该在MainActivity
中添加onNewIntent
函数,但不能在NearPhotoActivity
中添加
main活动
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
。在我的main活动中处理onNewIntent没有意义,因为我在NearPhotoActivity中需要额外的内容,因为location从那里向TextView组件提供信息。我猜它将返回JSON
,而不是location对象?否,它将返回有效的位置对象,现在一切正常。