Android 服务不会立即启动
public class TestService extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Intent service=new Intent(getApplicationContext(),MessageListener.class);
Log.v("Test", "Going to start service");
startService(service);
Log.v("Test", "service started?");
}
}
public class MessageListener extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v("Test", "Start Cmd");
intent.setAction("Started");
new Thread(new Runnable() {
@Override
public void run() {
for(int i=100;i<200;i++){
Log.v("Test",i+"");
}
}
}).start();
return START_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
Log.v("Test", "Create");
}
我希望它会打印:
Start Service
create
Start cmd
print 1->100
Service Started.
但我得到了
Start Service
Service Started.
create
Start cmd
prints 1->100
为什么是这样?
我发现问题是由于异步造成的。 startService 将在父方法完成后被调用。 解决方案是:
public class TestService extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent service=new Intent(getApplicationContext(),MessageListener.class);
startService(service);
mCheckerHandler.sendEmptyMessageDelayed(MSG_CHECK_SERVICE_RUNNING, 100);
}
private static final int MSG_CHECK_SERVICE_RUNNING = 0x001122;
private Handler mCheckerHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == MSG_CHECK_SERVICE_RUNNING) {
if (checkServiceRunning()) {
//Do something
} else {
//Send another message to check in the next 100ms
sendEmptyMessageDelayed(MSG_CHECK_SERVICE_RUNNING, 100);
}
}
};
};
}
谢谢大家。特别是对平先生:)
public class TestService extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Intent service=new Intent(getApplicationContext(),MessageListener.class);
Log.v("Test", "Going to start service");
startService(service);
Log.v("Test", "service started?");
}
}
public class MessageListener extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v("Test", "Start Cmd");
intent.setAction("Started");
new Thread(new Runnable() {
@Override
public void run() {
for(int i=100;i<200;i++){
Log.v("Test",i+"");
}
}
}).start();
return START_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
Log.v("Test", "Create");
}
I expect it would print:
Start Service
create
Start cmd
print 1->100
Service Started.
But I'm getting
Start Service
Service Started.
create
Start cmd
prints 1->100
Why is it?
I've found the problem is due to Asynchronous. startService will be called after parent's method finished.
The solution is:
public class TestService extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent service=new Intent(getApplicationContext(),MessageListener.class);
startService(service);
mCheckerHandler.sendEmptyMessageDelayed(MSG_CHECK_SERVICE_RUNNING, 100);
}
private static final int MSG_CHECK_SERVICE_RUNNING = 0x001122;
private Handler mCheckerHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == MSG_CHECK_SERVICE_RUNNING) {
if (checkServiceRunning()) {
//Do something
} else {
//Send another message to check in the next 100ms
sendEmptyMessageDelayed(MSG_CHECK_SERVICE_RUNNING, 100);
}
}
};
};
}
Thanks for all of you. Especially to Mr Binh :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为线程以“伪”并行方式执行,因此在计数器线程获得任何 CPU 时间进行写入之前调用
Log.v("Test", "service started?");
。“伪”并行是因为大多数手机没有超过 1 个 CPU,因此它们无法并行计算,因此只能从一个线程切换到另一个线程。您可以在 Wikipedia 或您喜欢的任何其他来源上阅读有关线程的更多信息。
That's because the thread is executing in "pseudo"-parallel so the
Log.v("Test", "service started?");
is called before the counter-thread gets any CPU-time to write."Pseudo"-parallel because most phones don't have more than 1 CPU so they cannot compute in parallel so they only switch from one thread to another. You can read more about threading on Wikipedia or any other source you like.