0

I have app with two activities. The first one (Bluetooth Activity) is to search for Bluetooth devices and connect with them (Working fine). The second (Control Activity) have a control buttons. Each button suppose to send command via Bluetooth.

The problem is: 1. When I use Intent it pass date from "Control Activity" to "Bluetooth Activity", but also change the view to "Bluetooth Activity" which I don't want.

2.I have multiple command in "Control Activity", if I use "intent.putExtra" it's need to insert a KEY. How to find which key is sent.

"Control Activity"

Intent intent = new Intent(this,Bluetooth.class);
    intent.putExtra("blue", "b");
    startActivity(intent);

"Bluetooth Activity"

getIntent().getExtras().getString("blue")
eightShirt
  • 1,387
  • 2
  • 13
  • 25
a.alharazi
  • 25
  • 5

6 Answers6

1

You can define constants in Bluetooth Activity class, for example:

public static final String EXTRA_KEY_BLUE = "blue";

And then you have one definition of this constant so you can use it from every class, for example:

Intent intent = new Intent(this,Bluetooth.class);
intent.putExtra(Bluetooth.EXTRA_KEY_BLUE, "b");
startActivity(intent);

In Bluetooth activity you can check if the bundle contains specific keys.

galvan
  • 6,848
  • 7
  • 32
  • 52
1

You could also use Shared Preferences

Android Shared Preferences

mstranne
  • 88
  • 11
1

Create an intent pass class name or intnet action name

Intent intent = new Intent(this,Bluetooth.class);
     // put key/value data
    intent.putExtra("message", "Hello From Your Class");
     //  or you can add data to a bundle
    Bundle extras = new Bundle();
    extras.putString("status", "Data Received!");
     //  add bundle to intent
    intent.putExtras(extras);
     //  start the activity
    startActivity(intent);

We can access to the sent data by getting a reference to the sent intent using getIntent(); Here we can extract the sent message getIntent().getStringExtra(“message”) Also we can get the access to the attached bundle getIntent().getExtras() then we can extract the data from the bundle.

 @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);

    // 1. get passed intent 
    Intent intent = getIntent();

    // 2. get message value from intent
    String message = intent.getStringExtra("message");

    // 3. show message on textView 
    ((TextView)findViewById(R.id.tvMessage)).setText(message);

    // 4. get bundle from intent
    Bundle bundle = intent.getExtras();

    // 5. get status value from bundle
    String status = bundle.getString("status");

    // 6. show status on Toast
    Toast toast = Toast.makeText(this, status, Toast.LENGTH_LONG);
    toast.show();
}

Reference

For good practice

If you have a relatively small collection of key-values that you'd like to save, you should use the SharedPreferences Demo.

IntelliJ Amiya
  • 70,230
  • 14
  • 154
  • 181
  • @a.alharazi .This question is too old . Please check here http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android – IntelliJ Amiya Aug 04 '15 at 11:34
0

but also change the view to "Bluetooth Activity" which I don't want

If you don't want to start an activity, you'll have to do something else, like use a class that is not an activity, it might be suitable for such a thing as Bluetooth to have a singleton:

public enum Bluetooth {
    INSTANCE;

    //methods
}

That aside, the general way I pass to activities on start is I tend both define a private constant, and to put both sections of code in the same class:

public class Bluetooth extends Activity {

   private static final String EXTRA_KEY_BLUE = "blue";

   public static Intent createIntent(String blue) {
       Intent intent = new Intent(this,Bluetooth.class);
       intent.putExtra(EXTRA_KEY_BLUE, "b");
       return intent;
   }

   @Override
   public void onCreate(Bundle savedInstanceState){
       super.onCreate(...)
       String blue = getIntent().getExtras().getString(EXTRA_KEY_BLUE);

   }

}

Control Activity (with no mention of the keys, so it is strongly typed):

Intent intent = Bluetooth.createIntent("b");
startActivity(intent);
weston
  • 51,132
  • 20
  • 132
  • 192
0

in "Bluetooth Activity":

Bundle bundle = getIntent().getExtras();
if (bundle != null) {
    Set<String> keys = bundle.keySet();
    Iterator<String> it = keys.iterator();

    while (it.hasNext()) {
        String key = it.next();                        //your key
        bundle.get(key);                               //your value
    }
}
CodeNinja
  • 1,098
  • 2
  • 12
  • 27
0

A Intent is used to change from an Activity to other. If you only want use some functionality of Bluetooth would be better define a method in BluetoothActivity and call it directly from ControlActivity, but not changing the view to BluetoothActivity.

In "BluetoothActivity"

public static void action(String str) {
    //Some actions

}

And in "ControlActivity"

BluetoothActivity.action("blue")

Another way, if you don´t want use a static method, you could also keep a reference of BluetoohActivity from ControlActivity and then define previous method not static and call it from this reference.