-1
내에서 onCreate 함수 내 onclicklistner있는 버튼을 눌러 작동하지 않습니다 :공유 환경 설정은 내가 아래의 공유 환경 설정 값을 취소 할 수있는 버튼을 구현하기 위해 노력하고
btn_clear = (Button) findViewById(R.id.bt_clear);
btn_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (pref.contains(ARRAY_KEY)) {
prefsEditor.clear();
boolean test = prefsEditor.commit();
Log.d("click","bool---> "+ test);
}
}
});
내가 디버거를 실행하고, 모든 코드를 단계별로 실행하고 prefsEditor.commit의 반환 값은 true입니다. 그러나 저장된 내 값이 지워지지 않았습니다.
public void get() {
prefsEditor.clear();
prefsEditor.commit();
//List<String> save_plot_list = new ArrayList<String>();
Gson gson = new Gson();
if (pref.contains(ARRAY_KEY)) {
X_axies = pref.getInt(INT_KEY, 0);
String json = pref.getString(ARRAY_KEY, "");
List<String> my_saved_plots = gson.fromJson(json, List.class);
save_plot_list = gson.fromJson(json, List.class);
for (String temp : save_plot_list) {
Log.d("open", temp + " - " + "x=" + X_axies);
}
displaySavedData();
}
}
이이 일 :
나는 명확한를 배치하고 내 GET 같은() 함수 쇼 아래에, 다른 장소에서 기능을 저지했습니다. 아무도 왜 값을 지우는 데 아무 것도하지 않는다는 것을 압니까? 다음은 버튼을 구현하려고하는 파일입니다.public class DeviceControlActivity extends Activity {
private final static String TAG = DeviceControlActivity.class.getSimpleName();
public static final String EXTRAS_DEVICE_NAME = "DEVICE_NAME";
public static final String EXTRAS_DEVICE_ADDRESS = "DEVICE_ADDRESS";
private Button btnRead;
private TextView mConnectionState;
private TextView mDataField;
private TextView ch1DataField;
private TextView ch2DataField;
private String mDeviceName;
private String mDeviceAddress;
private ExpandableListView mGattServicesList;
private BluetoothLeService mBluetoothLeService;
private ArrayList<ArrayList<BluetoothGattCharacteristic>> mGattCharacteristics =
new ArrayList<ArrayList<BluetoothGattCharacteristic>>();
private boolean mConnected = false;
private BluetoothGattCharacteristic mNotifyCharacteristic;
private final String LIST_NAME = "NAME";
private final String LIST_UUID = "UUID";
//graph variables
LineGraphSeries<DataPoint> series0;
private int X_axies = 0;
// Code to manage Service lifecycle.
//save the application state varibales
public SharedPreferences pref;
List<String> save_plot_list = new ArrayList<String>();
int saved_x = 0;
public SharedPreferences.Editor prefsEditor;
public static final String ARRAY_KEY = "store_array_key1";
public static final String INT_KEY = "store_x_axies_key";
Button btn_clear;
private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
if (!mBluetoothLeService.initialize()) {
Log.e(TAG, "Unable to initialize Bluetooth");
finish();
}
// Automatically connects to the device upon successful start-up initialization.
mBluetoothLeService.connect(mDeviceAddress);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mBluetoothLeService = null;
}
};
// Handles various events fired by the Service.
// ACTION_GATT_CONNECTED: connected to a GATT server.
// ACTION_GATT_DISCONNECTED: disconnected from a GATT server.
// ACTION_GATT_SERVICES_DISCOVERED: discovered GATT services.
// ACTION_DATA_AVAILABLE: received data from the device. This can be a result of read
// or notification operations.
private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
mConnected = true;
updateConnectionState(R.string.connected);
invalidateOptionsMenu();
} else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
mConnected = false;
updateConnectionState(R.string.disconnected);
invalidateOptionsMenu();
clearUI();
} //else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
// mBluetoothLeService.readCustomCharacteristic(SampleGattAttributes.CH0_CHARACTERISTIC);
// SystemClock.sleep(500);
// mBluetoothLeService.readCustomCharacteristic(SampleGattAttributes.CH1_CHARACTERISTIC);
else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
displayData(mDataField, intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
displayData(ch1DataField, intent.getStringExtra(BluetoothLeService.CH1_DATA));
displayData(ch2DataField, intent.getStringExtra(BluetoothLeService.CH2_DATA));
}
if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
}
}
};
// If a given GATT characteristic is selected, check for supported features. This sample
// demonstrates 'Read' and 'Notify' features. See
// http://d.android.com/reference/android/bluetooth/BluetoothGatt.html for the complete
// list of supported characteristic features.
// private final ExpandableListView.OnChildClickListener servicesListClickListner =
// new ExpandableListView.OnChildClickListener() {
// @Override
// public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
// int childPosition, long id) {
// if (mGattCharacteristics != null) {
// final BluetoothGattCharacteristic characteristic =
// mGattCharacteristics.get(groupPosition).get(childPosition);
// final int charaProp = characteristic.getProperties();
// if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
// // If there is an active notification on a characteristic, clear
// // it first so it doesn't update the data field on the user interface.
// if (mNotifyCharacteristic != null) {
// mBluetoothLeService.setCharacteristicNotification(
// mNotifyCharacteristic, false);
// mNotifyCharacteristic = null;
// }
// mBluetoothLeService.readCharacteristic(characteristic);
// }
// if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
// mNotifyCharacteristic = characteristic;
// mBluetoothLeService.setCharacteristicNotification(
// characteristic, true);
// }
// return true;
// }
// return false;
// }
// };
private void clearUI() {
//mGattServicesList.setAdapter((SimpleExpandableListAdapter) null);
mDataField.setText(R.string.no_data);
ch1DataField.setText(R.string.no_data);
ch2DataField.setText(R.string.no_data);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.button_control);
btn_clear = (Button) findViewById(R.id.bt_clear);
btn_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (pref.contains(ARRAY_KEY)) {
prefsEditor.clear();
boolean test = prefsEditor.commit();
Log.d("onclick","bool---> "+ test);
}
}
});
pref = getSharedPreferences("MyPrefs",Context.MODE_PRIVATE);
prefsEditor = pref.edit();
double y, x;
GraphView graph = (GraphView) findViewById(R.id.graph);
series0 = new LineGraphSeries<DataPoint>();
graph.addSeries(series0);
setGraphUI(graph);
//retrieve data
get();
final Intent intent = getIntent();
mDeviceName = intent.getStringExtra(EXTRAS_DEVICE_NAME);
mDeviceAddress = intent.getStringExtra(EXTRAS_DEVICE_ADDRESS);
// Sets up UI references.
/*
((TextView) findViewById(R.id.device_address)).setText(mDeviceAddress);
mGattServicesList = (ExpandableListView) findViewById(R.id.gatt_services_list);
mGattServicesList.setOnChildClickListener(servicesListClickListner);
mConnectionState = (TextView) findViewById(R.id.connection_state);
*/
mDataField = (TextView) findViewById(R.id.data_value);
ch1DataField = (TextView) findViewById(R.id.ch1_value);
ch2DataField = (TextView) findViewById(R.id.ch2_value);
// btnRead = (Button) findViewById(R.id.button);
getActionBar().setTitle(mDeviceName);
getActionBar().setDisplayHomeAsUpEnabled(true);
// Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
// bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
service_init();
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
if (mBluetoothLeService != null) {
final boolean result = mBluetoothLeService.connect(mDeviceAddress);
Log.d(TAG, "Connect request result=" + result);
}
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(mGattUpdateReceiver);
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(mServiceConnection);
mBluetoothLeService = null;
}
@Override
protected void onStop() {
super.onStop(); // Always call the superclass method first
save();
Toast.makeText(getApplicationContext(), "onStop called", Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.gatt_services, menu);
if (mConnected) {
menu.findItem(R.id.menu_connect).setVisible(false);
menu.findItem(R.id.menu_disconnect).setVisible(true);
} else {
menu.findItem(R.id.menu_connect).setVisible(true);
menu.findItem(R.id.menu_disconnect).setVisible(false);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_connect:
mBluetoothLeService.connect(mDeviceAddress);
return true;
case R.id.menu_disconnect:
mBluetoothLeService.disconnect();
return true;
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
private void updateConnectionState(final int resourceId) {
runOnUiThread(new Runnable() {
@Override
public void run() {
//mConnectionState.setText(resourceId);
}
});
}
private void displayData(TextView dataField, String data) {
if (data != null) {
dataField.setText(data);
if(dataField == ch1DataField) {
String[] parts = data.split(" ");
int ch1data = Integer.parseInt(parts[1]);
save_plot_list.add(parts[1]);
series0.appendData(new DataPoint(X_axies++, ch1data), true, 33);
}
}
}
private void displaySavedData() {
for(int i=0; i < save_plot_list.size(); i++) {
series0.appendData(new DataPoint(i, Integer.parseInt(save_plot_list.get(i))), true, 33);
}
}
private void displayCh2Data(String data) {
if (data != null) {
ch2DataField.setText(data);
}
}
private void service_init() {
Intent bindIntent = new Intent(this, BluetoothLeService.class);
bindService(bindIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
LocalBroadcastManager.getInstance(this).registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
}
// Demonstrates how to iterate through the supported GATT Services/Characteristics.
// In this sample, we populate the data structure that is bound to the ExpandableListView
// on the UI.
private void displayGattServices(List<BluetoothGattService> gattServices) {
if (gattServices == null) return;
String uuid = null;
String unknownServiceString = getResources().getString(R.string.unknown_service);
String unknownCharaString = getResources().getString(R.string.unknown_characteristic);
ArrayList<HashMap<String, String>> gattServiceData = new ArrayList<HashMap<String, String>>();
ArrayList<ArrayList<HashMap<String, String>>> gattCharacteristicData
= new ArrayList<ArrayList<HashMap<String, String>>>();
mGattCharacteristics = new ArrayList<ArrayList<BluetoothGattCharacteristic>>();
// Loops through available GATT Services.
for (BluetoothGattService gattService : gattServices) {
HashMap<String, String> currentServiceData = new HashMap<String, String>();
uuid = gattService.getUuid().toString();
currentServiceData.put(
LIST_NAME, SampleGattAttributes.lookup(uuid, unknownServiceString));
currentServiceData.put(LIST_UUID, uuid);
gattServiceData.add(currentServiceData);
ArrayList<HashMap<String, String>> gattCharacteristicGroupData =
new ArrayList<HashMap<String, String>>();
List<BluetoothGattCharacteristic> gattCharacteristics =
gattService.getCharacteristics();
ArrayList<BluetoothGattCharacteristic> charas =
new ArrayList<BluetoothGattCharacteristic>();
// Loops through available Characteristics.
for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
charas.add(gattCharacteristic);
HashMap<String, String> currentCharaData = new HashMap<String, String>();
uuid = gattCharacteristic.getUuid().toString();
currentCharaData.put(
LIST_NAME, SampleGattAttributes.lookup(uuid, unknownCharaString));
currentCharaData.put(LIST_UUID, uuid);
gattCharacteristicGroupData.add(currentCharaData);
}
mGattCharacteristics.add(charas);
gattCharacteristicData.add(gattCharacteristicGroupData);
}
SimpleExpandableListAdapter gattServiceAdapter = new SimpleExpandableListAdapter(
this,
gattServiceData,
android.R.layout.simple_expandable_list_item_2,
new String[] {LIST_NAME, LIST_UUID},
new int[] { android.R.id.text1, android.R.id.text2 },
gattCharacteristicData,
android.R.layout.simple_expandable_list_item_2,
new String[] {LIST_NAME, LIST_UUID},
new int[] { android.R.id.text1, android.R.id.text2 }
);
mGattServicesList.setAdapter(gattServiceAdapter);
}
private static IntentFilter makeGattUpdateIntentFilter() {
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED);
intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED);
intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED);
intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE);
return intentFilter;
}
public void onClickWrite(View v){
if(mBluetoothLeService != null) {
mBluetoothLeService.writeCustomCharacteristic(0xAA);
readCharLocal();
}
}
public void onClickClear(View v){
if(mBluetoothLeService != null) {
// mBluetoothLeService.writeCustomCharacteristic(0xAA);
// readCharLocal();
//SystemClock.sleep(500);
if (pref.contains(ARRAY_KEY)) {
prefsEditor.clear();
// SystemClock.sleep(500);
boolean test = prefsEditor.commit();
Log.d("onclick","bool---> "+ test);
}
}
}
public void onClickRead(View v){
if(mBluetoothLeService != null) {
int i = 0;
mBluetoothLeService.readCustomCharacteristic(SampleGattAttributes.CH0_CHARACTERISTIC);
SystemClock.sleep(500);
mBluetoothLeService.readCustomCharacteristic(SampleGattAttributes.CH1_CHARACTERISTIC);
}
}
public void readCharLocal() {
SystemClock.sleep(500);
mBluetoothLeService.readCustomCharacteristic(SampleGattAttributes.CH0_CHARACTERISTIC);
SystemClock.sleep(500);
mBluetoothLeService.readCustomCharacteristic(SampleGattAttributes.CH1_CHARACTERISTIC);
SystemClock.sleep(500);
mBluetoothLeService.readCustomCharacteristic(SampleGattAttributes.CH2_CHARACTERISTIC);
}
//set up the auto scroll and zoom when ploting in real time.
private void setGraphUI(GraphView graph) {
//data
graph.getLegendRenderer().setVisible(true);
graph.getLegendRenderer().setAlign(LegendRenderer.LegendAlign.TOP);
graph.getLegendRenderer().setTextSize(20);
Viewport viewport = graph.getViewport();
// activate horizontal zooming and scrolling
viewport.setScalable(true);
// activate horizontal scrolling
viewport.setScrollable(true);
// activate horizontal and vertical zooming and scrolling
viewport.setScalableY(true);
// activate vertical scrolling
viewport.setScrollableY(true);
graph.getGridLabelRenderer().setPadding(96);
}
public void save() {
Gson gson = new Gson();
String json = gson.toJson(save_plot_list);
//saves the array
prefsEditor.putString(ARRAY_KEY, json);
//saves the x axeies
prefsEditor.putInt(INT_KEY, X_axies);
Log.d("close", "array------> " + json);
prefsEditor.commit();
}
public void get() {
// prefsEditor.clear();
// prefsEditor.commit();
//List<String> save_plot_list = new ArrayList<String>();
Gson gson = new Gson();
if (pref.contains(ARRAY_KEY)) {
X_axies = pref.getInt(INT_KEY, 0);
String json = pref.getString(ARRAY_KEY, "");
List<String> my_saved_plots = gson.fromJson(json, List.class);
save_plot_list = gson.fromJson(json, List.class);
for (String temp : save_plot_list) {
Log.d("open", temp + " - " + "x=" + X_axies);
}
displaySavedData();
}
}
public void clearPrefKeys() {
if (pref.contains(ARRAY_KEY)) {
prefsEditor.clear();
boolean test = prefsEditor.commit();
Log.d("onclick","bool---> "+ test);
}
}
}