2013-12-18 54 views
0

一种活性的方法我有两个活动主ND 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); 
    } 
} 

我有一个方法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的主要searchDevices()方法的AvailableDevices活动应该被称为我该怎么做呢?

+0

创建 –

回答

1

这样称呼它是AvailableDevices.searchDevices()从提供的代码,它看起来像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(); 
    } 

我希望这有助于

+0

谢谢你这么多先生的静态方法.it可以解释代码意图intent = new Intent(this,AvailableDevices.class); intent.putExtra(“CallSearchDevices”,true); startActivity(intent); 并且在onCreate方法结束时的AvailableDevices中, Intent intent = getIntent(); 布尔标志= intent.getBooleanExtra(“CallSearchDevices”,false);如果(标志){ searchDevices(); } – treesa

+0

再次来自searchDevice()我想访问主要活动中的一个线程我该怎么做 – treesa

2

您可以使用广播消息。点击按钮后发送广播。并在另一项活动中注册将处理brodcast的BroadcastReceiver。所以只要你有想要的消息,然后在第二个活动中调用该方法。

0

你应该声明你的方法static,因此您可以从您的其他活动