在另一个内部调用一个活动并获取方法

时间:2014-09-10 12:38:29

标签: android android-activity

我正在开发一个Android应用程序,我有一个问题,我希望你能帮我解决它。 所以我有一个名为MainActivity的activity1和名为myown的activity2。在myown我有一个公共课,我想在onItemClick() MainActivity中打电话。以下是这两项活动的代码:

MainActivity.java

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;

public class MainActivity  extends ActionBarActivity  {
class Active extends myown {


}
private BluetoothAdapter btadapter;
private Set<BluetoothDevice> pairedDevices;
private SeekBar redcontrol = null;
private SeekBar greencontrol = null;
private SeekBar bluecontrol = null;
private TextView redv;
private TextView greenv;
private TextView bluev;
private ListView lv;



LinearLayout linearLayout;
Button on, off, button;
private static final UUID MY_UUID =
        UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

        // Insert bluetooth devices MAC address
private static String address = "00:14:02:13:08:21";



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_CANCELED) {

    finish();
        System.exit(10);
    }
}



protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    setContentView(R.layout.activity_main);
    btadapter = (BluetoothAdapter.getDefaultAdapter());



    button = (Button)findViewById(R.id.button);

    lv = (ListView)findViewById(R.id.listView1);

    btadapter = BluetoothAdapter.getDefaultAdapter();

    button = (Button) findViewById(R.id.button);
    redv = (TextView) findViewById(R.id.redv);
    greenv = (TextView) findViewById(R.id.greenv);
    bluev = (TextView) findViewById(R.id.bluev);
    redcontrol = (SeekBar) findViewById(R.id.seekBar);
    greencontrol = (SeekBar) findViewById(R.id.seekBar2);
    bluecontrol = (SeekBar) findViewById(R.id.seekBar3);
    redcontrol.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        int progressChanged = 0;

        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            progressChanged = progress;
            redv.setText(String.valueOf(progress));
        }

        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }

        public void onStopTrackingTouch(SeekBar seekBar) {
            //Toast.makeText(MainActivity.this, "seek bar progress:" + progressChanged,
            // Toast.LENGTH_SHORT).show();
        }
    });
    greencontrol.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        int progressChanged = 0;

        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            progressChanged = progress;
            greenv.setText(String.valueOf(progress));
        }

        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }

        public void onStopTrackingTouch(SeekBar seekBar) {
           // Toast.makeText(MainActivity.this, "seek bar progress:" + progressChanged,

           //Toast.LENGTH_SHORT).show();
        }
    });
    bluecontrol.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        int progressChanged = 0;

        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            progressChanged = progress;
            bluev.setText(String.valueOf(progress));
        }

        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }

        public void onStopTrackingTouch(SeekBar seekBar) {
            //Toast.makeText(MainActivity.this, "seek bar progress:" + progressChanged,
               //     Toast.LENGTH_SHORT).show();
        }
    });

    linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
    on = (Button) findViewById(R.id.on);
    off = (Button) findViewById(R.id.off);

    on.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            on.setBackgroundColor(Color.parseColor("#57D3E8"));
            off.setBackgroundColor(Color.parseColor("#EAEAEA"));
            if (!btadapter.isEnabled()) {
                Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(turnOn, 0);
                Toast.makeText(getApplicationContext(), "Bluetooth Turning On"
                        , Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), "Already On",
                        Toast.LENGTH_SHORT).show();
            }
        }
    });


    off.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            off.setBackgroundColor(Color.parseColor("#57D3E8"));
            on.setBackgroundColor(Color.parseColor("#EAEAEA"));
            if (btadapter.isEnabled()) {
                btadapter.disable();

                Toast.makeText(getApplicationContext(), "Bluetooth Off"
                        , Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), "Already Off",
                        Toast.LENGTH_SHORT).show();
            }
        }
    });


}
public void list(View view)  {
    pairedDevices = btadapter.getBondedDevices();

    ArrayList list = new ArrayList();
    for(BluetoothDevice bt : pairedDevices)
        list.add(bt.getName()  +"\n" + bt.getAddress()  );


    Toast.makeText(getApplicationContext(),"Showing Paired Devices",
            Toast.LENGTH_SHORT).show();
    final ArrayAdapter adapter = new ArrayAdapter
            (this,android.R.layout.simple_list_item_1, list);
    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {

            String item = ((TextView) view).getText().toString();

            Toast.makeText(getBaseContext(), item, Toast.LENGTH_SHORT).show();


            for (BluetoothDevice device : pairedDevices) {

                if ("bluetooth".equals(device.getName())) {
                    Intent launchActivity2 = new Intent(MainActivity.this, myown.class);
                    MainActivity.this.startActivity(launchActivity2);

                    break;


                }
                if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(true)) {
                    Toast.makeText(getBaseContext(), "Connected",Toast.LENGTH_SHORT).show();

                    //do nothing
                }else {
                    Toast.makeText(getBaseContext(), "Unable to connect Light",Toast.LENGTH_SHORT).show();
                }
                view.setEnabled(false);

            }
        }
    });




}




@Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        return true;
    }

}

myown.java

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import java.io.IOException;
import java.util.UUID;

public class myown {
public class myOwnBroadcastReceiver extends BroadcastReceiver {

    ConnectToBluetooth connectBT;
    public BluetoothSocket scSocket;
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        BluetoothDevice discoveredDevice =            intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        connectBT = new ConnectToBluetooth(discoveredDevice);
        new Thread(connectBT).start();
    }

    class ConnectToBluetooth implements Runnable {
        private BluetoothDevice btShield;
        private BluetoothSocket mySocket = null;
        private UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

        public  ConnectToBluetooth(BluetoothDevice bluetoothShield) {
            btShield = bluetoothShield;
            try {
                mySocket = btShield.createRfcommSocketToServiceRecord(uuid);
            } catch (IOException createSocketException) {

            }
        }

        @Override
        public void run() {


            try {

                mySocket.connect();
                scSocket = mySocket;
            } catch (IOException connectException) {

                try {
                    mySocket.close(); //try to close the socket
                } catch (IOException closeException) {
                }
                return;
            }
        }

        // Will allow you to get the socket from this class
        public BluetoothSocket getSocket() {
            return mySocket;
        }

        /* Will cancel an in-progress connection, and close the socket */
        public void cancel() {
            try {
                mySocket.close();
       } catch (IOException e) {
      }
     }
    }
   }
  }

1 个答案:

答案 0 :(得分:0)

将此添加到您的活动中

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {


    //add this
       Intent launchActivity2 = new Intent(MainActivity.this, myown.class);
       MainActivity.this.startActivity(launchActivity2);
    //

    String item = ((TextView) view).getText().toString();
    Toast.makeText(getBaseContext(), item, Toast.LENGTH_SHORT).show();

    for (BluetoothDevice device : pairedDevices) {

      if ("bluetooth".equals(device.getName())) {
           Intent launchActivity2 = new Intent(MainActivity.this, myown.class);
           MainActivity.this.startActivity(launchActivity2);
           break;
      }

      if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(true)) {
          Toast.makeText(getBaseContext(), "Connected",Toast.LENGTH_SHORT).show();
          //do nothing
      }else {
          Toast.makeText(getBaseContext(), "Unable to connect Light",Toast.LENGTH_SHORT).show();
      }
   view.setEnabled(false);

        }
    }
});