Service生命周期和启动方式及后台操作及IntentService
Service生命周期和启动方式
Service介绍
Service(服务):是一种可以在后台执行长时间运行操作而没有用户界面的应用组件。
一经启动会长时间在后台运行,即使启动它的Activity或者Broadcast被销毁了也不会受影响。
注意:Service和Broadcast一样,运行在主线程中,不能做长时间的耗时操作。
Service特点
service在后台运行,不用与用户进行交互。即使应用退出,服务也不会停止。
当应用进程被杀死时,服务便会停止.
service运行在主线程中,当需要执行耗时操作的时候,需要在服务中创建子线程完成
service 的用途:播放音乐;后台下载大文件等
如何创建Service
1, 定义一个类, 继承Service
2, 重写父类的方法, onBind() — 必须重写的方法
3, 在清单文件中, 注册Service
Service的启动方式以及生命周期
启动方式一 (startService)
1,右键创建一个Service ,会自动在清单文件中创建,和Activity一样一样的.
public class MyService extends Service {
private static final String TAG = "MyService";
public MyService() {
Log.i(TAG, "MyService: 不好...被创建了");
}
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind: 绑啥绑");
return null;
}
布局文件
两个按钮,一个启动,一个停止
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动一个服务"
android:id="@+id/create"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止一个服务"
android:id="@+id/stop"/>
</LinearLayout>
java 代码文件
public class MainActivity extends AppCompatActivity {
private Button create;
private Button stop;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
create = (Button) findViewById(R.id.create);
stop = (Button) findViewById(R.id.stop);
create.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent(MainActivity.this, MyService.class);
startService(intent);
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopService(intent);
}
});
}
}
完善方法,验证生命周期
如果服务已经开启,就不会重复的执行 onCreate() ,而是调用 onStart() 或 onStartCommand()。而服务停止的时候调用 onDestory()
一旦服务开启跟开启者就没有任何关系;
开启者退出之后,服务还是可以在后台长期运行的。前提是没有调用 stopService(Intent);
开启者不能调用服务里面的方法
启动方式二 (bindService)(就是想调用服务里面的方法)
使用Service的步骤如下:
1,定义一个类,并继承 Service
2,在 AndroidManifest.xml 中配置此 Service
3,使用 Context 的 bindService(Intent, ServiceConnection, int) 方法来启动此 Service
4,不使用该服务时,调用 unbindService(ServiceConnection) 方法停止此 Service
绑定服务不会调用 onStart() 或 onStartCommand() 方法。
注意:同一个服务, 只能被同一个启动源, 绑定一次, 除非解绑后, 可以再次绑定
特点:
bind的方式开启服务,绑定服务,调用者挂了,服务也会跟着挂掉。
绑定者可以调用服务里面的方法。
Service代码
(1)创建服务:自定义类继承Service,返回代理人,方便开启者调用服务中的方法
public class MyService extends Service {
private static final String TAG = "MyService";
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
public class MyBinder extends Binder{
public void callStart(){
Start();
}
public void callstop(){
stop();
}
}
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "onUnbind: ");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy: ");
}
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate: ");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
public void Start(){
Log.i(TAG, "Start: ");
}
public void stop(){
Log.i(TAG, "stop: ");
}
}
(2)清单文件注册服务:
<service
android:name=".Service.MyService"
android:enabled="true"
android:exported="true" />
(3)绑定服务和解除绑定
private Button bind;
private Button unbind;
private Intent intent;
private Button start11;
private Button stop1;
private ServiceConnection serviceConnection;
private static final String TAG = "MainActivity";
private MyService.MyBinder binder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bind = (Button) findViewById(R.id.bind);
unbind = (Button) findViewById(R.id.unbind);
start11 = (Button) findViewById(R.id.start11);
stop1 = (Button) findViewById(R.id.stop1);
bind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 = new Intent(MainActivity.this, MyService.class);
serviceConnection= new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
binder = (MyService.MyBinder) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
bindService(intent1,serviceConnection, Service.BIND_AUTO_CREATE);
}
});
start11.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
binder.callStart();
}
});
stop1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
binder.callstop();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (serviceConnection!=null){
unbindService(serviceConnection);
}
}
}
总结:
绑定方式:onCreate() – onBind() — onUnbind() — onDestroy()
绑定服务:bindService()
解除绑定:unbindService()
两种启动方式切换
比如有一个需求:
想让服务在后台长期运行, 又想调用服务里面的方法
固定步骤:
1,先掉用startServer 方法开启服务.(保证长期运行)
2,调用bindServer 去获取中间人对象
3,调用nubindServer解绑服务.(服务不销毁)
4,调用stopService 去停止服务.
注意观察服务的生命周期.
想想我们的音乐播放器的模式,就是这种模式.
Service-IntentService
IntentService
一.IntentService介绍
IntentService,可以看做是Service和HandlerThread的结合体,在完成了使命之后会自动停止,适合需要在工作线程处理UI无关任务的场景。
IntentService 是继承自 Service 并处理异步请求的一个类,在 IntentService
内有一个工作线程来处理耗时操作。
当任务执行完后,IntentService 会自动停止,不需要我们去手动结束。
如果启动 IntentService 多次,那么每一个耗时操作会以工作队列的方式在 IntentService 的
onHandleIntent 回调方法中执行,依次去执行,使用串行的方式,执行完自动结束。
二.IntentService的优点:不用开启线程
三.IntentService的缺点:使用广播向activity传值
四.案例:使用IntentService网络请求json串,将json串使用广播发送给activity界面
思路:创建服务,注册服务 ,
(1)创建服务:MyIntentService.java
public class MyTestIntentService extends IntentService {
//TODO 注意:必须提供无参数构造,不然清单文件中注册报错
public MyTestIntentService() {
super("MyTestIntentService");
}
//TODO 将所有耗时操作都在该方法中完成,相当于开启线程
@Override
protected void onHandleIntent( Intent intent) {
StringBuffer buffer = new StringBuffer();
try {
URL url = new URL("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if (connection.getResponseCode()==200){
InputStream inputStream = connection.getInputStream();
byte[] bytes = new byte[1024];
int len=0;
while((len=inputStream.read(bytes))!=-1){
buffer.append(new String(bytes,0,len));
}
//TODO 向Activity或Fragment发送json串
Intent intent1 = new Intent();
intent1.setAction("com.zhou.intentService");
Bundle bundle = new Bundle();
bundle.putString("json",buffer.toString());
intent1.putExtras(bundle);
sendBroadcast(intent1);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
(2)清单文件注册服务
<service
android:name=".Service.MyTestIntentService"
android:exported="false" />
(3)Activity代码
public class Main2Activity extends AppCompatActivity {
private MyReceiver myReceiver;
private Intent intent;
private TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textview = (TextView) findViewById(R.id.textview);
//动态注册广播
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.zhou.intentService");
myReceiver = new MyReceiver();
registerReceiver(this.myReceiver,intentFilter);
//注册广播
intent= new Intent(this, MyTestIntentService.class);
startService(intent);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myReceiver);
stopService(intent);
}
private class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("com.zhou.intentService".equals(intent.getAction())){
Bundle extras = intent.getExtras();
String string = extras.getString("json", "aaaaa");
// Toast.makeText(Main2Activity.this,string,Toast.LENGTH_SHORT).show();
textview.setText(string);
}
}
}
}
通知(类似于播放器)
(1)创建服务
public class MusicService extends Service {
public MusicService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Notification.Builder builder = new Notification.Builder(getApplicationContext());
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("这是一个标题");
builder.setContentText("这是一个内容");
Notification build = builder.build();
startForeground(110,build);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
stopForeground(true);
super.onDestroy();
}
}
(2)清单文件注册服务
<service
android:name=".Service.MusicService"
android:enabled="true"
android:exported="true" />
(3)Activity代码
public class MusicActivity extends AppCompatActivity {
private Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MusicActivity.this, MusicService.class);
startService(intent);
}
});
}
}