今天来谈一下服务。停,别胡思乱想,不是你想象的那种服务。。。。
首先新建一个项目,命名为ServiceDemo。
在项目名下新建一个类,命名为MyService让他继承Service。
1.项目结构
2.代码块
mainactivity:
package com.glsite.servicedemo;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private Intent mIntent;
private ServiceConnection mConnection= new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
mService = ((MyService.MyBinder) service).getService();
System.out.println("service调用成功");
mService.execute();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mService = null;
}
};
private MyService mService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIntent = new Intent(this, MyService.class);
// startService(mIntent);
bindService(mIntent,mConnection, Context.BIND_AUTO_CREATE);
}
public void stop(View view) {
if (mIntent != null) {
stopService(mIntent);
}
}
public void unbind(View view) {
unbindService(mConnection);
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(mConnection);
}
}
myservice:
package com.glsite.servicedemo;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
/**
* @author glsite.com
* @version $Rev$
* @des ${TODO}
* @updateAuthor $Author$
* @updateDes ${TODO}
*/
public class MyService extends Service {
private IBinder mBinder = new MyBinder();
public MyService() {
System.out.println("maservice constructor");
}
@Override
public IBinder onBind(Intent intent) {
System.out.println("onbind");
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
System.out.println("创建了");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//zhe这一句必须写在return的上面,否则访问不到
System.out.println("饿呢恩");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("销毁了");
}
@Override
public boolean onUnbind(Intent intent) {
System.out.println("解绑");
return super.onUnbind(intent);
}
public class MyBinder extends Binder{
MyService getService(){
return MyService.this;
}
}
public void execute(){
System.out.println("通过Binder得到severe的引用来调用service内部的方法");
}
}
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="stop"
android:text="停止服务"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="96dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="解绑服务"
android:onClick="unbind"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2" />
</android.support.constraint.ConstraintLayout>