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

本文只讨论扩展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>
时间: 2024-08-26 10:35:39

我有一壶酒 Android学习之Service(1)--->BinderService方式的相关文章

Android学习之Service(1)---&gt;Started方式

界面退出后进程程序还在运行,不会被杀死,如音乐播发器.后台下载等 注:本文只讨论Started方式 main.xml代码分析 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"

Android学习总结——Service组件

从Service的启动方式上,可以将Service分为Started Service和Bound Service.在使用Service时,要想系统能够找到此自定义Service,无论哪种类型,都需要在AndroidManifest.xml中声明: <service android:name=".MyService"> 一:StartService方式启动服务 Started Service相对比较简单,通过context.startService(Intent servic

Android 学习笔记 Service服务与远程通信...(AIDL)

PS:这一章节看的我有几分迷茫,不是很容易理解...不过还好总算是明白了一大半了...基本的迷惑是解决了... 学习内容: 1.跨应用启动服务... 2.跨应用绑定服务... 3.跨应用实现通信... 由于5.0版本之前和5.0版本之后是有很大的区别的,因此我都会在这里进行具体的介绍... 1.跨应用启动服务...   跨应用启动服务,其实就是多个应用程序之间产生一种沟通关系,应用程序之间可以进行通信或者是完成一些其他的互动,总而言之,就是在我本身的应用程序中去启动其他应用程序的某个服务,这就完

android学习笔记 Service

Service(服务): 长期后台运行的没有界面的组件 android应用什么地方需要用到服务? 天气预报:后台的连接服务器的逻辑,每隔一段时间获取最新的天气信息.股票显示:后台的连接服务器的逻辑,每隔一段时间获取最新的股票信息.mp3播放器: 后台长期的播放音乐. ---------------------------------------------------------------------------------new Thread(){}.start(); 子线程没有界面,也是长

Android学习之Http使用Post方式进行数据提交

转自:http://blog.csdn.net/wulianghuan/article/details/8626551 我们知道通过Get方式提交的数据是作为Url地址的一部分进行提交,而且对字节数的长度也有限制,与Get方式类似,http-post参数也是被URL编码的,然而它的变量名和变量值不作为URL的一部分被传送,而是放在实际的HTTP请求消息内部被传送. 可以通过如下的代码设置POST提交方式参数: [html] view plaincopyprint? HttpURLConnecti

Android——Activity以及Service的启动方式

1. Activity共有四种启动方式(android:launchMode) (1) standard:默认方式,不用再次配置 standart模式就是一个栈的模式,默认每次跳转到一个新的Activity都会新建一个实例,不管这个实例是否存在.每次跳转就新建,压栈,每次返回就pop弹栈. (2) singleTop:为<activity>指定属性android:launchMode="singleTop",栈顶重复使用模式. 每次判断栈顶有没有,没有,创建:有,继续使用.

Android 学习资料分享(2015 版)

我是如何自学Android,资料分享(2015 版) Tikitoo2015.02.11 10:21 1713 字 3932 次阅读 自己学了两三个月的Android,最近花了一周左右的时间写了个App--Diigoer(已开源),又花了一两周时间找工作,收到了两个Offer,也算是对自己学习的一种认可吧:我刚开始学习总结的--<我是如何自学Android,资料分享>,如果是初学Android 的话,不应该错过的,而今天这篇分享好这篇文章,相对于第一次写的会有所提升,所以建议先把上一篇看了,再

android学习十八(Service服务的基本用法)

定义一个服务 在项目中定义一个服务,新建一个ServiceTest项目,然后在这个项目中新增一个名为MyService的类,并让它继承自Service,完成后的代码如下所示: package com.jack.servicetest; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class MyService extends Service { @Over

android学习路线:如何成长为高级工程师

博主参加了2014 CSDN博客之星评选,帮我投一票吧.点击给我投票前言之所以写这篇文章,是因为最近博客和我的开发群( 215680213 )中,不少小伙伴都让我讲讲android学习路线,所以我决定写一篇博客,来说明这个问题.既然有不少小伙伴来咨询这个问题,说明大家都还是想快速地提高技术,毕竟技术的提高是职业发展中不可或缺的一步,技术好了,才能得到认可,才能升职加薪.一般来说,快速提高技术是不太容易的,但是通过正确的方法加上辛勤的汗水是可以缩短这个过程的.或许有人会问,你凭什么来写这个学习路线