androidvncserver下载_android bindservice

androidvncserver下载_android bindservice关于安卓BindService绑定问题BindService主要用于当用户想要将活动activity与service进行绑定,当activity结束时(调用者)service也跟着一起结束了;而startService()只能用来启动服务,当服务启动后,就与前台活动没有了关系;当我们要从service外部获得service中的方法内容时,我们必须要Ibinder返回的service对象

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

关于安卓BindService 绑定问题
BindService 主要用于当用户想要将活动activity与service进行绑定,当activity结束时(调用者)service也跟着一起结束了;而startService()只能用来启动服务,当服务启动后,就与前台活动没有了关系;
当我们要从service外部获得service中的方法内容时,我们必须要Ibinder返回的service对象才能操作;所以我们必须利用bindservice的方式去绑定服务获取binder对象,利用binder的方式去获取service的方法内容;


使用bindservice的具体过程如下:
1)创建意图:Intent intent = new Intent(MainActivity.this,BindService.class);
2)创建bindservice对象:bindService(intent,conn,BIND_AUTO_CREATE);
3)创建内部类MyBinder、新建mybinder对象:
private  MyBinder myBinder = new MyBinder();
    class MyBinder extends Binder{
        public BindService getService(){
            return BindService.this;
        }
    }
4)onBind()返回mybinder对象:
public IBinder onBind(Intent intent) {
        return myBinder;
    }
5)通过serviceconnection获取Binder对象进一步来获取service对象,最后调用service中的方法:
ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            BindService.MyBinder binder = (BindService.MyBinder)service;
            BindService bindService = binder.getService();
            bindService.print();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

示例代码:
MainActivity
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initEvent();
    }
    ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            BindService.MyBinder binder = (BindService.MyBinder)service;
            BindService bindService = binder.getService();
            bindService.print();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    private void initEvent() {
        findViewById(R.id.button4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,BindService.class);
                bindService(intent,conn,BIND_AUTO_CREATE);
            }
        });
        findViewById(R.id.button5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unbindService(conn);
            }
        });
    }
}
BindService
public class BindService extends Service {
    
    public void print(){
        Log.d(TAG, "print: BindService");
    }
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate");
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Log.d(TAG, "onStart");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand");
        return super.onStartCommand(intent, flags, startId);
        
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(TAG, "onUnbind:");
        return super.onUnbind(intent);
    }

    @Override
    public void onRebind(Intent intent) {
        Log.d(TAG, "onRebind");
        super.onRebind(intent);
    }

    private  MyBinder myBinder = new MyBinder();
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return myBinder;
    }
    class MyBinder extends Binder{
        public BindService getService(){
            return BindService.this;
        }
    }
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/193749.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • SpringBoot——SpringBoot整合RabbitMQ(下)

    SpringBoot——SpringBoot整合RabbitMQ(下)SpringBoot——SpringBoot整合RabbitMQ(下)

  • select2 使用教程(简)「建议收藏」

    select2 使用教程(简)「建议收藏」用了这么久的Select2插件,也该写篇文章总结总结。当初感觉Select2不是特别好用,但又找不到比它更好的下拉框插件。在我的印象里Select2有2个版本,最新版本有一些新的特性,并且更新了一下方法参数,比最初版本要好看一些,本文针对新版本。官网:http://select2.github.io/演示:请选择由于博客系统的原因,所以只能演示简单

  • 阿里云URL转发类问题排查[通俗易懂]

    阿里云URL转发类问题排查[通俗易懂]概念URL转发包含URL隐性转发和URL显性转发,主要是指将一个域名指向另外一个已经存在的站点时,可以通过URL转发实现。隐性URL转发是用的是iframe框架技术,显性URL转发采用的是301(也称之为永久性转移)或302重定向技术(也称之为暂时性转移)。301和302说明301redirect:301代表永久性转移(PermanentlyMoved)302redirect:302代表暂时性转移(TemporarilyMoved)共同点:301和302状态码都表示重定向,当浏览

    2022年10月19日
  • Windows Azure Platform 系列文章,第二章 使用Azure PowerShell 修改Windows服务器登录帐号密码…

    Windows Azure Platform 系列文章,第二章 使用Azure PowerShell 修改Windows服务器登录帐号密码…

  • Drupal开发学习入门指引[通俗易懂]

    Drupal开发学习入门指引[通俗易懂]我是云客,《云客drupal8源码分析》系列的作者,伴随着drupal8第一个正式版本发布到现在,该系列已经发布100期,超过60万字,作为中国投入精力最大、同时也是时间最早的第一批用户之一,云客觉得有义务帮助初学者准备好行囊,叮嘱好一路上的注意事项,之前也零星写过一些入门方面的资料,会在本文末给出链接(这些资料也很值得参考),本篇将为您全面整理需要的一切和规划学习步骤,这里假定您是一个刚刚学会p…

  • 几个国外SPS技术网站

    几个国外SPS技术网站http://www.tech-archive.net/Archive/SharePoint/microsoft.public.sharepoint.portalserver.development/http://weblogs.asp.net/autocrat/archive/2004/11/10/254825.aspxhttp://www.mev.com/modules/lists/msft/…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号