版权声明:有问题可联系博主QQ:15577969,大家一起相互交流和学习。 https://blog.csdn.net/qq15577969/article/details/80960175
1、首先自定义一个服务类MyService.java,继承自Service
package com.t20.servicedemo.serv;
/**
* 服务类
*/
import com.t20.servicedemo.serv.function.PayMentInterface;
import com.t20.servicedemo.serv.function.PayMentInterface.Stub;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
/**
* 绑定服务时,调用onBind方法
*/
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new MyBinder();
}
/**
* 自定义的绑定器
* @author Administrator
*
*/
class MyBinder extends Stub{
@Override
public void payment(int money) {
// TODO Auto-generated method stub
pay(money);
}
}
/* (non-Javadoc)
* @see android.app.Service#onCreate()
*/
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.w("服务消息", "服务已启动");
}
/* (non-Javadoc)
* @see android.app.Service#onDestroy()
*/
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.w("服务消息", "服务已关闭");
}
private void pay(int money){
Log.e("支付信息", "支付了"+money+"元");
}
}
2、在清单文件AndroidManifest.xml中注册服务
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.t20.servicedemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.t20.servicedemo.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="com.t20.servicedemo.serv.MyService">
<intent-filter>
<!-- 自定义支付服务的动作 -->
<action android:name="com.xxx.pay"/>
</intent-filter>
</service>
</application>
</manifest>
3、写一个支付接口PayMentInterface.aidl(接口定义好再把java后缀名改成aidl)
package com.t20.servicedemo.serv.function;
/**
* 支付接口
* @author Administrator
*
*注:aidl类不能加public
*/
interface PayMentInterface {
void payment(int money);
}
4、MainActivity.java
package com.t20.servicedemo;
import com.t20.servicedemo.serv.MyService;
import com.t20.servicedemo.serv.function.PayMentInterface;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private MyConnection conn;
PayMentInterface pif;
private EditText etMoney;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etMoney=(EditText) findViewById(R.id.etMoney);
}
/**
* 开启服务
* @param v
*/
public void start(View v){
Intent startIntent=new Intent();
startIntent.setClass(MainActivity.this, MyService.class);
//1.启动服务
startService(startIntent);
//2.创建活动与服务的连接
conn=new MyConnection();
//3.绑定服务与活动
bindService(startIntent, conn, BIND_AUTO_CREATE);
//-------------意图-------连接---绑定时自动创建(也可以写0)
}
/**
* 用于创建活动与服务连接的类(自定义)
* @author Administrator
*
*/
class MyConnection implements ServiceConnection{
//连接启用时调用
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
//获取接口对象
pif=(PayMentInterface) service;
}
//连接关闭时调用
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
}
/**
* 停止服务
* @param v
*/
public void stop(View v){
if(conn!=null){
Intent stopIntent=new Intent();
stopIntent.setClass(MainActivity.this, MyService.class);
//解绑服务与活动
unbindService(conn);
pif=null;
conn=null;
//停止服务
stopService(stopIntent);
}
}
/**
* 支付功能
* @param v
*/
public void pay(View v){
String moneyString=etMoney.getText().toString();
if(moneyString.isEmpty()){
Toast.makeText(MainActivity.this, "请输入金额", Toast.LENGTH_SHORT).show();
return;
}
int money=Integer.parseInt(moneyString);
if(pif!=null){
try {
pif.payment(money);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
Toast.makeText(MainActivity.this, "请先开启服务", Toast.LENGTH_SHORT).show();
}
}
}