zoukankan      html  css  js  c++  java
  • 通过bind实现activity与service的交互

    先点bind按钮实现onCreate,在点击start给service传值(get方法)

    xml:

     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     tools:context="com.zzw.bind.MainActivity" >
     6 
     7     <Button
     8         android:id="@+id/start"
     9         android:layout_width="wrap_content"
    10         android:layout_height="wrap_content"
    11         android:layout_alignParentTop="true"
    12         android:layout_centerHorizontal="true"
    13         android:layout_marginTop="71dp"
    14         android:text="start" />
    15 
    16     <Button
    17         android:id="@+id/bind"
    18         android:layout_width="wrap_content"
    19         android:layout_height="wrap_content"
    20         android:layout_alignLeft="@+id/start"
    21         android:layout_below="@+id/start"
    22         android:layout_marginTop="65dp"
    23         android:text="bind" />
    24 
    25     <Button
    26         android:id="@+id/dedao"
    27         android:layout_width="wrap_content"
    28         android:layout_height="wrap_content"
    29         android:layout_alignLeft="@+id/bind"
    30         android:layout_alignParentBottom="true"
    31         android:layout_marginBottom="80dp"
    32         android:text="取值" />
    33 
    34 </RelativeLayout>
    布局

    记住注册service

    MainActivity:

     1 package com.zzw.bind;
     2 
     3 import com.zzw.bind.MyAppService.MyBinder;
     4 
     5 import android.app.Activity;
     6 import android.app.Service;
     7 import android.content.ComponentName;
     8 import android.content.Intent;
     9 import android.content.ServiceConnection;
    10 import android.os.Bundle;
    11 import android.os.IBinder;
    12 import android.util.Log;
    13 import android.view.View;
    14 import android.view.View.OnClickListener;
    15 
    16 public class MainActivity extends Activity {
    17     private ServiceConnection sc;
    18     private MyAppService myAppService;
    19 
    20     @Override
    21     protected void onCreate(Bundle savedInstanceState) {
    22         super.onCreate(savedInstanceState);
    23         setContentView(R.layout.activity_main);
    24 
    25         sc = new ServiceConnection() {
    26             @Override
    27             public void onServiceDisconnected(ComponentName name) {
    28 
    29             }
    30 
    31             @Override
    32             public void onServiceConnected(ComponentName name, IBinder service) {
    33                 MyBinder mBinder = (MyBinder) service;
    34                 myAppService = mBinder.getService();
    35             }
    36         };
    37         findViewById(R.id.start).setOnClickListener(new OnClickListener() {
    38 
    39             @Override
    40             public void onClick(View v) {
    41                 startService();
    42             }
    43         });
    44         findViewById(R.id.bind).setOnClickListener(new OnClickListener() {
    45 
    46             @Override
    47             public void onClick(View v) {
    48                 bindService();
    49             }
    50         });
    51         findViewById(R.id.dedao).setOnClickListener(new OnClickListener() {
    52 
    53             @Override
    54             public void onClick(View v) {
    55                 String str = myAppService.getServiceValue();
    56                 Log.d("得到的值", str);
    57             }
    58         });
    59 
    60     }
    61 
    62     private void startService() {
    63         myAppService.setServiceValue("hello");
    64 
    65         Intent intent = new Intent(this, MyAppService.class);
    66         startService(intent);
    67     }
    68 
    69     private void bindService() {
    70         Intent intent = new Intent(this, MyAppService.class);
    71         bindService(intent, sc, Service.BIND_AUTO_CREATE);
    72     }
    73 
    74     @Override
    75     protected void onDestroy() {
    76         super.onDestroy();
    77         Intent intent = new Intent(this, MyAppService.class);
    78         stopService(intent);
    79         Log.d("MainActivity", "onDestroy");
    80     }
    81 
    82 }

    Service:

     1 package com.zzw.bind;
     2 
     3 import android.app.Service;
     4 import android.content.Intent;
     5 import android.os.Binder;
     6 import android.os.IBinder;
     7 import android.util.Log;
     8 
     9 public class MyAppService extends Service {
    10     private String str;
    11 
    12     @Override
    13     public void onCreate() {
    14         super.onCreate();
    15         Log.d("MyAppService","onCreate");
    16     }
    17 
    18     @Override
    19     public int onStartCommand(Intent intent, int flags, int startId) {
    20         this.str=str+",world";
    21         Log.d("onStartCommand",str);
    22         return super.onStartCommand(intent, flags, startId);
    23     }
    24 
    25     public String getServiceValue() {
    26         Log.d("getServiceValue", str);
    27         return str;
    28     }
    29 
    30     public void setServiceValue(String str) {
    31         Log.d("setServiceValue", str);
    32         this.str = str;
    33     }
    34 
    35     @Override
    36     public IBinder onBind(Intent intent) {
    37         Log.d("onBind", "======");
    38         return new MyBinder();
    39     }
    40 
    41     public class MyBinder extends Binder {
    42         public MyAppService getService() {
    43             Log.d("getService", "====");
    44             return MyAppService.this;
    45         }
    46     }
    47 
    48     @Override
    49     public void onDestroy() {
    50         Log.d("MyAppService", "onDestroy");
    51         super.onDestroy();
    52     }
    53 
    54     @Override
    55     public boolean onUnbind(Intent intent) {
    56         Log.d("MyAppService", "onUnbind");
    57         return super.onUnbind(intent);
    58     }
    59     
    60 }
  • 相关阅读:
    angularJS的学习资源,巨全
    JSON语法
    handlerbars入门学习
    js中局部变量和全局变量的易混点
    js中判断一个变量是否为数字类型的疑问
    js中的类型转换方法
    使用sn.exe为程序集签名
    探究Entity Framework如何在多个仓储层实例之间工作单元的实现及原理(2018-05-31、2019-08-16修改部分严重错误代码)
    Fiddler4无法抓取HttpWebRequest本地请求的解决办法
    JS判断时特殊值与boolean类型的转换
  • 原文地址:https://www.cnblogs.com/zzw1994/p/4950283.html
Copyright © 2011-2022 走看看