package com.example.service_binder;
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.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* @author HD
* @date 2015-11-26
* @package_name com.example.service_binder
* @file_name MainActivity.java
*/
public class MainActivity extends Activity {
private Button mButton;
private TextView mTextView;
boolean mIsbound = false;
MyService myservice;
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("hhhd", "onServiceDisconnected");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("hhhd", "onServiceConnected");
myservice = ((MyService.MyBinder) service).getService();
int result = myservice.getResult(55, 22);
int receive_msg = myservice.sendMsg;
mTextView.setText(result+""+receive_msg);
mIsbound = true;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.button1);
mTextView = (TextView) findViewById(R.id.textView1);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doBindService();
}
});
}
void doBindService(){
Intent intent = new Intent();
intent.setClass(MainActivity.this, MyService.class);
bindService(intent, conn, Context.BIND_AUTO_CREATE);
}
void doUnbindService(){
if(mIsbound){
if(myservice != null){
unbindService(conn);
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
doUnbindService();
}
}
package com.example.service_binder;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
/**
* @author HD
* @date 2015-11-26
* @package_name com.example.service_binder
* @file_name MyService.java
*/
public class MyService extends Service {
int sendMsg = 2;
MyBinder mYBinder = new MyBinder();
public class MyBinder extends Binder{
MyService getService(){
return MyService.this;
}
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
return mYBinder;
}
@Override
public void onDestroy() {
super.onDestroy();
}
int getResult(int arg1,int arg2){
int result = arg1+arg2;
return result;
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.service_binder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<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=".MyService"></service>
</application>
</manifest>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.service_binder.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="138dp"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
</RelativeLayout>