У меня есть два вида деятельности Основные обозначения даты AvaiableDevices в Основной вид деятельности у меня есть кнопка btnAdvc в AvailableDevicesвызова метода одной деятельности от другой деятельности
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);
}
}
У меня есть метод searchDevices() в деятельности AvailableDevices
public class AvailableDevices extends Activity
{
public BluetoothAdapter mBluetoothAdapter;
public BluetoothDevice device;
public ListView lv;//for Available Devices
public ArrayList<String> s=new ArrayList<String>();
public HashSet<String> hs = new HashSet<String>();
public ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_available_devices);
adapter = new ArrayAdapter<String>(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<?> 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<?> 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
}
Когда я нажимаю на btnAdvc main, вы должны вызвать метод searchDevices() операции AvailableDevices, как я могу это сделать?
Создать статический метод –