来自不同 activity 的调用函数

Call function from different activity

我正在开发一个让用户连接到 BLE 设备的应用程序,然后在从 BLE 外围设备读取 20 个值后,它移动到一个新的 activity/screen,我在其中使用 20 个值制作图像我读到的价值观。然后我希望能够读取一组新的 20 个值,但不离开显示图像的屏幕。我怎样才能做到这一点?

在activity我看BLE特性的地方,我有这个功能:

public class BluetoothLeService extends Service {
    private final static String TAG = BluetoothLeService.class.getSimpleName();
    ....
    public int read_counter = 0;
    public int measurement_arr[] = new int[20];


    // Implements callback methods for GATT events that the app cares about.  For example,
    // connection change and services discovered.
    private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {

    ....
        @Override
        public void onCharacteristicRead(BluetoothGatt gatt,
                                         BluetoothGattCharacteristic characteristic,
                                         int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) { // IF THE READ OPERATION WAS SUCCESSFUL.
                broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
            }
        }

     public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }
        mBluetoothGatt.readCharacteristic(characteristic);
    }
    ....
    private void broadcastUpdate(final String action,
                                 final BluetoothGattCharacteristic characteristic) {
            final Intent intent = new Intent(action);

            final byte[] data = characteristic.getValue(); // data is presented as a byte array over BLE characteristics.

            measurement_arr[read_counter] = (int) data[0]; // Read data byte.

            // Use hex formatting.
            if (data != null && data.length > 0) {
                final StringBuilder stringBuilder = new StringBuilder(data.length);
                for(byte byteChar : data)
                    stringBuilder.append(String.format("%02X ", byteChar));
                intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString()); // THIS IS WHERE DATA IS BEING READ.
            }
            read_counter += 1; // after value read, increment the count.
            if (read_counter < 20){ // Receive 20 packets of data.
                Log.d("MEASUREMENT", "Reading new characteristic");
                readCharacteristic(characteristic);
            }
            else {
                Log.d("Finished BLE read", "Moving to next activity.");
                read_counter = 0;
                // Go to next activity where we show image. 
                Intent show_image = new Intent(this, Reconstruction.class);
                show_image.putExtra("myArr", measurement_arr); // Go to acivity where we reconstruct image.
                show_image.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(show_image);
            }
        sendBroadcast(intent);
    }

我的第二个 activity,它使用 Canvas 显示图像,然后具有以下代码。

public class Reconstruction extends AppCompatActivity {
    public int data[] = new int[20];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyView(this));
        // Get Bundle object that contain the array
        Bundle extras = getIntent().getExtras();
        // Extract the array from the Bundle object
        data = extras.getIntArray("myArr");
        // Output the array
        for(int item:data){
            Log.i("2nd_ACTIVITY", String.valueOf(item));
        }
    }

    public class MyView extends View
    {
        Paint paint = null;
        public MyView(Context context)
        {
            super(context);
            paint = new Paint();
        }

        @Override
        protected void onDraw(Canvas canvas)
        {
            ... make image

            ...After making the image, how do I go back to the readCharacteristic function from the previous activity, but without changing what the display is showing?
        }
    }
}

我已经在网上阅读了一些关于将我的第二个 activity 的上下文传递给第一个的内容,但我不太明白这一点,而且我也不太清楚上下文是如何工作的。

不要将 link 用于另一个 activity 中的 activity。它可能导致内存泄漏。

关于您的问题,您可以使用广播接收器在 Activity 之间进行通信,或者使用 Service 将与您的 BLE 设备进行通信

Broadcast Receiver的简单用法:

class YourActivity extends Activity {
    private BroadcastReceiver mMyReceiver;

    protected void onCreate(Bundle data) {
        ...
        mMyReceiver = new MyReceiver();
    }

    public void onStart() {
        registerReceiver(mMyReceiver, new IntentFilter("your action"));
    }

    public void onStop() {
        unregisterReceiver(mMyReceiver);
    }

    public void onDestroy() {
        mMyReceiver = null;
    }

    // Inner class has a link to YourActivity instance
    private class MyReceiver extends BroadcastReceiver {
        public void onReceive(Intent intent) {
            // procces messages here
        }
    }
}

// Calling:
context.sendBroadcast(new Intent("your action"));