我正在尝试利用共享偏好来提取处理程序中的首选项。 我在onCreate()期间调用sharedpreference但尝试在处理程序案例中使用首选项。
偏好是: 1布尔值 1个漂浮
它们都用作处理程序中if语句的检查。 代码基于bluetoothchat示例。
package com.application.feveralert;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Men>u;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.content.SharedPreferences;
/*** This is the main Activity that displays the current chat session.*/
public class BluetoothChat extends Activity {
// Debuggng
private static final String TAG = "BluetoothChat";
private static final boolean D = true;
// Message types sent from the BluetoothChatService Handler
public static final int MESSAGE_STATE_CHANGE = 1;
public static final int MESSAGE_READ = 2;
public static final int MESSAGE_WRITE = 3;
public static final int MESSAGE_DEVICE_NAME = 4;
public static final int MESSAGE_TOAST = 5;
// Key names received from the BluetoothChatService Handler
public static final String DEVICE_NAME = "device_name";
public static final String TOAST = "toast";
// Intent request codes
private static final int REQUEST_CONNECT_DEVICE_INSECURE = 2;
private static final int REQUEST_ENABLE_BT = 3;
// Layout Views
private TextView mTitle;
// Name of the connected device
private String mConnectedDeviceName = null;
// Array adapter for the conversation thread
private ArrayAdapter<String> mConversationArrayAdapter;
// Local Bluetooth adapter
private BluetoothAdapter mBluetoothAdapter = null;
// Member object for the chat services
private BluetoothChatService mChatService = null;
TextView textOut;
//protected boolean prefalert;
//protected Float preftemp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(D) Log.e(TAG, "+++ ON CREATE +++");
// Settings
PreferenceManager.setDefaultValues(this, R.xml.preference, false);
addPreferencesFromResource(R.xml.preference);
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
Float preftemp = getFloat("preftemp",83.0);
Boolean prefalert = getBoolean("prefAlert",true);
// Set up the window layout
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
// Set up the custom title
mTitle = (TextView) findViewById(R.id.title_left_text);
mTitle.setText(R.string.app_name);
mTitle = (TextView) findViewById(R.id.title_right_text);
textOut = (TextView) findViewById(R.id.TempDisplay);
// Get local Bluetooth adapter
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// If the adapter is null, then Bluetooth is not supported
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is Not Available", Toast.LENGTH_LONG).show();
finish();
return;
}
// Initialize Sound
SoundManager.getInstance();
SoundManager.initSounds(this);
SoundManager.loadSounds();
}
private float getFloat(String string, double e) {
return 0;
// TODO Auto-generated method stub
}
private boolean getBoolean(String string, boolean b) {
return b;
// TODO Auto-generated method stub
}
private void addPreferencesFromResource(int preference) {
// TODO Auto-generated method stub
}
和
// The Handler that gets information back from the BluetoothChatService
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_STATE_CHANGE:
if(D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
switch (msg.arg1) {
case BluetoothChatService.STATE_CONNECTED:
mTitle.setText(R.string.title_connected_to);
mTitle.append(mConnectedDeviceName);
mConversationArrayAdapter.clear();
break;
case BluetoothChatService.STATE_CONNECTING:
mTitle.setText(R.string.title_connecting);
break;
case BluetoothChatService.STATE_LISTEN:
case BluetoothChatService.STATE_NONE:
mTitle.setText(R.string.title_not_connected);
break;
}
break;
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
//mConversationArrayAdapter.add(readMessage + " F");
textOut.setText(readMessage + " F");
Float out = new Float(readMessage);
// Check for Fever
if (prefalert = true){
if (out >= 80.3){
SoundManager.playSound(1, 1);
Toast.makeText(getApplicationContext() ,"Fever Alert", Toast.LENGTH_LONG).show();
}
}
break;
谢谢。