自己用的AIDL

AActivity类
package com.example.xunlianchang;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class AActivity extends AppCompatActivity {
    TextView textView0,textView1,textView2,textView3;
    ImageView textView;
    private MyServiceConnection myServiceConnection = new MyServiceConnection();
    private Bean b;
    Intent intent;
    private Button button;
    AIDLcallbackInterface aidLcallbackInterface;
    private ObjectAnimator mProgressAnimator;
    private Animation aaa;
    private SeekBar mSeekBar;
    private static final int PROGRESS_MIN = 0;
    private static final int PROGRESS_MAX = 1000;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aaaa);
        mSeekBar = findViewById(R.id.prediction_seek_bar);
        textView0 = findViewById(R.id.a1);
        textView1 = findViewById(R.id.a2);
        textView2 = findViewById(R.id.a3);
        textView3 = findViewById(R.id.a4);
        textView = findViewById(R.id.a5);
        button = findViewById(R.id.bt1);
        aaa = AnimationUtils.loadAnimation(this, R.anim.xlist_progress);
        textView.setAnimation(aaa);                  //为控件设置动画
        aaa.setFillAfter(true);                   //停留在结束位置
        aaa.setFillEnabled(true);
        aaa.startNow();

        Object object = ((MyApplication)getApplicationContext()).getHashmap().get("aaaa");
        if (object instanceof Bean){
            b = (Bean) object;
            textView0.setText(b.getA()+"");
            textView1.setText(b.getB()+"");
            textView2.setText(b.getC()+"");
            textView3.setText(b.getE()+"");
        }
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.startAnimation(aaa);
            }
        });
        textView0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent = new Intent();
                //由于是隐式启动Service 所以要添加对应的action,A和之前服务端的一样。
                intent.setAction("com.aaa.hello");
                //android 5.0以后直设置action不能启动相应的服务,需要设置packageName或者Component。
                intent.setPackage("com.example.xunlianchang"); //packageName 需要和服务端的一致.
                bindService(intent,myServiceConnection,BIND_AUTO_CREATE);

            }
        });
        mProgressAnimator = ObjectAnimator.ofInt(mSeekBar, "progress", PROGRESS_MIN, PROGRESS_MAX);
        mProgressAnimator.setDuration(5000);
        mProgressAnimator.setRepeatCount(0);
        mProgressAnimator.setInterpolator(new LinearInterpolator());
        mProgressAnimator.start();
        mProgressAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int currentValue = (Integer) animation.getAnimatedValue();
                Log.e("aaaaaaaaaa",currentValue+"");
            }
        });
    }
    private IMyAidlInterface.Stub iMyAidlInterface = new IMyAidlInterface.Stub() {
        @Override
        public void getInt(int a) throws RemoteException {
            Log.e("aaaaa",a+"");
        }
    };
    class MyServiceConnection implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
             aidLcallbackInterface = AIDLcallbackInterface.Stub.asInterface(service);
            try {
                aidLcallbackInterface.BeanCallback(b);
                aidLcallbackInterface.rescart(iMyAidlInterface);
            } catch (RemoteException e) {
                e.printStackTrace();
            }


        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }
}
RemoteService类
public class RemoteService extends Service {
    private IMyAidlInterface i;

    @Override
    public void onCreate() {
        super.onCreate();

    }

    private AIDLcallbackInterface.Stub aidLcallbackInterface = new AIDLcallbackInterface.Stub() {
        @Override
        public void BeanCallback(Bean bean) throws RemoteException {

        }

        @Override
        public void rescart(IMyAidlInterface i) throws RemoteException {
            RemoteService.this.i = i;
            i.getInt(1);
        }

    };
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d("RemoteService","远程服务绑定成功");
        return aidLcallbackInterface;
    }
}

AIDLcallbackInterface类
package com.example.xunlianchang;

import com.example.xunlianchang.Bean;
import com.example.xunlianchang.IMyAidlInterface;
// Declare any non-default types here with import statements

interface AIDLcallbackInterface {

    void BeanCallback(in Bean bean);

    void rescart(IMyAidlInterface i);
}
IMyAidlInterface类
// IMyAidlInterface.aidl
package com.example.xunlianchang;

// Declare any non-default types here with import statements

interface IMyAidlInterface {

   void getInt(int a);
}

Bean

package com.example.xunlianchang;

parcelable Bean;
  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值