Note that there are some explanatory texts on larger screens.

plurals
  1. POcalling method of one activity from other activity
    text
    copied!<p>I have two activities Main nd AvaiableDevices in Main activity i have a button btnAdvc in AvailableDevices </p> <pre><code>public class MainActivity extends Activity { Button btnAdvc; Button btnPdvc; Button btnDdvc; Button btnMdvc; public BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); public static final int REQUEST_ENABLE_BT = 1; public static UUID MY_UUID=null; public BluetoothServerSocket mmServerSocket; public void onCreate(Bundle savedInstanceState) { try{ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MY_UUID= UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //Function enbling Bluetooth enableBluetooth(); ///Function to initialize components init(); //Calling AvailableDevices class's method searchDevice to get AvailableDevices btnAdvc.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { call(); } }); }catch(Exception e){Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();} } public void init() { btnAdvc=(Button)findViewById(R.id.btnAdvc); btnPdvc=(Button)findViewById(R.id.btnPdvc); btnDdvc=(Button)findViewById(R.id.btnDdvc); btnMdvc=(Button)findViewById(R.id.btnMdvc); } public void call() { Intent intent=new Intent(this,AvailableDevices.class); startActivity(intent); } } </code></pre> <p>I have a method searchDevices() in AvailableDevices activity </p> <pre><code>public class AvailableDevices extends Activity { public BluetoothAdapter mBluetoothAdapter; public BluetoothDevice device; public ListView lv;//for Available Devices public ArrayList&lt;String&gt; s=new ArrayList&lt;String&gt;(); public HashSet&lt;String&gt; hs = new HashSet&lt;String&gt;(); public ArrayAdapter&lt;String&gt; adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_available_devices); adapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, s); lv=(ListView)findViewById(R.id.listView1); Intent intent=getIntent(); } public AvailableDevices(BluetoothAdapter bt) { mBluetoothAdapter =bt; } public void searchDevice() { if(mBluetoothAdapter.isEnabled()) { mBluetoothAdapter.startDiscovery(); // Create a BroadcastReceiver for ACTION_FOUND final BroadcastReceiver mReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); // When discovery finds a device if (BluetoothDevice.ACTION_FOUND.equals(action)) { try{ // Get the BluetoothDevice object from the Intent device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // Add the name and address to an array adapter to show in a ListView s.add(device.getName()+ "\n" + "#"+ device.getAddress()); //To avoid duplicate devices put it in to hash set which doesn't allow duplicates hs.addAll(s); //Clear all the devices in String array S s.clear(); //Replace with hash set s.addAll(hs); // lv.setAdapter(adapter); }catch(Exception e){Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();} } lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { //A local bluetooth device to Hold the selected device to connect BluetoothDevice devtoconnect; public void onItemClick(AdapterView&lt;?&gt; parentAdapter, View view, int position, long id) { try{ mBluetoothAdapter.cancelDiscovery(); //A local String array to hold the Name and address of the selected bluetooth device String[] separated = adapter.getItem(position).split("#"); if(mBluetoothAdapter.checkBluetoothAddress(separated[1])==true) { devtoconnect = mBluetoothAdapter.getRemoteDevice(separated[1]); } // Create object of Connect Thread Class to get connection // ConnectThread t1=new ConnectThread(devtoconnect); }catch(Exception e){} }//Closes onItemClick(AdapterView&lt;?&gt; parentAdapter, View view, int position, long id) }); //Closes lv.setOnItemClickListener(new AdapterView.OnItemClickListener() }//Coses function onReceive };//Closes BroadcastReceiver // Register the BroadcastReceiver IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy }//if }// function searchDevices } </code></pre> <p>when i click in btnAdvc of main the searchDevices() method of AvailableDevices activity should be called how can i do it?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload