栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android 服务(Service)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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:



    

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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/270866.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号