我创建了一个通过调用startService()方法启动服务的Android应用程序。我想在分配给它的任务完成时停止服务。我如何检查相应的任务是否完成?我可以在onStartCommand()中调用stopSelf()吗?调用stopSelf()或stopService()方法的最佳实践是什么?它的呼叫应该放在哪里?
谢谢!我们可以在哪里调用stopSelf()或stopService()方法来停止服务?
1
A
回答
0
// main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Services Demo" android:gravity="center" android:textSize="20sp" android:padding="20dp"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/buttonStart" android:text="Start"></Button>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Stop" android:id="@+id/buttonStop"></Button>
</LinearLayout>
//Activity class
public class ServicesDemo extends Activity implements OnClickListener {
private static final String TAG = "ServicesDemo";
Button buttonStart, buttonStop;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
}
public void onClick(View src) {
switch (src.getId()) {
case R.id.buttonStart:
Log.d(TAG, "onClick: starting srvice");
startService(new Intent(this, MyService.class));
break;
case R.id.buttonStop:
Log.d(TAG, "onClick: stopping srvice");
stopService(new Intent(this, MyService.class));
break;
}
}
}
//Service class
public class MyService extends Service {
private static final String TAG = "MyService";
MediaPlayer player;
@Override
public IBinder onBind(Intent intent) {
Toast.makeText(this, "My Service Bind", Toast.LENGTH_LONG).show();
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
player = MediaPlayer.create(this, R.raw.preview);
player.setLooping(false); // Set looping
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
player.stop();
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
player.start();
}
}
2
如果坚持更好的选择是使用IntentService
如果你想执行一个特定的任务并停止服务。通过使用IntentService
,当您的任务完成时,您将收到使用BroadCastReceiver
的通知。 Here
是一个完整的例子说明。
相关问题
- 1. 在哪里调用stopService()以确保服务停止?
- 2. 服务不停止在调用stopService()
- 3. ASP.Net或Silverlight Web服务,我们在哪里停止
- 4. 我应该在哪里停止服务?
- 5. 我可以在Service.onStartCommand中调用stopSelf()吗?
- 6. 将stopService()停止前台服务?
- 7. stopService(意图)不停止服务?
- 8. 什么时候会停止提供服务,如果如果我使用<code>NOT</code><code>IntentService</code>那么当将服务停止,如果我不叫<code>stopService()</code>或<code>stopSelf()</code>方法我不叫stopService()或stopSelf()方法
- 9. 我们在哪里可以使用java中的Object.clone()方法?
- 10. 我们在哪里可以使用递归方法“这个”
- 11. 我们可以在哪里使用AS3?
- 12. 我们可以调用一个servlet web服务方法
- 13. 我们可以在哪里写MainMenu.xib的windowDidBecomeKey方法
- 14. stopService不会停止我的服务....为什么?
- 15. 我们可以重写WCF或Web服务中的方法吗?
- 16. 调用stopService后服务正在运行
- 17. 我可以同步停止服务吗?
- 18. 当Windows服务停止时,WCF服务仍然可以调用
- 19. 我可以在哪里调用gluUnproject?
- 20. 节点服务器停止后,我可以调用$ modal.open吗?
- 21. 我在哪里可以获得Web服务器或文件共享服务器?
- 22. 我在哪里可以找到Android方法的来源?
- 23. 我们可以调用Spring Remoting web服务JAVA来自.Net
- 24. 我可以告诉哪个线程调用了我的服务方法吗?
- 25. 在哪种情况下,我们可以使用android服务?
- 26. 我可以在哪里存储我的网络服务器URL?
- 27. 停止或中止服务
- 28. 如何在onPause方法被调用时停止服务?
- 29. 我们可以在哪里配置WCF RIA服务的WCF参数?
- 30. OAuth使用者停止工作,我在哪里开始调试?
你可以打开你的Android SDK目录,浏览到ApiDemos'SRC/com.example.android.apis.app',开'AlarmService_Service.java'以获取更多信息。 – 2012-03-06 06:32:34