我正在处理一大堆代码,使用户能够摇动手机,并显示或启动摇动活动。我试图找出当我摇动设备时代码没有运行的原因。然而,当我在服务之外运行此代码时,它运行完美。
注意:服务确实启动了,我查了一下。要发布的课程在我的清单中,我再次检查。没有错误存在,甚至更加怪异的代码在启动时不会停止或失败。它只是没有做我说的。是的,也设置了所有适当的权限。
所以这是代码:
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;
public class GMeter extends Service implements SensorEventListener {
private float mLastX, mLastY, mLastZ;
private boolean mInitialized;
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private final float NOISE = (float) 2.0;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
mInitialized = false;
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer , SensorManager.SENSOR_DELAY_NORMAL);
Toast.makeText(this, "Starting Service", Toast.LENGTH_LONG).show();
}
protected void onResume() {
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
mSensorManager.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// can be safely ignored for this demo
}
@Override
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
double a = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
double currentG = (a/ SensorManager.STANDARD_GRAVITY);
Toast.makeText(this, "Calculating G's", Toast.LENGTH_LONG).show();
if (!mInitialized && currentG > 1.0) {
mLastX = x;
mLastY = y;
mLastZ = z;
mInitialized = true;
//Start Second Activity
Intent intent = new Intent(getBaseContext(), thirdClass.class);
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
getApplication().startActivity(intent);
}else {
float deltaX = Math.abs(mLastX - x);
float deltaY = Math.abs(mLastY - y);
float deltaZ = Math.abs(mLastZ - z);
if (deltaX < NOISE) deltaX = (float)0.0;
if (deltaY < NOISE) deltaY = (float)0.0;
if (deltaZ < NOISE) deltaZ = (float)0.0;
mLastX = x;
mLastY = y;
mLastZ = z;
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
这不会给我任何错误,因此不会出现logcat。
答案 0 :(得分:1)
添加以下行,以使服务继续运行。
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
同样服务没有OnResume
和OnPause
功能,所以除非你自己打电话,否则它们没用。但是Service确实有一个onDestroy函数,您可以覆盖该函数以取消注册您的监听器
修改强>:
您使用的onCreate(Bundle savedInstanceState)
不是服务调用的oncreate签名。
使用
public void onCreate()
答案 1 :(得分:0)
服务和活动使用相同的上下文..您必须获得适当的上下文才能启动活动
喜欢在onCreate服务方法中获取应用程序上下文
public class abc extends Service {
Context mContext;
@Override
public void onCreate()
{
mContext=getApplicationContext();
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent)
{
// TODO Auto-generated method stub
return null;
}
}
并将此'mContext'用于startActivity