【问题标题】:Android: passing data between activitiesAndroid:在活动之间传递数据
【发布时间】:2015-08-04 07:14:46
【问题描述】:

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

问题是: 1. 当我使用 Intent 时,它会将日期从“控制活动”传递到“蓝牙活动”,但也会将视图更改为我不想要的“蓝牙活动”。

2.我在“控制活动”中有多个命令,如果我使用“intent.putExtra”,则需要插入一个KEY。如何查找发送的密钥。

“控制活动”

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

“蓝牙活动”

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

【问题讨论】:

    标签: android android-activity android-studio bluetooth


    【解决方案1】:

    您也可以使用共享首选项

    Android Shared Preferences

    【讨论】:

      【解决方案2】:

      您可以在蓝牙Activity类中定义常量,例如:

      public static final String EXTRA_KEY_BLUE = "blue";
      

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

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

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

      【讨论】:

        【解决方案3】:

        创建意图传递类名称或 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

        【讨论】:

        【解决方案4】:

        但也将视图更改为我不想要的“蓝牙活动”

        如果您不想启动活动,则必须做其他事情,例如使用不是活动的类,它可能适合蓝牙之类的单例:

        public enum Bluetooth {
            INSTANCE;
        
            //methods
        }
        

        除此之外,我在启动时传递给活动的一般方式是我倾向于定义一个私有常量,并将两个代码部分放在同一个类中:

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

        控制活动(没有提到键,所以它是强类型的):

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

        【讨论】:

          【解决方案5】:

          在“蓝牙活动”中:

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

          【讨论】:

            【解决方案6】:

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

            在“蓝牙活动”中

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

            在“ControlActivity”中

            BluetoothActivity.action("blue")
            

            另一种方式,如果您不想使用静态方法,您还可以保留 ControlActivity 对 BluetoohActivity 的引用,然后定义先前的方法不是静态的,并从该引用中调用它。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-04-19
              • 2014-12-08
              • 2012-08-29
              • 2011-10-06
              相关资源
              最近更新 更多