使用IntentService的作用:
为了解决一些程序员忘记开启线程,或者忘记调用stopSelf()方法。实现简单的创建一个异步的,会自动停止的服务。
实现时要提供无参的构造函数,在其内部调用父类的有参的构造函数。
在inHandleIntent()中实现一些具体的逻辑。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/start_service"
android:text="Start Service"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/stop_service"
android:text="Stop service"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bind_service"
android:text="Bind Service"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/UnBind_service"
android:text="UnBind service"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/start_intent_service"
android:text="start_intent_service"/>
</LinearLayout>
package com.example.servicetest;
import android.app.IntentService;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button startService ;
private Button stopService ;
private Button bindService ;
private Button unBindService ;
private MyService.DownloadBinder downloadBinder ;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) { //绑定服务时调用
downloadBinder = (MyService.DownloadBinder) service ;//调用了服务里的方法,实现控制服务
downloadBinder.startDownload();
downloadBinder.getProgress() ;
}
@Override
public void onServiceDisconnected(ComponentName name) {//解除绑定事调用
}
};
private Button startIntentService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService = (Button)findViewById(R.id.start_service);
stopService = (Button)findViewById(R.id.stop_service);
bindService = (Button) findViewById(R.id.bind_service);
unBindService = (Button) findViewById(R.id.UnBind_service);
startIntentService = (Button) findViewById(R.id.start_intent_service);
startService.setOnClickListener(this);
stopService.setOnClickListener(this);
bindService.setOnClickListener(this);
unBindService.setOnClickListener(this);
startIntentService.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.start_service:
Intent startIntent = new Intent(this , MyService.class);
startService(startIntent);//启动服务
break;
case R.id.stop_service:
Intent stopIntent = new Intent(this,MyService.class);
stopService(stopIntent);//停止服务
break;
case R.id.bind_service:
Intent bindIntent = new Intent(this , MyService.class);
bindService(bindIntent,connection , BIND_AUTO_CREATE);//绑定服务 第三个参数表示活动和服务绑定后自动创建服务 onCreate()会执行 但onStartCommand()
//不会执行.
break;
case R.id.UnBind_service:
// Intent stopIntent1 = new Intent(this,MyService.class);
// 我们可能对一个服务及调用了startService(),又调用了bindService(),
// 这时需要同时调用stopService()和unbindService()方法,onDestory()才会执行
// stopService(stopIntent1);//停止服务
unbindService(connection);//解绑服务
break;
case R.id.start_intent_service:
Log.d("MainActivity","The Thread is " + Thread.currentThread().getId());
Intent intentService = new Intent(this , MyIntentService.class);
startService(intentService);
break;
default:
break;
}
}
}
package com.example.servicetest;
import android.app.DownloadManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
public class MyService extends Service {
private DownloadBinder mBinder = new DownloadBinder();
class DownloadBinder extends Binder {
public void startDownload(){
Log.d("Myservice" , "--startDownload");
}
public int getProgress(){
Log.d("Myservice" , "--getProgress executed");
return 0;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent){
return mBinder;
}
@Override
public void onCreate() { //创建服务时调用
Notification.Builder builder = new Notification.Builder(MyService.this);
builder.setTicker("This is a trcker");//通知栏的预览文字
builder.setSmallIcon(R.mipmap.ic_launcher);//图标
builder.setContentTitle("This is title");//通知的标题
builder.setContentText("This is content");//通知的内容
builder.setWhen(System.currentTimeMillis());//设置通知时间为当前系统时间
Notification notification = builder.build();//获取Notification的实例
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
startForeground(1, notification);
Log.d("Myservice" , "--OnCreate executed");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) { //每次开启服务时调用
Log.d("Myservice" , "--onStartCommand executed");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() { //销毁服务时调用
Log.d("Myservice" , "--onDestroy executed");
super.onDestroy();
}
}
package com.example.servicetest;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
public class MyIntentService extends IntentService {
public MyIntentService(){
super("MyIntentService");//调用父类的有参构造函数
}
protected void onHandleIntent(Intent intent){
Log.d("MyIntentService","The Thread is " + Thread.currentThread().getId());
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("MyIntentService","onDestory executed ");
}
}
<service android:name=".MyService">
</service>
<service android:name=".MyIntentService">
</service>