ANDROID_MARS學習筆記_S01原始版_017_綁定SERVICE

來源:互聯網
上載者:User

標籤:

一、代碼
1.xml
(1)activity_main.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     android:paddingBottom="@dimen/activity_vertical_margin" 6     android:paddingLeft="@dimen/activity_horizontal_margin" 7     android:paddingRight="@dimen/activity_horizontal_margin" 8     android:paddingTop="@dimen/activity_vertical_margin" 9     tools:context="com.service2.MainActivity" >10 11     <Button12         android:id="@+id/bindBtn"13         android:layout_width="wrap_content"14         android:layout_height="wrap_content"15         android:text="完成綁定操作" />16 17 </RelativeLayout>

 

(2)AndroidManifest.xml

 1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3     package="com.service2" 4     android:versionCode="1" 5     android:versionName="1.0" > 6  7     <uses-sdk 8         android:minSdkVersion="8" 9         android:targetSdkVersion="21" />10 11     <application12         android:allowBackup="true"13         android:icon="@drawable/ic_launcher"14         android:label="@string/app_name"15         android:theme="@style/AppTheme" >16         <activity17             android:name=".MainActivity"18             android:label="@string/app_name" >19             <intent-filter>20                 <action android:name="android.intent.action.MAIN" />21 22                 <category android:name="android.intent.category.LAUNCHER" />23             </intent-filter>24         </activity>25         <service android:name=".SecondService"></service>26     </application>27 28 </manifest>

 

2.java
(1)MainActivity.java

 1 package com.service2; 2  3 import android.app.Activity; 4 import android.content.ComponentName; 5 import android.content.Intent; 6 import android.content.ServiceConnection; 7 import android.os.Bundle; 8 import android.os.IBinder; 9 import android.view.View;10 import android.view.View.OnClickListener;11 import android.widget.Button;12 13 import com.service2.SecondService.FirstBinder;14 15 public class MainActivity extends Activity {16 17     private Button bindBtn = null;18     @Override19     protected void onCreate(Bundle savedInstanceState) {20         super.onCreate(savedInstanceState);21         setContentView(R.layout.activity_main);22         bindBtn = (Button)findViewById(R.id.bindBtn);23         bindBtn.setOnClickListener(new OnClickListener() {24             @Override25             public void onClick(View v) {26                 Intent intent = new Intent();27                 intent.setClass(MainActivity.this, SecondService.class);28                 bindService(intent, conn, BIND_AUTO_CREATE);29             }30         });31     }32     33     ServiceConnection conn = new ServiceConnection() {34         @Override35         public void onServiceDisconnected(ComponentName name) {36             System.out.println("onServiceDisconnected>>>>>>");37         }38         @Override39         public void onServiceConnected(ComponentName name, IBinder service) {40             FirstBinder binder = (FirstBinder)service;41             System.out.println("onServiceConnected>>>>>"+binder.getData());42         }43     };44 }

 

(2)SecondService.java

 1 package com.service2; 2  3 import android.app.Service; 4 import android.content.Intent; 5 import android.os.Binder; 6 import android.os.IBinder; 7  8 public class SecondService extends Service { 9 10     @Override11     public IBinder onBind(Intent intent) {12         IBinder binder = new FirstBinder();13         return binder;14     }15 16     class FirstBinder extends Binder {17         public String getData() {18             return "test data";19         }20     }21 }

 

ANDROID_MARS學習筆記_S01原始版_017_綁定SERVICE

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.