1. 调用服务放在oncreat()中或把返回服务的内容放在conn中
2. 既要保证服务长期后台运行,又要调用服务里面的方法
3. 混合调用的服务的生命周期:
服务长期后台运行,又想调用服务的方法:
1.start方式开启服务(保证服务长期后台运行)
2.bind方式绑定服务(保证调用服务的方法)
3.unbind解除绑定服务
4.stopService停止服务。
服务:
1.支付宝
1.1 配置文件
<service android:name="com.itheima.alipay.SafePayService"> <intent-filter> <action android:name="com.itheima.alipay"/> </intent-filter> </service>
1.2服务SafePayService
public class SafePayService extends Service {
@Override
public IBinder onBind(Intent intent) {
System.out.println("服务被绑定 onbind");
return new MyBinder();
}
/**
* 安全支付的方法
*/
private boolean pay(long time,String pwd,double money){
if("123".equals(pwd)){
return true;
}else{
return false;
}
}
private class MyBinder extends ISafePay.Stub{
/**
* 调用安全支付的逻辑
*/
@Override
public boolean callPay(long time, String pwd, double money)
throws RemoteException {
return pay(time, pwd, money);
}
}
@Override
public void onCreate() {
System.out.println("oncreate支付宝服务被创建,一直在后台运行,检查手机的安全状态");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("服务onstart");
return super.onStartCommand(intent, flags, startId);
}
@Override
public boolean onUnbind(Intent intent) {
System.out.println("onunbind");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
System.out.println("ondestory支付宝服务被销毁");
super.onDestroy();
}
}
1.3 aidl接口(new file----选择aidl)
package com.itheima.alipay;
interface ISafePay{
boolean callPay(long time,String pwd,double money);
}
2 捕鱼达人 (代码可能改动过,按照混合调用的生命周期来写才正确,详情查看视频)
2.1 新建一个与服务一模一样的包,然后把aidl接口复制到包下
package com.itheima.alipay;
interface ISafePay{
boolean callPay(long time,String pwd,double money);
}
2.2 调用服务
public class MainActivity extends Activity {
private ISafePay iSafePay;
private MyConn conn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent();
intent.setAction("com.itheima.alipay");
startService(intent);
//保证服务长期后台运行。
}
public void start(View view){
Intent intent = new Intent();
intent.setAction("com.itheima.alipay");
startService(intent);
}
public void stop(View view){
Intent intent = new Intent();
intent.setAction("com.itheima.alipay");
stopService(intent);
}
public void bind(View view){
Intent intent = new Intent();
intent.setAction("com.itheima.alipay");
conn = new MyConn();
bindService(intent, conn, BIND_AUTO_CREATE);//异步的操作
}
public void unbind(View view){
unbindService(conn);
}
public void click(View view){
Intent intent = new Intent();
intent.setAction("com.itheima.alipay");
conn = new MyConn();
bindService(intent, conn, BIND_AUTO_CREATE);//异步的操作
//绑定服务调用服务的方法。
}
private class MyConn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iSafePay = ISafePay.Stub.asInterface(service);
try {
boolean result = iSafePay.callPay(System.currentTimeMillis(), "123", 3.52f);
if(result){
Toast.makeText(getApplicationContext(), "支付成功,获取大炮弹", 0).show();
}else{
Toast.makeText(getApplicationContext(), "支付失败,请重试", 0).show();
}
// unbindService(conn);
// conn = null;
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
}