I have an application that contains a main Activity, which has a very simple screen with all application setup options and a few user selections. The main screen buttons start other Activities.
(1) I'm now implementing a bluetooth scanner into the application and i would like to be able to edit settings and establish a bluetooth connection on the main Activity and pass that connection (via Bundle or Parcelable perhaps?) to the other Activities.
I looked up Bundle as a way to pass the connection, but it appears to only accept primitive and basic types as values. I noticed Intent.putExtra(String, Parcelable) was available, but Parcel sort of confused me. What would be the best way to implement this, or is it even possible?
some relevant code:
//subclass definitions for the Runnables which start the applications in their own threads
private final Handler smsHandler = new Handler();
private final Handler smsOutHandler = new Handler();
private final Runnable smsIntentRunnable = new Runnable() {
public void run() {
Intent smsIntent = new Intent(smsMobile.this, smsActivity.class);
startActivity(smsIntent);
}
};
private final Runnable smsOutIntentRunnable = new Runnable() {
public void run() {
Intent smsOutIntent = new Intent(smsMobile.this, smsOut开发者_运维百科Activity.class);
startActivity(smsOutIntent);
}
};
//button listeners located in onCreate(), which start the Activities
newTicket.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
smsHandler.post(smsIntentRunnable);
}
});
updateTicket.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
smsOutHandler.post(smsOutIntentRunnable);
}
});
(2) Could I also pass a database connection? I understand that SQLiteOpenHelper must be subclassed, which could complicate IPC, but thought it might still be possible somehow?
I am in the process of rewriting an app to share a bluetooth service between two activities. Previously I have just solved the problem by making initializing the bluetooth service as a public object in the starting activity (where the service is set up, and from where other activities are launched), and then invoking the .read() and .write() functions of the public service from the new activity. This works fine for writing, but it's a bit onerous for reading (I have to launch a thread each time I expect data back, to sit and wait for data). I've migrated all of my other projects to just do certain actions cued from the bluetooth service handler when certain key data are read by the service (e.g., update temperature on user interface when coded temperature data is received). To be able to have the more elegant reading with both activities, I added a new function in my bluetooth service:
/**
* Change the handler (route handler info to new activity). */
public synchronized void newHandler(Handler handle) {
mHandler = handle;
}
Then call on this function to reassign a new handler from the new Activity when the new Activity is launched (e.g., "StartingActivity.mBTService.newHandler(NewActivityHandler);") Of course, when returning back to the StartingActivity make sure to reassign the Handler appropriately ;-)
To my surprise, it works, without all the overhead of having to make the bluetooth service a singleton, parcelable object, or a global service through the Application (none of which I was able to code successfully).
I'm going to test more thoroughly, but hopefully this helps someone (about time I give back something- I've learned a lot from this community)!
There are several options to share data between activities and services.
Update: to expand on your situation a little bit:
You only need IPC/Parcelable when code runs in separate processes. You have to specify in manifest to make code run in separate processes: http://developer.android.com/guide/topics/fundamentals.html#procthread
In your case I asume you activities run in the same process, so you can simply use custom Application class to store/share this data. Make sure it's
synchronized
if you access it from different threads. Alternatively you can use any other approach listed in the first link.You can of course pass database connection within same process, as this is one java application. But you can not pass it via IPC between processes, because all classes need to implement Parcelable in order to pass via IPC and, AFAIK, SQLiteDatabase does not implement parcelable (classes talking to native functions usually don't).
精彩评论