如何在后台为 Android 应用程序维护服务
How to maintain Service in background for Android app
所以我能够获得一项服务,该服务将 运行 首先使用 FLAG_ACTIVITY_NEW_TASK
。希望这是执行此操作的正确方法。但是,由于我在程序完全启动之前使用 intent 在启动画面上启动 service(),因此它不允许启动画面图像工作,只允许服务工作,然后它进入主屏幕。
我怎样才能让两者都起作用?我使用 Service
正确吗?
如有任何帮助,我们将不胜感激。这是我目前所拥有的:
我的服务class:
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class SpeedManagerService extends Service {
private static final String TAG = "SpeedCheckerService";
@Override
public void onCreate() {
Log.i( TAG, "Service onCreate" );
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i( TAG, "Service onStartCommand" );
Intent intnt = new Intent(this, SpeedChecker.class);
intnt.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intnt);
return Service.START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
这是我的启动画面:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class SplashActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.splash );
/**
* Timer set to spend 3 seconds on splash screen then off to main menu
*/
Thread timerThread = new Thread() {
public void run() {
try {
startService();
sleep( 3000 );
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent intent = new Intent( SplashActivity.this, MenuActivity.class );
startActivity( intent );
}
}
};
timerThread.start();
}
@Override
protected void onPause() {
super.onPause();
finish();
}
public void startService() {
Intent intent = new Intent( this, SpeedManagerService.class );
startService( intent );
}
}
我的整个想法是让 Service
运行 在后台运行,每当它到达需要的位置时,就会发出通知,单击通知后,它会转到以下位置之一其他 classes (otheractivity.class)。
如果我需要 post 更多代码,请告诉我。谢谢!
因此,当您启动这样的服务时,它不在后台而是在主线程中,请参阅 https://developer.android.com/guide/components/services.html
这就是你的另一个 activity 没有加载的原因 - 可能你正在做一些你认为会在后台执行的工作(网络访问或磁盘访问或一些 CPU 密集型工作)。
我相信 IntentService 与您现在正在做的不同,它会更好地满足您的需求 - 加上它的 onHandleIntent(Intent) 已经 运行 在后台,请参阅 https://developer.android.com/reference/android/app/IntentService.html
所以我能够获得一项服务,该服务将 运行 首先使用 FLAG_ACTIVITY_NEW_TASK
。希望这是执行此操作的正确方法。但是,由于我在程序完全启动之前使用 intent 在启动画面上启动 service(),因此它不允许启动画面图像工作,只允许服务工作,然后它进入主屏幕。
我怎样才能让两者都起作用?我使用 Service
正确吗?
如有任何帮助,我们将不胜感激。这是我目前所拥有的:
我的服务class:
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class SpeedManagerService extends Service {
private static final String TAG = "SpeedCheckerService";
@Override
public void onCreate() {
Log.i( TAG, "Service onCreate" );
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i( TAG, "Service onStartCommand" );
Intent intnt = new Intent(this, SpeedChecker.class);
intnt.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intnt);
return Service.START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
这是我的启动画面:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class SplashActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.splash );
/**
* Timer set to spend 3 seconds on splash screen then off to main menu
*/
Thread timerThread = new Thread() {
public void run() {
try {
startService();
sleep( 3000 );
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent intent = new Intent( SplashActivity.this, MenuActivity.class );
startActivity( intent );
}
}
};
timerThread.start();
}
@Override
protected void onPause() {
super.onPause();
finish();
}
public void startService() {
Intent intent = new Intent( this, SpeedManagerService.class );
startService( intent );
}
}
我的整个想法是让 Service
运行 在后台运行,每当它到达需要的位置时,就会发出通知,单击通知后,它会转到以下位置之一其他 classes (otheractivity.class)。
如果我需要 post 更多代码,请告诉我。谢谢!
因此,当您启动这样的服务时,它不在后台而是在主线程中,请参阅 https://developer.android.com/guide/components/services.html 这就是你的另一个 activity 没有加载的原因 - 可能你正在做一些你认为会在后台执行的工作(网络访问或磁盘访问或一些 CPU 密集型工作)。
我相信 IntentService 与您现在正在做的不同,它会更好地满足您的需求 - 加上它的 onHandleIntent(Intent) 已经 运行 在后台,请参阅 https://developer.android.com/reference/android/app/IntentService.html