Project——建立能与服务通信的应用与服务的生命周期

本文介绍了一个Android应用中通过服务组件实现的数据通信案例。主要讲解了如何定义服务、创建Binder接口及其实现,并在Activity中绑定服务进行数据交互的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="cn.itcast.who"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  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=".WhoService"/>
    </application>
    <uses-sdk android:minSdkVersion="8" />

</manifest>

2、

package cn.itcast.who;

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

public class MainActivity extends Activity {
    private IWhoService binder;
    private TextView resultView;
    private ServiceConnection conn = new WhoServiceConnection();
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        resultView = (TextView)this.findViewById(R.id.result);
      
     Intent service = new Intent(MainActivity.this, WhoService.class);
  bindService(service, conn, BIND_AUTO_CREATE);
        Button button = (Button)this.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {   
   @Override
   public void onClick(View v) {
    resultView.setText(binder.who(0));
   }
  });
    }
   
    private final class WhoServiceConnection implements ServiceConnection{
  @Override
  public void onServiceConnected(ComponentName name, IBinder service) {
   binder = (IWhoService)service;
  }

  @Override
  public void onServiceDisconnected(ComponentName name) {
   binder = null;
  }
    }

 @Override
 protected void onDestroy() {
  if(conn!=null && binder != null){
   unbindService(conn);
  }
  super.onDestroy();
 }
   
   
}

3、

package cn.itcast.who;

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

public class WhoService extends Service {
 private IBinder binder = new WhoBinder();
 private String[] names = {"孙晓芳", "刘俊汀"};

 public String getName(int id){
  if(id<0 || id>1) return null;
  return names[id];
 }
 
 @Override
 public IBinder onBind(Intent intent) {
  return binder;
 }
 
 private final class WhoBinder extends Binder implements IWhoService{
  @Override
  public String who(int id) {
   return getName(id);
  }  
 }

}

4、

package cn.itcast.who;

public interface IWhoService {
 public String who(int id);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值