从其他活动中调用一个活动的方法

时间:2013-12-18 07:50:35

标签: android

我有两项活动主要活动主要活动中的AvaiableDevices我在AvailableDevices中有一个按钮btnAdvc

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

我在AvailableDevices活动中有一个方法searchDevices()

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
}

当我点击main的btnAdvc时,应该调用AvailableDevices活动的searchDevices()方法,我该怎么办?

3 个答案:

答案 0 :(得分:2)

您可以使用广播消息。按钮点击后发送广播。在另一个活动中注册一个BroadcastReceiver,它将处理brodcast。所以每当你得到所需的消息,然后在第二个活动中调用该方法。

答案 1 :(得分:1)

从提供的代码看,searchDevices()方法看起来不是一种独立的方法,而是使用AvaiableDevices类中的decalred变量/属性。所以你可以尝试的可能就像

    Intent intent=new Intent(this,AvailableDevices.class);
    intent.putExtra("CallSearchDevices", true);
    startActivity(intent);

在onCreate方法结束时的AvailableDevices中,

    Intent intent=getIntent();
    boolean flag = intent.getBooleanExtra("CallSearchDevices", false);
    if (flag){
      searchDevices();
    }

我希望这会有所帮助

答案 2 :(得分:0)

您应该声明方法static,以便您可以像其他活动中的AvailableDevices.searchDevices()一样调用它

相关问题