2016-08-15 94 views
0

在調用活動的非靜態方法我想從活動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類的方法 –

回答

-3

請閱讀我的示例代碼

是這樣的這一個活動類和含C()方法;方法

class A extend Activity 
{ 
    public void c() 
    { 

    } 
} 

,這是你的第二個活動等級

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

    a.c(); 
} 

我希望這會幫助你

+0

我試過這個。但沒有什麼幫助 –

+2

雖然這在技術上是真的(也許你可以這樣做),但這是一個非常非常糟糕的方法! –

+0

好的我如何解決這個問題有什麼辦法可以做到這一點 –

1

你必須與你的代碼的結構問題。活動A中的函數建立了與Arduino的連接,應該在另一個類中移動。可以說一個Utils類。

+0

好的,我會嘗試一下 –

1
  1. 重構的通用代碼來一個新的類,這樣就可以管理它通過單一實例
  2. 這LIB EventBus可以解決您的問題
0

另一種方式是創建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); 
    } 


}