2016-08-15 5 views
0

ich mir helfen, nicht statische Methode in meiner Aktivität A von Aktivität B wieAufruf nicht statische Methode in Aktivität von einer anderen Aktivität

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

Wie kann ich tue dies in android Aktivität anrufen möge.

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() ist die Methode, die ich in B

+0

Warum willst du das? Was ist der Zweck der Methode? – raxelsson

+0

Sie können nicht. Können Sie den Zweck angeben? – Stefan

+0

Nun, ich möchte Verbindung mit Bluetooth und ich habe Methode in Klasse A, die Signale an Arduino senden In dieser Klasse ich die Verbindung auch so in Klasse B erstellen, wenn Benutzer klicken Sie auf Schaltfläche Ich möchte Signal an Arduino Aufruf dieser Methode in Klasse A –

Antwort

-3

Bitte lesen Sie meinen Beispielcode

Ist das ein Aktivitätsklasse wie diese und enthalten c() aufgerufen werden soll;

Methode
class A extend Activity 
{ 
    public void c() 
    { 

    } 
} 

Und Dies ist Ihre zweite Aktivitätsklasse

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

    a.c(); 
} 

Ich hoffe, das hilft Ihnen

+0

senden Ich habe das versucht. Aber nichts hilft –

+2

Obwohl dies technisch wahr ist (aka Sie können das tun), ist dies ein sehr, sehr schlechter Ansatz! –

+0

ok Wie löse ich das? Gibt es eine Möglichkeit, dies zu tun? –

1

Sie haben ein Problem mit der Struktur des Codes. Die Funktion in Aktivität A, die eine Verbindung zum Arduino herstellt, sollte in eine andere Klasse verschoben werden. Sagen wir eine Utils Klasse.

+0

ok Ich werde das versuchen –

1
  1. Refactoring Ihre gemeinsamen Code zu einer neuen Klasse, so dass Sie Manager durch einzelne Instanz
  2. Diese lib EventBus Ihr Problem lösen kann
0

Ein anderer Weg ist Seprate Klasse erstellen und alle Methode verwenden von seprate Klasse

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

und Aktivitätsklasse Gefällt Ihnen dieses

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


} 
Verwandte Themen