0

就我而言,如果strReceived从蓝牙匹配最终声明的字符串,我在启动新意图或调用方法来启动此意图时遇到问题str。如何解决“无法从 Activity 类型对非静态方法 getIntent() 进行静态引用”。

我注意到我不能strReceived在处理程序之外引用这个全局值:

public class Standby extends ListActivity {

public final static String UUID = "00001101-0000-1000-8000-00805F9B34FB";
public final static String str = "\fDETECT1\n\r";
public static String strRead;
public static String strReceived;

BluetoothAdapter bluetoothAdapter;
BroadcastReceiver discoverDevicesReceiver;
BroadcastReceiver discoveryFinishedReceiver;

//---store all the discovered devices---
ArrayList<BluetoothDevice> discoveredDevices;
ArrayList<String> discoveredDevicesNames;

public static TextView txtData;

//---thread for connecting to the client socket---
ConnectToServerThread connectToServerThread;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.standby);

    //---init the ArrayList objects and bluetooth adapter---
    discoveredDevices = new ArrayList<BluetoothDevice>();
    discoveredDevicesNames = new ArrayList<String>();

    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    //---for displaying the messages received---
    txtData = (TextView) findViewById(R.id.txtData);
}

//Method to make yourself discoverable
public void MakeDiscoverable(View view) 
{ 
    Intent i = new Intent(
        BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    i.putExtra(
        BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); //300 seconds
    startActivity(i);
}

//Method to discover other bluetooth devices
private void DiscoveringDevices() { 
    if (discoverDevicesReceiver == null) {
        discoverDevicesReceiver = new BroadcastReceiver() { 
            //Fired when a new device is discovered
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();

                //When a device is discovered---
                if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                    //---get the BluetoothDevice object from 
                    // the Intent---
                    BluetoothDevice device = 
                        intent.getParcelableExtra(
                            BluetoothDevice.EXTRA_DEVICE);

                    //---add the name and address to an array 
                    // adapter to show in a ListView---
                    //---only add if the device is not already 
                    // in the list---
                    if (!discoveredDevices.contains(device)) {
                        //---add the device---
                        discoveredDevices.add(device);

                        //---add the name of the device; used for 
                        // ListView---
                        discoveredDevicesNames.add(device.getName());

                        //---display the items in the ListView---
                        setListAdapter(new 
                                ArrayAdapter<String>(getBaseContext(),
                                android.R.layout.simple_list_item_1, 
                                discoveredDevicesNames));
                    } 
                }
            }
        };
    }

    if (discoveryFinishedReceiver==null) {
        discoveryFinishedReceiver = new BroadcastReceiver() {
            //Fired when the discovery is done
            public void onReceive(Context context, Intent intent) {
                //---enable the listview when discovery is over; 
                // about 12 seconds---
                getListView().setEnabled(true); 
                Toast.makeText(getBaseContext(), 
                        "Discovery completed. Please select BlueBee.", 
                        Toast.LENGTH_LONG).show();
                unregisterReceiver(discoveryFinishedReceiver);
            } 
        };
    }

    //---register the broadcast receivers---
    IntentFilter filter1 = new
        IntentFilter(BluetoothDevice.ACTION_FOUND);
    IntentFilter filter2 = new
        IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

    registerReceiver(discoverDevicesReceiver, filter1);
    registerReceiver(discoveryFinishedReceiver, filter2);

    //---disable the listview when discover is in progress---
    getListView().setEnabled(false);
    Toast.makeText(getBaseContext(), 
            "Discovery in progress...please wait...", 
            Toast.LENGTH_LONG).show();
    bluetoothAdapter.startDiscovery();
}


//Calls the method to discover other bluetooth devices
public void DiscoverDevices(View view) 
{
    //---discover other devices---
    DiscoveringDevices();   
}

static Handler UIupdater = new Handler(){
public void handleMessage(Message msg) { 
    int numOfBytesReceived = msg.arg1;
    byte[] buffer = (byte[]) msg.obj;
    //---convert the entire byte array to string---
    String strReceived = new String(buffer);
    //---extract only the actual string received---
    strReceived = strReceived.substring(
        0, numOfBytesReceived);

    //---display the text from string str on the TextView in one line only---
    //txtData.setText(strReceived);

    //Compare string. Method will return 0 if both are identical
    if (strReceived.compareTo(str)==0){
        txtData.setText("FALL DETECTED!!");
        Intent i = new Intent ("com.example.NEXT"); 

        //Get the data using bundle
        //Add the set of extended data to the intent and start it
        Bundle b = getIntent().getExtras(); 
        i.putExtras(b);
        startActivity(i);
        //readySMS();
    }
}
};

//Fired when method is called
public void readySMS() {

    // TODO Auto-generated method stub
        Intent i = new Intent ("com.example.NEXT"); 

        //Get the data using bundle
        //Add the set of extended data to the intent and start it
        Bundle b = getIntent().getExtras(); 
        i.putExtras(b);
        startActivity(i);
}

public void onPause() {
    super.onPause();
    //---cancel discovery of other bluetooth devices
    bluetoothAdapter.cancelDiscovery();

    //---unregister the broadcast receiver for 
    // discovering devices--- 
    if (discoverDevicesReceiver != null) {
        try {
            unregisterReceiver(discoverDevicesReceiver); 
        } catch(Exception e) {
        }
    }

    //---if you are currently connected to someone...---
    if (connectToServerThread!=null) { 
        try {
            //---close the connection---
            connectToServerThread.bluetoothSocket.close();

            //Calls the method to get ready to SMS
        } catch (IOException e) {
            Log.d("MainActivity", e.getLocalizedMessage());
        }
    }
}

//Fired when a client is tapped in the ListView---
public void onListItemClick(ListView parent, View v, 
int position, long id) {
    //---if you are already talking to someone...---
    if (connectToServerThread!=null) {
        try {
            //---close the connection first---
            connectToServerThread.bluetoothSocket.close();
        } catch (IOException e) {
            Log.d("MainActivity", e.getLocalizedMessage());
            //Toast.makeText(this, "Connect failed", Toast.LENGTH_SHORT).show();
        }
    }

    //---connect to the selected Bluetooth device---
    BluetoothDevice deviceSelected =
        discoveredDevices.get(position); 
    connectToServerThread = new 
        ConnectToServerThread(deviceSelected, bluetoothAdapter);
    connectToServerThread.start();

    /*  
    Thread cancel = new Thread(){   
            public void run(){
                    //this must be the same as the action name to be performed in manifest.xml file
                    //if (txtData.getText().toString().equalsIgnoreCase(str)){

                    //if (strReceived!=null){
                    //  strRead = strReceived;
                    //  if (strRead.compareTo(str)==0){
                            try{
                                sleep(3000);
                                Intent i = new Intent ("com.nick.falldetection.CANCEL");    

                                //Get the data using bundle
                                //Add the set of extended data to the intent and start it
                                Bundle b = getIntent().getExtras(); 
                                //String owner = b.getString("abc");
                                //String message = b.getString("longMessage");
                                //String phoneNo = b.getString("phoneNo");    

                                i.putExtras(b);
                                startActivity(i);
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                            finally{
                                //finish();
                            }
                        }
                    //}
    };

                    {
    cancel.start();

}
*/

};
}
4

2 回答 2

0

尝试这个。

 Standby me;
 protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
me = this;
}
static Handler UIupdater = new Handler()
  {
 public void handleMessage(Message msg) 
  { 

strReceived = me.strReceived.substring(0, numOfBytesReceived);

抱歉耽搁了。希望这会帮助你。

于 2013-05-25T14:24:38.063 回答
0

您可以通过YourActivity mActivity在您的字段中设置字段Handler并将其设置为thisActivity 的onCreate. 这样你就可以YourActivity从你的 Handler 中获取任何东西mHandler.mActivity.yourFieldOrMethod

你喜欢这样

class MyActivity {

static class MyHandler extends Handler() {
   MyActivity mActivity;
   void handleMessage() { 
        mActivity.someMethod();
   }
};

private MyHandler uiHandler = new MyActivity();

private void someMethod() { /* ... */ }

void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    uiHandler.mActivity = this;
}

}

于 2013-05-20T06:36:48.690 回答