java - 在 Activity 之间传递 "preference variable",在原始 Activity 中设置值

标签 java android

我试图允许用户以文本形式输入其模块的蓝牙地址。

在 Activity 1 (MainActivity.java) 中按 Menu 将显示一个选项菜单。选择设置切换到 Activity 2 (SetPreferences.java)。在文本框中输入 MAC 地址并按应用 将恢复 Activity 1,地址值不会发生变化。

任何帮助,我们将不胜感激!

开始代码:

MainActivity.java:

package com.showstopperled.defiance;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Random;
import java.util.UUID;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.showstopperled.defiance.ColorPickerDialog.OnColorChangedListener;

public class MainActivity extends Activity implements OnColorChangedListener {

    public static final String PREFS_NAME = "DefiancePrefsFile";
    static String Packet = null;
    private BluetoothAdapter mBluetoothAdapter = null;
    private BluetoothSocket btSocket = null;
    private static OutputStream outStream = null;

    SeekBar seekbarUnicorns, seekbarChan, seekbarRed, seekbarGreen,
            seekbarBlue, seekbarBand, seekbarSens;
    TextView valueUnicorns, valueChan, valueRed, valueGreen, valueBlue,
            valueBand, valueSens, valueSave;
    static int Channel = 0;
    static int Rset = 255;
    static int Gset = 0;
    static int Bset = 0;
    static int Mode = 0;
    static int Band = 0;
    static int Sens = 24;
    static int Save = 0;
    static int Unicorns = 0;

    String audB = "63Hz";

    private static final UUID MY_UUID = UUID
            .fromString("00001101-0000-1000-8000-00805F9B34FB");

    // hardcoded MAC address
    static String address = null;
    private BufferedReader buf;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

        valueChan = (TextView) findViewById(R.id.textViewChan);
        seekbarChan = (SeekBar) findViewById(R.id.seekBarChan);
        valueRed = (TextView) findViewById(R.id.textViewRed);
        seekbarRed = (SeekBar) findViewById(R.id.seekBarRed);
        valueGreen = (TextView) findViewById(R.id.textViewGreen);
        seekbarGreen = (SeekBar) findViewById(R.id.seekBarGreen);
        valueBlue = (TextView) findViewById(R.id.textViewBlue);
        seekbarBlue = (SeekBar) findViewById(R.id.seekBarBlue);
        valueBand = (TextView) findViewById(R.id.textViewBand);
        seekbarBand = (SeekBar) findViewById(R.id.seekBarBand);
        valueSens = (TextView) findViewById(R.id.textViewSens);
        seekbarSens = (SeekBar) findViewById(R.id.seekBarSens);
        valueUnicorns = (TextView) findViewById(R.id.textViewUnicorns);
        seekbarUnicorns = (SeekBar) findViewById(R.id.seekBarUnicorns);

        Spinner s = (Spinner) findViewById(R.id.spinnerMode);
        s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> adapter, View v, int i,
                    long lng) {
                Mode = i;
                packet(Channel, Rset, Gset, Bset, Mode, Band, Sens, Unicorns,
                        Save);
            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // do something else
            }
        });

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
            Toast.makeText(this, "Bluetooth is not available.",
                    Toast.LENGTH_LONG).show();
            finish();
            return;
        }

        if (!mBluetoothAdapter.isEnabled()) {
            Toast.makeText(this,
                    "Please enable your BT and re-run this program.",
                    Toast.LENGTH_LONG).show();
            finish();
            return;
        }

        int sbar_chan_position = settings.getInt("seekbar_chan_pref", 0);
        seekbarChan.setProgress(sbar_chan_position);
        valueChan.setText("Channel: " + sbar_chan_position);
        Channel = sbar_chan_position;

        int sbar_red_position = settings.getInt("seekbar_red_pref", 0);
        seekbarRed.setProgress(sbar_red_position);
        valueRed.setText("Red: " + sbar_red_position);
        Rset = sbar_red_position;

        int sbar_green_position = settings.getInt("seekbar_green_pref", 0);
        seekbarGreen.setProgress(sbar_green_position);
        valueGreen.setText("Green: " + sbar_green_position);
        Gset = sbar_green_position;

        int sbar_blue_position = settings.getInt("seekbar_blue_pref", 0);
        seekbarBlue.setProgress(sbar_blue_position);
        valueBlue.setText("Blue: " + sbar_blue_position);
        Bset = sbar_blue_position;

        int sbar_band_position = settings.getInt("seekbar_band_pref", 0);
        seekbarBand.setProgress(sbar_band_position);
        valueBand.setText("Band: " + sbar_band_position);
        Band = sbar_band_position;

        int sbar_sens_position = settings.getInt("seekbar_sens_pref", 0);
        seekbarSens.setProgress(sbar_sens_position);
        valueSens.setText("Sensitivity: " + sbar_sens_position);
        Sens = sbar_sens_position;

        int sbar_unicorns_position = settings
                .getInt("seekbar_unicorns_pref", 0);
        seekbarUnicorns.setProgress(sbar_unicorns_position);
        valueUnicorns.setText("Unicorns!: " + sbar_unicorns_position);
        Unicorns = sbar_unicorns_position;

        seekbarChan.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                valueChan.setText("Channel: " + (progress + 1));
                Channel = progress;
                packet(Channel, Rset, Gset, Bset, Mode, Band, Sens, Unicorns,
                        Save);
            }

            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }

            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }
        });

        seekbarRed.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                valueRed.setText("Red: " + progress);

                Rset = progress;
                packet(Channel, Rset, Gset, Bset, Mode, Band, Sens, Unicorns,
                        Save);
            }

            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }

            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }
        });

        seekbarGreen.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                valueGreen.setText("Green: " + progress);

                Gset = progress;
                packet(Channel, Rset, Gset, Bset, Mode, Band, Sens, Unicorns,
                        Save);

            }

            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }

            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }
        });

        seekbarBlue.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                valueBlue.setText("Blue: " + progress);

                Bset = progress;
                packet(Channel, Rset, Gset, Bset, Mode, Band, Sens, Unicorns,
                        Save);

            }

            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }

            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }
        });

        seekbarBand.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                switch (progress) {
                case 0:
                    audB = "63Hz";
                    break;
                case 1:
                    audB = "160Hz";
                    break;
                case 2:
                    audB = "400Hz";
                    break;
                case 3:
                    audB = "1kHz";
                    break;
                case 4:
                    audB = "2.5kHz";
                    break;
                case 5:
                    audB = "6.25kHz";
                    break;
                case 6:
                    audB = "16kHz";
                    break;
                }
                valueBand.setText("Band: " + audB);

                Band = progress;
                packet(Channel, Rset, Gset, Bset, Mode, Band, Sens, Unicorns,
                        Save);

            }

            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }

            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }
        });

        seekbarSens.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                valueSens.setText("Sensitivity: " + (progress + 1));

                Sens = progress;
                packet(Channel, Rset, Gset, Bset, Mode, Band, Sens, Unicorns,
                        Save);

            }

            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }

            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }
        });

        seekbarUnicorns
                .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

                    public void onProgressChanged(SeekBar seekBar,
                            int progress, boolean fromUser) {
                        valueUnicorns.setText("Unicorns!: " + progress);

                        Unicorns = progress;
                        packet(Channel, Rset, Gset, Bset, Mode, Band, Sens,
                                Unicorns, Save);

                    }

                    public void onStartTrackingTouch(SeekBar seekBar) {
                        // TODO Auto-generated method stub
                    }

                    public void onStopTrackingTouch(SeekBar seekBar) {
                        // TODO Auto-generated method stub
                    }
                });

    }

    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, 0, 0, "Settings");
        menu.add(0, 1, 0, "About");
        menu.add(0, 2, 0, "Help");
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch (item.getItemId()) {
        case 0:
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, SetPreferences.class);
            startActivityForResult(intent, 0);
            address = intent.getExtras().getString("address");
            SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putString("address", address);
            editor.commit();
            return true;
        case 1:
            // launch ABOUT here
            break;
        case 2:
            // Launch here
            break;
        default:
            // Launch SETTINGS here
            break;
        }
        return false;
    }

    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);
    }

    public void colorChanged(int color) {

        int sbar_red_position = Color.red(color);
        int sbar_green_position = Color.green(color);
        int sbar_blue_position = Color.blue(color);

        seekbarRed.setProgress(sbar_red_position);
        valueRed.setText("Red: " + sbar_red_position);
        Rset = sbar_red_position;

        seekbarGreen.setProgress(sbar_green_position);
        valueGreen.setText("Green: " + sbar_green_position);
        Gset = sbar_green_position;

        seekbarBlue.setProgress(sbar_blue_position);
        valueBlue.setText("Blue: " + sbar_blue_position);
        Bset = sbar_blue_position;
        packet(Channel, Rset, Gset, Bset, Mode, Band, Sens, Unicorns, Save);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        try {
            File f = new File(Environment.getExternalStorageDirectory()
                    + "/Defiance.txt");
            FileInputStream fileIS = new FileInputStream(f);
            buf = new BufferedReader(new InputStreamReader(fileIS));
            String readString = new String();
            // just reading each line and pass it on the debugger
            while ((readString = buf.readLine()) != null) {
                address = readString;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // When this returns, it will 'know' about the server,
        // via it's MAC address.
        BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);

        try {
            btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) {
        }

        mBluetoothAdapter.cancelDiscovery();

        try {
            btSocket.connect();
        } catch (IOException e) {
            try {
                btSocket.close();
            } catch (IOException e2) {
            }
        }

        // Create a data stream so we can talk to server.
        try {
            outStream = btSocket.getOutputStream();
        } catch (IOException e) {
        }
    }

    public void onClickSave1(View v) throws IOException {
        Save = 100;
        packet(Channel, Rset, Gset, Bset, Mode, Band, Sens, Unicorns, Save);
        Save = 0;
    }

    public void onClickSave2(View v) throws IOException {
        Save = 200;
        packet(Channel, Rset, Gset, Bset, Mode, Band, Sens, Unicorns, Save);
        Save = 0;
    }

    public void onClickSave3(View v) throws IOException {
        Save = 300;
        packet(Channel, Rset, Gset, Bset, Mode, Band, Sens, Unicorns, Save);
        Save = 0;
    }

    public void onClickSave4(View v) throws IOException {
        Save = 400;
        packet(Channel, Rset, Gset, Bset, Mode, Band, Sens, Unicorns, Save);
        Save = 0;
    }

    public void onClickSave5(View v) throws IOException {
        Save = 500;
        packet(Channel, Rset, Gset, Bset, Mode, Band, Sens, Unicorns, Save);
        Save = 0;
    }

    public void onClickLoad1(View v) throws IOException {
        Save = 10;
        packet(Channel, Rset, Gset, Bset, Mode, Band, Sens, Unicorns, Save);
        Save = 0;
    }

    public void onClickLoad2(View v) throws IOException {
        Save = 20;
        packet(Channel, Rset, Gset, Bset, Mode, Band, Sens, Unicorns, Save);
        Save = 0;
    }

    public void onClickLoad3(View v) throws IOException {
        Save = 30;
        packet(Channel, Rset, Gset, Bset, Mode, Band, Sens, Unicorns, Save);
        Save = 0;
    }

    public void onClickLoad4(View v) throws IOException {
        Save = 40;
        packet(Channel, Rset, Gset, Bset, Mode, Band, Sens, Unicorns, Save);
        Save = 0;
    }

    public void onClickLoad5(View v) throws IOException {
        Save = 50;
        packet(Channel, Rset, Gset, Bset, Mode, Band, Sens, Unicorns, Save);
        Save = 0;
    }

    public void onClickApply(View v) throws IOException {

        packet(Channel, Rset, Gset, Bset, Mode, Band, Sens, Unicorns, Save);
    }

    @Override
    public void onPause() {
        super.onPause();

        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt("seekbar_red_pref", Rset);
        editor.putInt("seekbar_green_pref", Gset);
        editor.putInt("seekbar_blue_pref", Bset);
        editor.putString("address", address);
        editor.commit();

        if (outStream != null) {
            try {
                outStream.flush();
            } catch (IOException e) {
            }
        }

        try {
            btSocket.close();
        } catch (IOException e2) {
        }
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // Stop the Bluetooth chat services

    }

    public void onClickColorPicker(View v) {
        new ColorPickerDialog(this, this, Color.rgb(Rset, Gset, Bset)).show();
        packet(Channel, Rset, Gset, Bset, Mode, Band, Sens, Unicorns, Save);
    }

    public void onClickRandomColor(View v) {
        Random rnd = new Random();
        int min = 0, max = 255;
        int randomRed = rnd.nextInt(max - min + 1) + min;
        int randomGreen = rnd.nextInt(max - min + 1) + min;
        int randomBlue = rnd.nextInt(max - min + 1) + min;
        Rset = randomRed;
        Gset = randomGreen;
        Bset = randomBlue;
        new ColorPickerDialog(this, this, Color.rgb(Rset, Gset, Bset)).show();
        packet(Channel, Rset, Gset, Bset, Mode, Band, Sens, Unicorns, Save);
    }

    public static void packet(int Channel, int Rset, int Gset, int Bset,
            int Mode, int Band, int Sens, int Unicorns, int Save) {
        int Rfin;
        int Gfin;
        int Bfin;
        int Sfin;
        int Ufin;
        Rfin = Rset * 16;
        Rfin = 4095 - Rfin;
        Gfin = Gset * 16;
        Gfin = 4095 - Gfin;
        Bfin = Bset * 16;
        Bfin = 4095 - Bfin;
        Sfin = Sens * 32;
        Ufin = Unicorns * 10;
        Ufin = 1000 - Ufin;
        Packet = "<HMG:" + Channel + ":" + Rfin + ":" + Gfin + ":" + Bfin + ":"
                + Mode + ":" + Band + ":" + Sfin + ":" + Ufin + ":" + Save
                + ">";
        byte[] msgBuffer;
        msgBuffer = Packet.getBytes();
        try {
            outStream.write(msgBuffer);
        } catch (IOException e) {
        }
    }
}

SetPreferences.java:

package com.showstopperled.defiance;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class SetPreferences extends Activity {

    EditText editMac;
    TextView valueMac;
    String address;
    public static final String PREFS_NAME = "DefiancePrefsFile";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.set_preferences);
        EditText MacAddress = (EditText) findViewById(R.id.editMac);
        MacAddress = (EditText) findViewById(R.id.editMac);
        address = MacAddress.getText().toString();
        valueMac = (TextView) findViewById(R.id.textViewMac);
    }

    public void onClickMac(View v) throws IOException {
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("address", address);
        editor.commit();
        address = settings.getString("address", address);
        valueMac.setText("Mac Address: " + address);
        Intent i = new Intent(this, MainActivity.class);
        i.putExtra("address", address);
        startActivity(i);
    }
}

最佳答案

看起来您正在执行 startActivityForResult,然后在之后获取 Intent 的值。

这行不通,原因如下:

  1. startActivityForResult 在您调用它的方法(或更具体地说,UI 线程循环程序中运行的事件)返回之前实际上不会触发。

  2. 要获取 Activity 的结果,您应该使用 onActivityResult 。这个 Intent 将具有您想要的值(value)。

你所做的就是告诉 Looper 在方法返回时启动 Activity,然后你会从自己的 Intent 中获得额外的内容,然后返回并让 Activity 启动。当它返回时,您不会检查其结果。

关于java - 在 Activity 之间传递 "preference variable",在原始 Activity 中设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15167580/

相关文章:

java - Mockito:模拟类总是调用真实方法

java - 可变参数和歧义的概念?

android - 我可以检查默认的音频编码器吗?

java - ListView 中 url 中的图像随机图像错误

android - 从 Intent 启动 Instrumentation 测试

java - indexOf() - 列表 - Android - 无法正常工作

JAVA - Selenium WebDriver - 断言和等待

java - 数组中 "string"的索引始终返回 -1,使用 "Arrays.asList"

android - TimePickerDialog 和 AM 或 PM

Android 2.3.3 ListView停止垂直滚动