在 Android 服务中创建未使用的实例变量
我正在开发一个 Android 应用程序,它有一个服务监听一些事件并将它们记录在后台。由于我记录了多个事件,因此我为要记录的每种类型的事件创建了一个记录器类。下面是这样一个类。
public class AccelerometerLogger {
private final Context mContext;
private final SensorManager mSensorManager;
private final Sensor mAccelerometer;
private final AccelerometerListener mAccelerometerListener;
public AccelerometerLogger(Context context) {
super(context);
mContext = context;
mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = getAccelerometer();
mAccelerometerListener = new AccelerometerListener();
boolean status = false;
if (mAccelerometer != null) {
status = mSensorManager.registerListener(mAccelerometerListener, mAccelerometer,
SensorManager.SENSOR_DELAY_NORMAL);
}
Log.i("AccelerometerLogger", "Regisrtered = " + status);
}
private Sensor getAccelerometer() {
List<Sensor> sensors = mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
if (sensors.size() > 0) {
return sensors.get(0);
}
return null;
}
private class AccelerometerListener implements SensorEventListener {
long lastTime;
long now;
AccelerometerListener() {
now = lastTime = 0;
}
@Override
public void onSensorChanged(SensorEvent event) {
now = event.timestamp; //timestamp is in nanoseconds
if (now - lastTime > 5000000000l) {
// Log some stuff here
}
}
}
}
现在,在服务的 onCreate() 中,我执行了
AccelerometerLogger accelerometerLogger = new AccelerometerLogger(getApplicationContext());
但我永远不需要使用 AccelerometerLogger,因为该对象一旦构造就隐式完成所有工作,并且只要服务执行,它就会一直存在。尽管该方法似乎对我有用,但始终会出现未使用的局部变量的警告。有更好的方法来完成我想做的事情吗?
感谢您抽出时间来帮助我。
谢谢, 普拉迪普
I am developing an Android application that has a Service listening for some events and logging them in the back ground. Since I log several event, I have created a logger class for each type of event I want to log. The following is one such class.
public class AccelerometerLogger {
private final Context mContext;
private final SensorManager mSensorManager;
private final Sensor mAccelerometer;
private final AccelerometerListener mAccelerometerListener;
public AccelerometerLogger(Context context) {
super(context);
mContext = context;
mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = getAccelerometer();
mAccelerometerListener = new AccelerometerListener();
boolean status = false;
if (mAccelerometer != null) {
status = mSensorManager.registerListener(mAccelerometerListener, mAccelerometer,
SensorManager.SENSOR_DELAY_NORMAL);
}
Log.i("AccelerometerLogger", "Regisrtered = " + status);
}
private Sensor getAccelerometer() {
List<Sensor> sensors = mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
if (sensors.size() > 0) {
return sensors.get(0);
}
return null;
}
private class AccelerometerListener implements SensorEventListener {
long lastTime;
long now;
AccelerometerListener() {
now = lastTime = 0;
}
@Override
public void onSensorChanged(SensorEvent event) {
now = event.timestamp; //timestamp is in nanoseconds
if (now - lastTime > 5000000000l) {
// Log some stuff here
}
}
}
}
Now, in the onCreate() of the Service I do
AccelerometerLogger accelerometerLogger = new AccelerometerLogger(getApplicationContext());
But I never need to use accelerometerLogger, since the object implicitly does all the work once constructed, and hangs around as long as the Service does. Although the approach seems to be working for me, there is always a warning for unused local variable. Is there a better way of accomplishing what I want to do?
I appreciate your time to help me.
Thanks,
Pradeep
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 eclipse,您可以将其粘贴在声明上方。
我想因为提供了这个注释,所以创建未使用的对象不会被视为邪恶或任何东西。这些警告确实很烦人,所以很高兴他们有这些注释。
If your using eclipse you can just stick this above the declaration.
I guess because this annotation is provided, creating unused objects isn't seen as evil or anything. The warnings are really annoying though so it's great they have these kind of annotations.
编辑在OP在评论中澄清这被分配给服务类中的字段而不是局部变量之后,我的答案不再适用。
做。
你可以自己
EDIT following the OP's clarification in the comments that this is being assigned to a field in the service class, rather than a local variable, my answer no longer applies.
You can just do
by itself.