Android 上的 Headless Activity 具有 Viewmodel
Headless Activity on Android that has a Viewmodel
我正在创建一些地理围栏。为了稍后删除它们,我必须坚持使用 Intent
创建它们。
我想我会把它存储在一个 ViewModel
中并将它附加到一个无头的 Activity
上。当我发现这个时很兴奋:How to handle a silent SEND intent with a headless activity。但是,您必须在 activity 上调用 finish()
,我认为这也会导致 Viewmodel 被丢弃。
您不需要保留 Intent
本身来删除地理围栏,您尝试执行的操作将不起作用。
removeGeofences API takes a PendingIntent
. When comparing two PendingIntents to see if they are equal, they follow the rules of Intent.filterEqual():
Determine if two intents are the same for the purposes of intent resolution (filtering). That is, if their action, data, type, class, and categories are the same. This does not compare any extra data included in the intents.
只要你能构造一个Intent和之前一样的信息,你可以构造一个新的PendingIntent
并且仍然匹配。
或者,您可以使用 removeGeofences() method that takes the String request IDs, which you can add to your Geofence via setRequestId()。那么您只需要跟踪字符串即可取消地理围栏。
我正在创建一些地理围栏。为了稍后删除它们,我必须坚持使用 Intent
创建它们。
我想我会把它存储在一个 ViewModel
中并将它附加到一个无头的 Activity
上。当我发现这个时很兴奋:How to handle a silent SEND intent with a headless activity。但是,您必须在 activity 上调用 finish()
,我认为这也会导致 Viewmodel 被丢弃。
您不需要保留 Intent
本身来删除地理围栏,您尝试执行的操作将不起作用。
removeGeofences API takes a PendingIntent
. When comparing two PendingIntents to see if they are equal, they follow the rules of Intent.filterEqual():
Determine if two intents are the same for the purposes of intent resolution (filtering). That is, if their action, data, type, class, and categories are the same. This does not compare any extra data included in the intents.
只要你能构造一个Intent和之前一样的信息,你可以构造一个新的PendingIntent
并且仍然匹配。
或者,您可以使用 removeGeofences() method that takes the String request IDs, which you can add to your Geofence via setRequestId()。那么您只需要跟踪字符串即可取消地理围栏。