Android 后台服务(Service)

声明:各个方法的用法都在代码的注释里:可以自行观看

Service的代码:

public class MyService extends Service {
    
    
    MyBinder myBinder = new MyBinder();
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
    
    
        //放回的Ibinder是一个接口,放回Binder对象(继承了IBinder)也可以
        //返回的Binder会传入到ServiceConnection的重写方法onServiceConnected中
        Log.d("MyService" , "onBind: ");
        return myBinder;
    }

    @Override
    public void onRebind(Intent intent) {
    
    
        super.onRebind(intent);
        Log.d("MyService" , "onRebind: ");
        //当onUnbind的返回值为true时,与该服务绑定的活动离开视线后再重新回到视线后,重新bindService时会调用
    }

    @Override
    public boolean onUnbind(Intent intent) {
    
    
        Log.d("MyService" , "onUnbind: ");
        return true;

    }



    @Override
    public void onCreate() {
    
    
        //在onBind或onStartCommand前调用
        super.onCreate();
        Log.d("MyService" , "onCreat: ");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    
    
        Log.d("MyService" , "onStatrCommand: ");
        return super.onStartCommand(intent, flags, startId);
        //startService时调用

    }

    @Override
    public void onDestroy() {
    
    
        super.onDestroy();
        Log.d("MyService" , "onDestory: ");
        //stopService时调用
    }
}

layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical">
    <Button
        android:onClick="sendService"
        android:id="@+id/bt1"
        android:text="sendService"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:onClick="stopService"
        android:id="@+id/bt2"
        android:text="stopService"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:onClick="bindService"
        android:id="@+id/bt3"
        android:text="bindService"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:onClick="unbindService"
        android:id="@+id/bt4"
        android:text="unbindService"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:onClick="sendIntentService"
        android:id="@+id/bt5"
        android:text="sendIntentService"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>



</LinearLayout>

IntentService:解决耗时工作–在子线程中运行,结束会自动回调ondestory()


//为了实现多线程操作一些耗时的时候忘记stopService时的IntentService
//该类的功能时实现一些耗时工作后并自动关闭服务
public class MyIntentService extends IntentService {
    
    
    //一定要有无参构造函数,且要super一下父类的带参构造函数
    public MyIntentService() {
    
    
        super("MyIntentService");
    }


    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
    
    
        //此方法在子线程中运行,可以处理一下耗时的工作,具体逻辑自己写
        Log.d("MyIntentService", "onHandleIntent: ");
    }

    //onHandleIntent执行结束后会自动调用onDestory()
    @Override
    public void onDestroy() {
    
    
        super.onDestroy();
        Log.d("MyIntentService", "onDestroy: ");
    }
}

Binder:


public class MyBinder extends Binder {
    
    
    //在绑定的时候充当onBind的返回类型
    public void startDownload(){
    
    
        Log.d("MyService", "startDownload: ");
    }
    public void getProgress(){
    
    
        Log.d("MyService", "getProgress: ");
    }
}

主函数:

public class MainActivity extends AppCompatActivity {
    
    
     MyBinder myBinder;
     Intent intent;
     Intent intentService;
     ServiceConnection serviceConnection = new ServiceConnection(){
    
    
         @Override
         public void onServiceConnected(ComponentName name, IBinder service) {
    
    
             Log.d("MyService", "onServiceConnected: ");
             myBinder = (MyBinder) service;
             myBinder.getProgress();
             myBinder.startDownload();
         }

         @Override
         public void onServiceDisconnected(ComponentName name) {
    
    
             Log.d("MyService", "onServiceDisconnected: ");
         }
     };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intent = new Intent(this, MyService.class);
        intentService = new Intent(this,MyIntentService.class);
    }

    public void sendService(View view) {
    
    
        startService(intent);
    }

    public void stopService(View view) {
    
    
        stopService(intent);
    }

    public void bindService(View view) {
    
    
        bindService(intent,serviceConnection,0);
    }

    public void unbindService(View view) {
    
    
        unbindService(serviceConnection);
    }

    public void sendIntentService(View view) {
    
    

        startService(intentService);
    }
}

注:MyService中的onBind方法的返回值IBinder会给ServiceConnection中的onServiceConnected()方法,可以利用该对象实现IBinder里的方法

附件:onRebind什么时候调用–>转载自https://blog.csdn.net/fenggering/article/details/53116311

猜你喜欢

转载自blog.csdn.net/XJ200012/article/details/120514457