码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2016-07-31 15:52:08      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

技术分享

 

技术分享

技术分享

技术分享

技术分享

技术分享

技术分享

  本文只讨论扩展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>

 

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

标签:

原文地址:http://www.cnblogs.com/yihujiu/p/5722964.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!