Reputation: 1
I wrote an application that has 2 activities: one activity is the main activity and the other will be called by the main through an Intent. In the main activity I will connect to an Arduino board through Bluetooth. However, I want the connection to continue when I call the sub activity, but it disconnects when I push the button of the phone to escape the application (wherever in the main or sub activity) and go to the applications screen of the phone. So, please give me some ideas.
The main activity:
public class BackgroundActivity extends Activity {
private static final String DEVICE_ADDRESS = "00:06:66:43:9B:57";
private Button Living_Room;
private Intent L_intent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Amarino.connect(this, DEVICE_ADDRESS);// CONNECT TO ARDUINO BOARD
Living_Room = (Button) findViewById(R.id.living);
Living_Room.setBackgroundColor(Color.TRANSPARENT);
Living_Room.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
L_intent = new Intent(view.getContext(), LivingRoom.class);
startActivityForResult(L_intent, 0);
}
});
}
@Override
protected void onStop(){
super.onStop();
//Amarino.disconnect(this, DEVICE_ADDRESS);
}
}
Upvotes: 0
Views: 338
Reputation: 357
If I understood you correctly, you want to be able to run your "sub activity" while your application is not in the foreground.. This is what Android Service is for! So try changing your "sub activity" to a service.
Check it out here: http://developer.android.com/reference/android/app/Service.html It's similar to an Activity, but it runs in the background and doesn't have a GUI.
Upvotes: 3