首页 > 其他分享 >我有一壶酒 Android学习之Service(1)--->BinderService方式

我有一壶酒 Android学习之Service(1)--->BinderService方式

时间:2023-02-23 14:45:19浏览次数:48  
标签:Service void private --- BinderService import android public

 

  本文只讨论扩展Binder类

 

 

 创建一个Binder.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button
        android:id="@+id/btnStartBinderService"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start BinderService"
        />
    <Button
        android:id="@+id/btnStopBinderService"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Stop BinderService"
        />
</LinearLayout>
复制代码

 

创建一个BinderService.jvaa类,继承Service

复制代码
package com.szy.service;


import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class BinderService extends Service
{
    private static final String TAG = "BinderService";
    private MyBinder binder =new MyBinder();
    
    public class MyBinder extends Binder
    {
        public BinderService getService()
        {
            return BinderService.this;
        }
    }
    @Override
    public IBinder onBind(Intent intent)
    {
        return binder;
    }
    
    public void MyMethod()
    {
        Log.i(TAG, "MyMethod()");
    }

}
复制代码

 

再新建一个类BinderActivity.java继承Activity

复制代码
package com.szy.service;

import com.szy.service.BinderService.MyBinder;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class BinderActivity extends Activity
{
    private Button btnStartBinderService;
    private Button btnStopBinderService;
    
    private Boolean isConnected = false;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.binder);
        btnStartBinderService=(Button)findViewById(R.id.btnStartBinderService);
        btnStopBinderService=(Button)findViewById(R.id.btnStopBinderService);
        btnStartBinderService.setOnClickListener(listener);
        btnStopBinderService.setOnClickListener(listener);
    }
    
    private OnClickListener listener=new OnClickListener()
    {
    
        public void onClick(View v)
        {
            switch (v.getId())
            {
            case R.id.btnStartBinderService:
                bindService();
                break;
            case R.id.btnStopBinderService:
                unBind();
                break;
            default:
                break;
            }
        }

    };
    
    private void unBind()
    {
        if (isConnected)
        {
            unbindService(conn);
        }
    }

    private void bindService()
    {
        Intent intent=new Intent(BinderActivity.this, BinderService.class);
        bindService(intent, conn, Context.BIND_AUTO_CREATE);
    }
    
    private ServiceConnection conn=new ServiceConnection()
    {
        
        public void onServiceDisconnected(ComponentName name)
        {
            isConnected=false;
        }
        
    
        public void onServiceConnected(ComponentName name, IBinder binder)
        {
            MyBinder myBinder= (MyBinder)binder;
            BinderService service=myBinder.getService();
            service.MyMethod();
            isConnected=true;
            
        }
    };
}
复制代码

 

修改下AndroidManifest.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.szy.service"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
        </activity>
        
        <activity android:name=".BinderActivity"
                  android:label="@string/app_name">
        </activity>
        
        <activity android:name=".IntentActivity"
                  android:label="@string/app_name">
             <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <service android:name=".ExampleService" />
        <service android:name=".BinderService" />
        <service android:name=".MyService"/>
        <service android:name=".ExampleIntentService"/>
    </application>
</manifest>
复制代码

标签:Service,void,private,---,BinderService,import,android,public
From: https://www.cnblogs.com/kn-zheng/p/17147906.html

相关文章

  • Android学习之Service(1)--->Started方式
     界面退出后进程程序还在运行,不会被杀死,如音乐播发器、后台下载等  注:本文只讨论Started方式    main.xml代码分析<?xmlversion="1.......
  • windows 安装mysql-8.0.13(zip安装)
    安装环境说明系统版本:windows10mysql版本:mysql-8.0.13-winx64.zip下载地址:http://mirrors.163.com/mysql/Downloads/MySQL-8.0/mysql-8.0.13-winx64.zip解压安装......
  • python+playwright 学习-12.Mock 接口返回,模拟各种异常场景
    前言web自动化主要测前端UI的功能,有很多异常的场景,我们很难造真实的场景去触发,比如服务器异常时候,前端的提示语。这时候就可以使用mock功能,模拟接口的返回,测试前端的......
  • SpringBoot17 - 常用计量单位绑定
    常用计量单位绑定​ 在前面的配置中,我们书写了如下配置值,其中第三项超时时间timeout描述了服务器操作超时时间,当前值是-1表示永不超时。servers:ip-address:192.168......
  • SpringBoot18 - 校验
    校验​ 在书写时由于无法感知模型类中的数据类型,就会出现类型不匹配的问题,比如代码中需要int类型,配置中给了非法的数值,例如写一个“a",这种数据肯定无法有效的绑定,还会引......
  • SpringBoot19 - 数据类型转换
    数据类型转换​ 先把问题描述一下,这位开发者连接数据库正常操作,但是运行程序后显示的信息是密码错误。java.sql.SQLException:Accessdeniedforuser'root'@'localho......
  • SpringBoot14 - 热部署
    热部署​ 什么是热部署?简单说就是你程序改了,现在要重新启动服务器,嫌麻烦?不用重启,服务器会自己悄悄的把更新后的程序给重新加载一遍,这就是热部署。​ 热部署的功能是如......
  • 设计模式-模板方法-应用举例
    背景:设计一个任务系统,任务可以有多种类型(签到,看广告,填写调查问卷等等),每个任务的完成标准是可以做N次。需要记录每个任务的完成进度。每做一次需要做一些操作,如给用户......
  • SpringBoot15 - @ConfigurationProperties绑定属性
    @ConfigurationProperties为使用@Bean声明的第三方bean绑定属性​ 在基础篇学习了@ConfigurationProperties注解,此注解的作用是用来为bean绑定属性的。开发者可以在yml配......
  • WD-RDFMF21调频雷达物位计
    WD-RDFMF21调频雷达物位计测量介质:固体测量范围:0.3m~150m过程连接:法兰≥DN80过程温度:-40~110℃过程压力:-0.1~0.3MPa天线尺寸:78mm透镜天线+吹扫(或不带吹扫)天线材......