我想在程序的后台捕获 GPS 的速度。我有捕捉速度的代码,但除非它是程序中运行的第一件事,否则它将无法工作。应该将其编码为广播意图还是意图服务以便之后弹出通知?
SpeedManagerService.java:
public class SpeedManagerService extends Service {
private static final String TAG = "SpeedCheckerService";
private boolean isRunning = false;
@Override
public void onCreate() {
Log.i( TAG, "Service onCreate" );
isRunning = true;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i( TAG, "Service onStartCommand" );
//Creating new thread for my service
new Thread( new Runnable() {
@Override
public void run() {
if (isRunning) {
Log.i( TAG, "Service running" );
}
}
} ).start();
return Service.START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
然后我在 Splash 页面下添加了该特定页面的 Intent(在启动页面开始之前在 onCreate 方法下运行):
public class SplashActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Intent intent = new Intent( this, Checker.class );
startService( intent );
任何帮助将不胜感激。谢谢。