是中一个类,它是Android四大组件之一,使用Service可以在后台执行长时间的操作( perform long-running operations in the background ),Service并不与用户产生UI交互。其他的应用组件可以启动Service,即便用户切换了其他应用,启动的Service仍可在后台运行。一个组件可以与Service绑定并与之交互,甚至是跨进程通信(IPC)。例如,一个Service可以在后台执行网络请求、播放音乐、执行文件读写操作或者与 content provider交互 等。
ps:附上一个service组件介绍的网站:
下面是一个音乐后台播放的例子:
1.Activity类
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; /** * Created by cj on 2018/3/28. */ public class ServiceDemo extends Activity implements View.OnClickListener{ private static final String TAG = "ServiceDemo"; Button buttonStart, buttonStop; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.servicedemo); 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.i(TAG, "onClick: starting service"); startService(new Intent(this, MyService.class)); break; case R.id.buttonStop: Log.i(TAG, "onClick: stopping service"); stopService(new Intent(this, MyService.class)); break; } } } 2.Service子类:
import android.app.Notification; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.Build; import android.os.Debug; import android.os.IBinder; import android.support.annotation.Nullable; import android.util.Log; import android.widget.Toast; /** * Created by cj on 2018/3/28. */ public class MyService extends Service { private static final String TAG = "MyService"; private final static int GRAY_SERVICE_ID = 1001; MediaPlayer player; @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { Toast.makeText(this,"MyService Created",Toast.LENGTH_SHORT).show(); Log.i(TAG,"oncreated"); player = MediaPlayer.create(this,R.raw.huanghun); //此处的R.raw.huanghun为要播放的音乐资源。在res文件夹下创建raw目录,huanghun.mp3为音乐资源,放到创建好的raw文件夹下 player.setLooping(false); } @Override public int onStartCommand(Intent intent, int flags, int startId) { //sdk小于18时启动一个状态栏通知 if(Build.VERSION.SDK_INT<18){ startForeground(GRAY_SERVICE_ID,new Notification()); }else{ //sdk大于18 时启动一个状态栏通知 Intent intent1 = new Intent(this,GrayInnerService.class); startService(intent1); startForeground(GRAY_SERVICE_ID,new Notification()); } return super.onStartCommand(intent, flags, startId); } //给API >= 18 的平台上做灰色保护手段 public class GrayInnerService extends Service { @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { startForeground(GRAY_SERVICE_ID, new Notification()); stopForeground(true); stopSelf(); return super.onStartCommand(intent, flags, startId); } } @Override public void onDestroy() { Toast.makeText(this, "My Service Stoped", Toast.LENGTH_LONG).show(); Log.i(TAG, "onDestroy"); player.stop(); } @Override public void onStart(Intent intent, int startId) { Toast.makeText(this, "My Service Start", Toast.LENGTH_LONG).show(); Log.i(TAG, "onStart"); player.start(); } } 3.布局文件
4.String.xml文件
5.manifest.xml文件中配置 update //此处为运用程序的名称,一般项目创建时自带播放 停止 Service Demo
//此处配置,name为service的子类。