我对 Java 和 Android 开发都非常缺乏经验。但我设法制作了一个简单的 Android 应用程序来读取接近传感器的值。我还设法编写了一个 Java 应用程序来编辑谷歌电子表格中的一个单元格。
因此,现在我想将两者结合起来,并拥有一个 Android 应用程序,该应用程序将根据 poximity 传感器的值更改谷歌电子表格中单元格的值。
但是,当我组合代码时,我的应用程序崩溃了,而且我对 Eclipse 环境和调试器了解不足,无法弄清楚发生了什么。该应用程序安装良好并提供午餐。主视图仅显示一秒钟,然后应用程序崩溃。
package com.example.mysensorproject;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.CellEntry;
import com.google.gdata.data.spreadsheet.CellFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
public class MainActivity extends Activity implements SensorEventListener {
TextView proxText;
SensorManager sm;
Sensor proxSensor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sm=(SensorManager)getSystemService(SENSOR_SERVICE);
proxSensor=sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
proxText=(TextView)findViewById(R.id.proximityTextView);
sm.registerListener(this, proxSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
proxText.setText(String.valueOf(event.values[0]));
//change value in spreadsheet
SpreadsheetService service = new SpreadsheetService("wise");
service.setProtocolVersion(SpreadsheetService.Versions.V3);
try {
service.setUserCredentials("email@example.com", "password");
} catch (AuthenticationException e) {
e.printStackTrace();
}
URL url = null;
try {
url = new URL("https://spreadsheets.google.com/feeds/cells/0AuDoKcCfLURKdEQ3bVFLemY5aVh2SGQySE10QTYzRGc/od6/private/full");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
CellFeed cellFeed = null;
try {
cellFeed = service.getFeed(url, CellFeed.class);
// System.out.println("Cell Feed Worked");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (CellEntry cell : cellFeed.getEntries()) {
cell.changeInputValueLocal(String.valueOf(event.values[0]));
try {
cell.update();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
*/
}
}