在应用程序后台捕获速度
Capturing Speed in background of app
我想在程序后台捕捉 GPS 速度。我有捕获速度的代码,但它不会工作,除非它是程序中的第一件事 运行ning。应该将其编码为广播 Intent 还是 Intent 服务以便之后弹出通知?
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;
}
}
然后我在启动页面下为该特定页面添加了 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 );
如有任何帮助,我们将不胜感激。谢谢
您应该实施服务并在其中设置您的条件。对于初学者,请阅读此 Guide。您可以在位置侦听器中调用 getSpeed() 函数。
从您的描述来看,您似乎可以在 Service 中完成所有工作。它会一直保持 运行ning - 你可以 运行 一个单独的线程来管理传入的数据处理,在 运行 时间注册和取消注册接收器以及 s0notify 用户。根据我的经验,以反应方式 (rx) 处理值流是个好主意 - 您接收数据,将其传递给运算符链(按速度值过滤)并在最后处理。 IntentService 更适合长期 运行 一次性工作
我想在程序后台捕捉 GPS 速度。我有捕获速度的代码,但它不会工作,除非它是程序中的第一件事 运行ning。应该将其编码为广播 Intent 还是 Intent 服务以便之后弹出通知?
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;
}
}
然后我在启动页面下为该特定页面添加了 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 );
如有任何帮助,我们将不胜感激。谢谢
您应该实施服务并在其中设置您的条件。对于初学者,请阅读此 Guide。您可以在位置侦听器中调用 getSpeed() 函数。
从您的描述来看,您似乎可以在 Service 中完成所有工作。它会一直保持 运行ning - 你可以 运行 一个单独的线程来管理传入的数据处理,在 运行 时间注册和取消注册接收器以及 s0notify 用户。根据我的经验,以反应方式 (rx) 处理值流是个好主意 - 您接收数据,将其传递给运算符链(按速度值过滤)并在最后处理。 IntentService 更适合长期 运行 一次性工作