2012-03-06 40 views
1


我创建了一个通过调用startService()方法启动服务的Android应用程序。我想在分配给它的任务完成时停止服务。我如何检查相应的任务是否完成?我可以在onStartCommand()中调用stopSelf()吗?调用stopSelf()或stopService()方法的最佳实践是什么?它的呼叫应该放在哪里?
谢谢!我们可以在哪里调用stopSelf()或stopService()方法来停止服务?

+0

你可以打开你的Android SDK目录,浏览到ApiDemos'SRC/com.example.android.apis.app',开'AlarmService_Service.java'以获取更多信息。 – 2012-03-06 06:32:34

回答

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是一个完整的例子说明。

相关问题