android - 从不同的 Activity 中调用函数

标签 android android-activity bluetooth-lowenergy android-context

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

在我读BLE特性的 Activity 中,我有这个功能:

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 的上下文传递给第一个 Activity 的内容,但我不太了解这一点,而且我也不太确定上下文是如何工作的。

最佳答案

不要在另一个 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"));

关于android - 从不同的 Activity 中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49546019/

相关文章:

java - 打造节俭服务

Android:如何将本地javascript文件加载到WebView

android - Android : Application or Service for asynchronous updates? 中的 MVC

Android 流行动画

Android 蓝牙 accept()/connect() 与已经配对的设备

android - 在 Activity 之间保持蓝牙连接?

javascript - Android 上的手机唤醒后 HTML5 GPS 地理定位失败

android - 从 SherlockFragment 启动 ACTION_VIEW Activity 会导致 NullPointerException

java - 将 ArrayList 写入/读取文件的奇怪问题

ios - BLE 广告数据中有什么?