我只是使用这个简单的代码尝试Android中的服务:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(this, PlayerService.class));
}
PlayerService.java:
public class PlayerService extends IntentService
{
public PlayerService()
{
super("PlayerService");
}
@Override
protected void onHandleIntent(Intent i)
{
int n=0;
while(true)
{
Log.i("SERVICE", "Event n." + n++);
try {
Thread.sleep(1000);
}
catch (InterruptedException e)
{ }
}
}
@Override
public void onDestroy()
{
Log.i("SERVICE", "Distr Service");
}
}
我已在清单中声明了服务:
<service android:name="PlayerService"/>
当我启动此应用程序然后打开许多其他应用程序时,此应用程序停止工作(正如我在LogCat中看到的那样停止向我显示&#34;事件n ...&#34;)。为什么?