Service:应用组件---可以在后台进行长期运行操作,但却不用提供用户界面
1、可以由其他应用组件启动:如Activity 通过调用startService()启动服务。一旦启动就可以在后台无限期执行,通常不会将结果返回给调用方
2、当应用组件通过调用:bindService() 绑定到服务时,一旦绑定就可以允许组件与服务进行交互、发送请求、获取结果
因此应用可以处于:启动状态、绑定状态、启动并绑定状态
如果使用服务,在默认的情况下,任然会在应用的主线程中运行,因此如果服务执行的是密集型或阻止性操作,则仍应在服务内创建新线程。
举个例子说明启动服务问题:
1、MainActivity
import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //增加代码 Button start = (Button)findViewById(R.id.buttonstart); Button stop = (Button)findViewById(R.id.buttonstop); start.setOnClickListener(this); stop.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.buttonstart: Intent startIntent = new Intent(this,MyService.class); //调用startService()请求启动服务时,系统将调用onStartCommand() startService(startIntent); break; case R.id.buttonstop: Intent stopIntent= new Intent(this,MyService.class); stopService(stopIntent); default: break; } } }2、MyService:
import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class MyService extends Service { String TAG = "MyService"; public MyService() { } //另一个组件想通过调用 bindService() 与服务绑定(例如执行 RPC)时,系统将调用此方法 // 则服务只会在该组件与其绑定时运行。一旦该服务与所有客户端之间的绑定全部取消,系统便会销毁它。 @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } //首次创建服务时,系统将调用此方法来执行一次性设置程序(在调用 onStartCommand() 或 onBind() 之前)。 // 如果服务已在运行,则不会调用此方法。 @Override public void onCreate() { super.onCreate(); Log.i(TAG, "onCreate: "+"MyService start excute"); } //当一个组件(Activity)通过调用startService()请求启动服务时,系统将调用这个方法 //该服务启动之后,其生命周期独立于启动它的组件,即使启动它的组件被销毁也不受影响 //服务应通过调用stopSelf()来自行停止运行,后者由另一个组件通过调用stopService()来停止它 @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i(TAG, "onCreate: "+"MyService onStartCommand excute"); return super.onStartCommand(intent, flags, startId); } //当服务不再使用且将被销毁时,系统将调用此方法。 // 服务应该实现此方法来清理所有资源,如线程、注册的侦听器、接收器等。 这是服务接收的最后一个调用。 @Override public void onDestroy() { super.onDestroy(); Log.i(TAG, "onCreate: "+"MyService onDestroy excute"); } }3、其他需要文件
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.meizu.androidsdktest.MainActivity"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/buttonstart" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="StartService"/> <Button android:id="@+id/buttonstop" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="StopService"/> </LinearLayout> </android.support.constraint.ConstraintLayout>AndroidManifest.xml
<service android:name=".MyService" android:enabled="true" android:exported="false"> </service> 总结:一旦调用startService(),相应的服务会起来,并且会回调OnstartCommand()方法(如果服务为初次创建则会先调用onCreate()) 不管调用多少次startService()在销毁时只需要调用一次stopService()或者stopSelf(),服务就会停止