简体   繁体   English

如何在后台的两个Android活动之间交换数据以通过蓝牙发送数据?

[英]How to exchange data between two android activities in background to send data via bluetooth?

I need to pass data from Activity A to Activity B and backwards in background, because Activity A has method, which initializing class, which is managing bluetooth connection, and I cant initialize that class from Activity B , because method in Activity A has already initialized it. 我需要将数据从Activity A传递到Activity B并在后台向后传递,因为Activity A具有初始化类的方法,该类正在管理蓝牙连接,并且我无法从Activity B初始化该类,因为活动A中的方法已经初始化它。

I tried to make method that I need to make static , but from static method I cant call non-static methods from my bluetooth class: 我试图使我需要使之static方法,但是从静态方法中,我无法从我的蓝牙类中调用non-static方法:

   public static void setup(String address) {
            bt = new Bluetooth(this, handler);
            outStringBuffer = new StringBuffer("");
            bt.startConn(address);
        //Java throws error that bt is non-static
   }

So my questions: 所以我的问题是:

  1. Is there a way to call setup() from another class when it is non-static ? 当它non-static时,有没有办法从另一个类调用setup()
  2. And if answer is YES how? 如果答案是肯定的,怎么办? Or if answer is NO How to pass data without calling any methods. 或者如果答案为否,那么如何在不调用任何方法的情况下传递数据。

Keep in mind that startActivityForResult() or startActivity() will not solve this problem, because I need to send data from Activity B to A many times and Activity A should call method from my bluetooth class which sends that data via bluetooth. 请记住, startActivityForResult()startActivity()不会解决此问题,因为我需要多次将数据从Activity B发送到A ,并且Activity A应该调用我的蓝牙类中的方法,该类通过蓝牙发送该数据。

PS I found a way with ViewFlipper, but in my case this is too complicated to merge two classes. PS我找到了ViewFlipper的一种方法,但就我而言,这太复杂了,无法合并两个类。 And I cant send data directly from Activity B to Bluetooth class, because Activity A has already opened socket and Java will throw exception that it cant start activity or something like that. 而且我无法将数据直接从Activity B发送到蓝牙类,因为Activity A已经打开了套接字,并且Java会抛出无法启动活动之类的异常。

OK that's very nice...to pass strings from activity 1 to activity 2 you need 好的,这很不错...要将字符串从活动1传递到活动2

A method to pass and retrieve this, Intents 一种传递和检索此方法的方法

public void setUp (String address) {
/* start an intent to pass the string data*/
Intent intent = new Intent(GridViewActivity.this,       MovieDetailActivity.class);

/* use the intent object to pass string to another activity using putExtra method */

intent.putExtra("your string");
 start intent(intent);
}

Then in your receiving activity use getStringExtra 然后在接收活动中使用getStringExtra

final String original_title =     getIntent().getStringExtra("your string");

then you can use this variable pass from activity using Intent and PutExra and getIntent and getStringExtra methods 那么您可以使用Intent和PutExra以及getIntent和getStringExtra方法从活动使用此变量传递

You can google more on this. 您可以在Google上搜索更多。 Passing data from activity using intents. 使用意图从活动传递数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM