从另一个活动

问题描述:

在调用活动的非静态方法我想从活动B 调用非静态方法在我的活动一个像从另一个活动

class A extend Activity(){ public void c(){}} 
class B extend Activity(){ A.C(); } 

我怎么能在Android的活动做到这一点帮助我。

public class Voice extends Activity { 

TextView resultTEXT ; 
MediaPlayer mp; 
ImageView view; 


private BluetoothAdapter btAdapter = null; 
private BluetoothSocket btSocket = null; 

int page; 

// SPP UUID service 
private static final UUID BTMODULEUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 

// String for MAC address 
private static String address; 
private static String status; 
BluetoothDevice device; 



private ConnectedThread mConnectedThread; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_voice); 
    //get the stored mac address of the device 
    SharedPreferences shared = getSharedPreferences("BtAddress", MODE_PRIVATE); 
    address = (shared.getString("btAddress", "")); 
    status = (shared.getString("connect", "")); 
    btAdapter = BluetoothAdapter.getDefaultAdapter(); 
    //create device and set the MAC address 
    device = btAdapter.getRemoteDevice(address); 

    checkBTState(); 

    if(status=="true") 
    { 
     new CountDownTimer(1000, 10000) { 

      public void onTick(long millisUntilFinished) { 
      } 

      public void onFinish() { 
       mp = MediaPlayer.create(Voice.this, R.drawable.onload); 
       mp.setLooping(false); 
       mp.start(); 
      } 

     }.start(); 

    } 

    view=(ImageView)findViewById(R.id.imageButton); 
    view.setOnTouchListener(new View.OnTouchListener() { 


     Handler handler = new Handler(); 

     int numberOfTaps = 0; 
     long lastTapTimeMs = 0; 
     long touchDownMs = 0; 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 

      switch (event.getAction()) { 
       case MotionEvent.ACTION_UP: 
        touchDownMs = System.currentTimeMillis(); 
        break; 
       case MotionEvent.ACTION_DOWN: 
        handler.removeCallbacksAndMessages(null); 

        if ((System.currentTimeMillis() - touchDownMs) > ViewConfiguration.getTapTimeout()) { 
         //it was not a tap 

         numberOfTaps = 0; 
         lastTapTimeMs = 0; 
         break; 
        } 

        if (numberOfTaps > 0 
          && (System.currentTimeMillis() - lastTapTimeMs) < ViewConfiguration.getDoubleTapTimeout()) { 
         numberOfTaps += 1; 
        } else { 
         numberOfTaps = 1; 
        } 

        lastTapTimeMs = System.currentTimeMillis(); 

        if (numberOfTaps == 2) { 
         handler.postDelayed(new Runnable() { 
          @Override 
          public void run() { 
           //handle double tap 
           Toast.makeText(Voice.this, "Help", Toast.LENGTH_LONG).show(); 

           mp = MediaPlayer.create(Voice.this, R.drawable.help); 
           mp.setLooping(false); 
           mp.start(); 
          } 
         }, ViewConfiguration.getDoubleTapTimeout()); 
        } 
        else if(numberOfTaps== 1) 
        { 
         handler.postDelayed(new Runnable() { 
          @Override 
          public void run() { 
           Toast.makeText(Voice.this, "proceed",Toast.LENGTH_LONG).show(); 
           Intent intent=new Intent(Voice.this,ChangeSpeed.class); 
           startActivity(intent); 
          } 
         }, ViewConfiguration.getTapTimeout()); 

        } 
      } 

      return true; 
     } 
    }); 
} 



public void onActivityResult(int request_result, int result_code, Intent i) 
{ 

    super.onActivityResult(result_code, result_code, i); 

    switch (result_code) 
    { 

     case 100: if(result_code == RESULT_OK && i != null) 
     { 
      ArrayList<String> result = i.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
      resultTEXT.setText(result.get(0)); 


     } 
      break; 


    } 

} 
@Override 
public void onResume() { 
    super.onResume(); 

    try 
    { 
     btSocket = createBluetoothSocket(device); 
    } 
    catch (IOException e) 
    { 
     Toast.makeText(getBaseContext(), "Socket creation failed", Toast.LENGTH_LONG).show(); 
    } 
    // Establish the Bluetooth socket connection. 
    try 
    { 
     btSocket.connect(); 
    } 
    catch (IOException e) 
    { 
     try 
     { 
      btSocket.close(); 
     } 
     catch (IOException e2) 
     { 
      Log.e("",""+e2); 
     } 
    } 
    mConnectedThread = new ConnectedThread(btSocket); 
    mConnectedThread.start(); 

    // send a character when resuming.beginning transmission to check device is connected 
    //If it is not an exception will be thrown in the write method and finish() will be called 
    mConnectedThread.write("x"); 
} 
private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException { 

    return device.createRfcommSocketToServiceRecord(BTMODULEUUID); 

} 
@Override 
public void onPause() 
{ 
    super.onPause(); 
    try 
    { 
     // Bluetooth sockets close when leaving activity 
     btSocket.close(); 
    } catch (IOException e2) 
    { 
     Log.e("",""+e2); 
    } 
} 
/* 
//Checks that the Android device Bluetooth is available turned on if off automatically 
*/ 

private void checkBTState() 
{ 

    if(btAdapter==null) 
    { 
     Toast.makeText(getBaseContext(), "Device does not support bluetooth", Toast.LENGTH_LONG).show(); 
    } else 
    { 
     if (btAdapter.isEnabled()) 
     { 
     } 
     else 
     { 
      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableBtIntent, 1); 
     } 
    } 
} 
private class ConnectedThread extends Thread { 
    // private final InputStream mmInStream; 
    private final OutputStream mmOutStream; 

    //creation of the connect thread 
    public ConnectedThread(BluetoothSocket socket) 
    { 
     // InputStream tmpIn = null; 
     OutputStream tmpOut = null; 

     try 
     { 
      //Create I/O streams for connection 
      // tmpIn = socket.getInputStream(); 
      tmpOut = socket.getOutputStream(); 
     } 
     catch (IOException e) 
     { 

     } 

     //mmInStream = tmpIn; 
     mmOutStream = tmpOut; 
    } 


    //write method 
    public void write(String input) 
    { 
     byte[] msgBuffer = input.getBytes(); 
     //converts entered String into bytes 
     try 
     { 

      mmOutStream.write(msgBuffer); 

     } catch (IOException e) 
     { 
      //if you cannot write, close the application 
      Toast.makeText(getBaseContext(), "Connection Failure", Toast.LENGTH_LONG).show(); 
      finish(); 

     } 
    } 

} 
/* 
method to on the Fan 
*/ 
public void functionFanOn() 
{ 

    mConnectedThread.write("1"); 
    // Send "1" via Bluetooth 
    Toast.makeText(getBaseContext(), "Turn on Fan", Toast.LENGTH_SHORT).show(); 
    Log.e("", "On"); 
} 

functionFanOn()是我想在B到呼叫

+0

你为什么要这样? 该方法的目的是什么? – raxelsson

+0

你不能。你能指定目的吗? – Stefan

+0

嗯,我想连接到蓝牙,我有类A的方法发送信号给Arduino在那个类我创建连接,所以在类B中,当用户点击按钮我想发送信号给Arduino调用A类的方法 –

请阅读我的示例代码

是这样的这一个活动类和含C()方法;方法

class A extend Activity 
{ 
    public void c() 
    { 

    } 
} 

,这是你的第二个活动等级

class B extend Activity 
{ 
    A a=new A(); 

    a.c(); 
} 

我希望这会帮助你

+0

我试过这个。但没有什么帮助 –

+2

虽然这在技术上是真的(也许你可以这样做),但这是一个非常非常糟糕的方法! –

+0

好的我如何解决这个问题有什么办法可以做到这一点 –

你必须与你的代码的结构问题。活动A中的函数建立了与Arduino的连接,应该在另一个类中移动。可以说一个Utils类。

+0

好的,我会尝试一下 –

  1. 重构的通用代码来一个新的类,这样就可以管理它通过单一实例
  2. 这LIB EventBus可以解决您的问题

另一种方式是创建Seprate类,并使用所有的方法的seprate类

class Utils 
{ 
    public void c(Context context) 
    { 
     //put your code 
     } 
} 

而且Activity类像这个

class MyActivity extends Activity 
{ 
    public void onCreate(Bundle b) 
    { 
     super.onCreate(b); 
     setContentView(R.layout.main); 


     //method calling 
     Utils utils=new utils(); 
     utils.c(MyActivity.this); 
    } 


}