pol*_*kyg 0 android android-intent android-sensors intentservice
在我的应用程序onCreate我检查一些条件,然后我开始这样的活动:
Intent startIntent = new Intent(getApplicationContext(), EnableLocationProviderActivity.class);
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(startIntent);
Run Code Online (Sandbox Code Playgroud)
从该Activity开始,我启动一个IntentService,为传感器注册一些监听器,它以STICKY开头,意味着它应该被显式停止.IntentService监视传感器.
我的问题是,当我回到第一个Activity时,传感器不再感应了(我将一个Log.v放在onSensorChanged中(开始显示数据,然后停止).
如果我没有明确地停止它,为什么它会停止?此外,我看到有时会调用IntentService的OnDestroy,但是,如果它是STICKY并且我没有调用stopself()并且没有以任何其他方式停止,那么如何调用它?
谢谢!吉列尔莫.
编辑
这是IntentService的代码(它应该一直在运行,尽管手机进入睡眠状态或按下主页按钮(我知道电池和其他一切,用户将被警告这个并且会有机会)在他想要时关闭应用程序.
从MainActivity调用该服务,如下所示:
Intent startIntent = new Intent(GdpTesisApplication.getInstance().getApplicationContext(), SensingService.class);
startService(startIntent);
Run Code Online (Sandbox Code Playgroud)
服务代码是这样的:
public class SensingService extends IntentService implements SensorEventListener {
private float[] mAccelerationValues;
private SensorManager mSensorManager = null;
String sensorType = "";
public SensingService(String name) {
super(name);
setIntentRedelivery(true);
}
public SensingService() {
super("SensingService");
setIntentRedelivery(true);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v(ApplicationName,"SensingService.onStartCommand");
super.onStartCommand(intent, flags, startId); // If this is not written then onHandleIntent is not called.
return START_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
Log.v(ApplicationName, "SensingService.onCreate");
initialize();
}
private void initialize() {
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); // This must be in onCreate since it needs the Context to be created.
mAccelerationValues = new float[3];
Log.v(ApplicationName, "Opening Location Service from Sensing Service");
LocationService myLocation = new LocationService();
myLocation.getLocation(this, locationResult);
}
@Override
public void onDestroy() {
Log.v(ApplicationName, "SensingService.onDestroy");
super.onDestroy();
if (mSensorManager != null) {
mSensorManager.unregisterListener(this);
}
}
@Override
protected void onHandleIntent(Intent intent) {
Log.v(ApplicationName, "SensingService.onHandleIntent");
if (mSensorManager != null) {
registerListeners();
}
}
public LocationResult locationResult = new LocationResult() {
@Override
public void gotLocation(final Location location) {
if (location != null) {
Log.v(ApplicationName, "Location != null : (" + location.getLatitude() + "," + location.getLongitude() + ")");
} else {
Log.v(ApplicationName, "Location == null : (0,0)");
}
}
};
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent currentEvent) {
if (currentEvent.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
return;
}
synchronized (this) {
float[] accelVals = null;
float totalForce = 0.0f;
int sensor = currentEvent.sensor.getType();
System.arraycopy(currentEvent.values, 0, mAccelerationValues, 0, 3); // We use System.arraycopy because of this:
switch (sensor) {
case Sensor.TYPE_ACCELEROMETER:
sensorType = "Accelerometer";
totalForce = SensorsHelpers.getTotalForceInGs(mAccelerationValues);
break;
case Sensor.TYPE_LINEAR_ACCELERATION:
sensorType = "LinearAcceleration";
totalForce = SensorsHelpers.getTotalForceInGs(mAccelerationValues) + 1;
break;
case Sensor.TYPE_GRAVITY:
totalForce = SensorsHelpers.getTotalForceInGs(mAccelerationValues);
sensorType = "Gravity";
break;
}
Log.v(ApplicationName,DateHelper.GetUTCdatetimeFromDate(new Date()) + " - from sensingService");
}
}
private void registerListeners() {
Log.v(ApplicationName, "Registering sensors listeners");
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION), SensorManager.SENSOR_DELAY_UI);
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY),SensorManager.SENSOR_DELAY_UI);
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI);
}
}
Run Code Online (Sandbox Code Playgroud)
更新2
现在我在onCreate方法上添加了这个:
int NOTIFICATION_ID = 1;
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 1, intent, 0);
Notification notification = new Notification(R.drawable.ic_dialog_info, "Running in the Foregound", System.currentTimeMillis());
notification.setLatestEventInfo(this, "Title", "Text", pi);
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
startForeground(NOTIFICATION_ID, notification);
Run Code Online (Sandbox Code Playgroud)
将它作为startForground启动,但它将图标放在通知栏中,然后在服务中调用onDestroy并且通知图标消失.
我现在绝望了!请帮帮忙!
谢谢!吉列尔莫.
根据需要启动服务,使用工作线程依次处理每个Intent,并在工作失败时自行停止
此外,根据相同的文档,你不应该覆盖onStartCommand()和onDestroy()在你IntentService,我假设,因为它实现了自己的特殊行为,如上所述.也许你需要延伸Service而不是IntentService.
| 归档时间: |
|
| 查看次数: |
4879 次 |
| 最近记录: |