Android: 在活动之间传递数据

Android: passing data between activities

我有两个活动的应用程序。第一个 (Bluetooth Activity) 是搜索蓝牙设备并与它们连接(工作正常)。第二个(Control Activity)有一个控制按钮。每个按钮都假设通过蓝牙发送命令。

问题是: 1. 当我使用 Intent 时,它将日期从 "Control Activity" 传递到 "Bluetooth Activity",但也将视图更改为我不想要的 "Bluetooth Activity"。

2.I在"Control Activity"中有多个命令,如果我使用"intent.putExtra"需要插入一个KEY。如何找到发送的密钥。

"Control Activity"

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

"Bluetooth Activity"

getIntent().getExtras().getString("blue")

可以在蓝牙中定义常量Activityclass,例如:

public static final String EXTRA_KEY_BLUE = "blue";

然后你有了这个常量的一个定义,所以你可以在每个 class 中使用它,例如:

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

在蓝牙中 activity 您可以检查捆绑包是否包含特定密钥。

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

如果您不想启动 activity,则必须执行其他操作,例如使用不是 activity 的 class,它可能是适合蓝牙这种东西单例:

public enum Bluetooth {
    INSTANCE;

    //methods
}

除此之外,我在开始时传递给活动的一般方法是我倾向于定义一个私有常量,并将两部分代码放在同一个 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);

   }

}

控件Activity(没有提到键,所以它是强类型的):

Intent intent = Bluetooth.createIntent("b");
startActivity(intent);

在"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
    }
}

Intent 用于从 Activity 更改为其他。如果您只想使用蓝牙的某些功能,最好在蓝牙Activity 中定义一个方法并直接从 ControlActivity 调用它,但不要将视图更改为蓝牙Activity.

在"BluetoothActivity"

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

}

并且在"ControlActivity"

BluetoothActivity.action("blue")

另一种方法,如果你不想使用静态方法,你也可以从 ControlActivity 中保留对 BluetoohActivity 的引用,然后定义以前的非静态方法并从这个参考。

您也可以使用共享首选项

Android Shared Preferences

创建意图传递 class 名称或 intnet 操作名称

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);

我们可以通过使用 getIntent() 获取对已发送意图的引用来访问已发送的数据; 这里我们可以提取发送的消息getIntent().getStringExtra(“message”) 我们还可以访问附加的包 getIntent().getExtras() 然后我们可以从包中提取数据。

 @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

良好实践

如果您要保存的键值集合相对较小,则应使用 SharedPreferences Demo