Nas Pakiety wewnątrz intencji nie są związane z bezpieczeństwem, ponieważ użytkownicy Androida zrobili to w prosty sposób. Moim zdaniem używanie pakietów i intencji do przekazywania większych obiektów nie jest dobrym pomysłem. staje się zbyt skomplikowana do implementacji, sprawia, że dostajesz obiekt w dół do prymitywów (gdy używasz paczki), a także tworzy kopię po drugiej stronie w pamięci (bierzesz jeden obiekt, ustawiasz wszystko wewnątrz intencji i ponownie tworzysz go na druga strona tworzy nową kopię), która dla obiektów, które mają większy ślad pamięci, nie jest dobra.
Proponuję:
- albo za pomocą sklepu singleton
- Korzystanie z klasy aplikacji (który działa również jak pojedyncza)
Często jestem przy użyciu singleton, który ma HashMap wewnątrz, gdzie generowany jest klucz całkowity (z atomowej liczby całkowitej) i obiekt umieszczony na mapie. Po prostu wyślij identyfikator w intencji jako dodatkowy i pobierz go po drugiej stronie, pobierając klucz z intencji i uzyskując dostęp do singletonu, aby pobrać i usunąć obiekt (z tej mapy) i użyć go w swojej nowej działalności/usłudze.
Oto próbka coś takiego:
(Uwaga: jest to część mojego lib dla wniosków spoczynku (https://github.com/darko1002001/android-rest-client) w przypadku, gdy chcesz zobaczyć więcej szczegółów na temat tego, jak wszystko jest zaimplementowane). w twoim przypadku będziesz musiał usunąć część kodu i zastąpić go swoim własnym, ale ogólny pomysł jest taki sam.
/**
* @author Darko.Grozdanovski
*/
public class HttpRequestStore {
public static final String TAG = HttpRequestStore.class.getSimpleName();
public static final String KEY_ID = "id";
public static final String IS_SUCCESSFUL = "isSuccessful";
private static final HashMap<Integer, RequestWrapper> map = new HashMap<Integer, RequestWrapper>();
private final AtomicInteger counter = new AtomicInteger();
private static Class<?> executorServiceClass = HTTPRequestExecutorService.class;
private final Context context;
private static HttpRequestStore instance;
private HttpRequestStore(final Context context) {
this.context = context;
}
public static HttpRequestStore getInstance(final Context context) {
if (instance == null) {
instance = new HttpRequestStore(context.getApplicationContext());
}
return instance;
}
public static void init(final Class<?> executorServiceClass) {
HttpRequestStore.executorServiceClass = executorServiceClass;
}
public Integer addRequest(final RequestWrapper block) {
return addRequest(counter.incrementAndGet(), block);
}
public Integer addRequest(final Integer id, final RequestWrapper block) {
map.put(id, block);
return id;
}
public void removeBlock(final Integer id) {
map.remove(id);
}
public RequestWrapper getRequest(final Integer id) {
return map.remove(id);
}
public RequestWrapper getRequest(final Intent intent) {
final Bundle extras = intent.getExtras();
if (extras == null || extras.containsKey(KEY_ID) == false) {
throw new RuntimeException("Intent Must be Filled with ID of the block");
}
final int id = extras.getInt(KEY_ID);
return getRequest(id);
}
public Integer launchServiceIntent(final HttpRequest block) {
return launchServiceIntent(block, null);
}
public Integer launchServiceIntent(final HttpRequest block, RequestOptions options) {
if (executorServiceClass == null) {
throw new RuntimeException("Initialize the Executor service class in a class extending application");
}
if (isServiceAvailable() == false) {
throw new RuntimeException("Declare the " + executorServiceClass.getSimpleName() + " in your manifest");
}
final Intent service = new Intent(context, executorServiceClass);
final RequestWrapper wrapper = new RequestWrapper(block, options);
final Integer requestId = addRequest(wrapper);
service.putExtra(KEY_ID, requestId);
context.startService(service);
return requestId;
}
public boolean isServiceAvailable() {
final PackageManager packageManager = context.getPackageManager();
final Intent intent = new Intent(context, executorServiceClass);
final List<ResolveInfo> resolveInfo = packageManager.queryIntentServices(intent,
PackageManager.MATCH_DEFAULT_ONLY);
if (resolveInfo.size() > 0) {
return true;
}
return false;
}
}
najprostszym sposobem jest wykorzystanie koncepcji wiązki –
Jak używać wiązek wysłać obiektów, które nie są ciągiem lub coś? –
Nie, ponieważ po prostu uczą cię, jak wykonywać podstawowe czynności, takie jak łańcuchy ... –