一。
如图:
清单文件里注册服务
1.创建一个接口,写一个抽象方法
2.创建一个继承Service的类,写一个返回图中所示名的方法
3.再写一个内部类继承Binder并实现步骤1建的接口,实现的抽象方法里返回步骤2中写的方法
4.onBind方法里返回的是new 内部类名();
5.主逻辑代码文件中,创建一个内部类实现ServiceConnection接口
6. 定义一个接口类型变量,onServiceConnected方法里写:myinterface=(MyInterface) service;
7.onCreate方法里写Intent,然后绑定服务: bindService(intent, conn, Context.BIND_AUTO_CREATE);
8.按钮事件里直接调用接口里方法
逻辑代码:
<span style="font-size:18px;">package com.example.day23_aidservice;
import com.example.day23_aidservice.MyService.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.widget.Toast;
public class MainActivity extends Activity {
MyBinder binder;
MyInterface mi;
MyConn conn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent=new Intent();
intent.setAction("aa.bb.cc");
conn=new MyConn();
bindService(intent, conn, Context.BIND_AUTO_CREATE);
}
public void click(View v)
{
//String name1=binder.aaa();
String name1=mi.aaa();
Toast.makeText(getApplicationContext(), name1, 0).show();
System.out.println("===>"+name1);
}
class MyConn implements ServiceConnection
{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//binder=(MyBinder) service;
mi=(MyInterface) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
}
</span>
布局文件:
<span style="font-size:18px;"><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=".MainActivity" >
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="接口调服务里方法" />
</RelativeLayout>
</span>
接口:
<span style="font-size:18px;">package com.example.day23_aidservice;
public interface MyInterface {
public String aaa();
}
</span>
service类:
<span style="font-size:18px;">package com.example.day23_aidservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class MyService extends Service{
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
public class MyBinder extends Binder implements MyInterface
{
@Override
public String aaa() {
return name();
}
}
public String name()
{
return "zhaolinger";
}
}
</span>
二。
效果跟一一样,代码也差不多,改动几处
1.创建一个接口,写一个抽象方法
2.去工作环境找到项目,然后把接口的后缀名改为aidl
3..创建一个继承Service的类,写一个返回图中所示名的方法
4.再写一个内部类继承步骤1建的接口名.Stub,实现的抽象方法里返回步骤2中写的方法
5.onBind方法里返回的是new 内部类名();
6.主逻辑代码文件中,创建一个内部类实现ServiceConnection接口
7. 定义一个接口类型变量,onServiceConnected方法里写: myinterface=MyInterface.Stub.asInterface(service);
8.onCreate方法里写Intent,然后绑定服务: bindService(intent, conn, Context.BIND_AUTO_CREATE);
接口文件(写完再改后缀名):
<span style="font-size:18px;">package com.example.day23_aidservice2;
interface MyInterface {
String aaa();
}
</span>
逻辑代码文件:
<span style="font-size:18px;">package com.example.day23_aidservice2;
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.os.RemoteException;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
MyConn conn;
MyInterface minte;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent=new Intent("hh.pp");
conn=new MyConn();
bindService(intent, conn, Context.BIND_AUTO_CREATE);
}
public void click(View v)
{
try
{
String name= minte.aaa();
Toast.makeText(getApplicationContext(), name, 0).show();
}
catch (RemoteException e)
{
e.printStackTrace();
}
}
class MyConn implements ServiceConnection
{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
minte=MyInterface.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
}
</span>
布局文件:
<span style="font-size:18px;"><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=".MainActivity" >
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AID"
android:onClick="click"/>
</RelativeLayout>
</span>
服务类文件:
<span style="font-size:18px;">package com.example.day23_aidservice2;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class MyService extends Service{
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
class MyBinder extends MyInterface.Stub
{
@Override
public String aaa() {
return name();
}
}
public String name()
{
return "shanxuyang";
}
}
</span>