我有一个带有按钮活动的代码,当点击该按钮时会启动加速度计服务。
加速度计服务使用PARTIAL_WAKE_LOCK
中的onStart()
,如图所示:
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
//Power Manager
PowerManager pm = (PowerManager)getApplicationContext().getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MY WK");
wl.acquire();
//Power Manager
try{
mInitialized = false;
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer,SensorManager.SENSOR_DELAY_NORMAL);
}
catch(Exception e)
{
Log.e("acc","catch1");
}
}// end of onStart()
在onSensorChange(传感器事件)中,我有使用HTTP POSt将数据记录到远程服务器的代码,如下所示:
synchronized public void onSensorChanged(SensorEvent event) {
if( event.sensor.getType() == Sensor.TYPE_ACCELEROMETER )
{
//-----------------------------sending it to server---------
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("xaxis", xaxis));
nameValuePairs.add(new BasicNameValuePair("yaxis", yaxis));
nameValuePairs.add(new BasicNameValuePair("zaxis", zaxis));
//------------------
Thread networkThread = new Thread() {
@Override
public void run() {
HttpClient httpclient1 = new DefaultHttpClient();
HttpPost httppost1 = new HttpPost("http://www.xxxxx.com/filename.php");
httppost1.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response1 = httpclient1.execute(httppost1);
}
};
networkThread.start();
//----------------------------------
}//and of if
}//end of onSensorChanged
我的问题是:
当手机屏幕变暗时,HTTP会停止记录到服务器。但是,即使键盘被锁定,当屏幕亮时,它也会记录数据。
请帮助我,因为我想让这项服务在后台运行,并将数据记录到服务器,即使屏幕被锁定。
因此,它的工作正常,直到屏幕变暗,之后它没有记录任何数据。
Does HTTP not work with Partial_WAKE_Lock.
I searched for the answers but could not find any. Hoping to get some help !
Thank You
答案 0 :(得分:0)
幸运的是,我在这个链接Android accelerometer not working when screen is turned off中找到了一个答案,它说不是HTTP帖子不起作用,但是当屏幕关闭时,没有生成加速度计值。谢谢