跨进程通信之Service AiDl

清单文件注册 设置主进程与子进程(还有发送数据的Service)

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".ServiceActivity"
            android:process=".ServiceActivity"
            >
        </activity>

        <service android:name=".MyService"></service>

创建AiDl文件  在包名下右键New-AIDL   然后Build文件会生成一个接口   不可以有修饰符!!!

// IGudieface.aidl
package com.example.myapplication;

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

interface IMyAidlInterface {

       String getString();

}

MainActivity代码实列

package com.example.myapplication;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    private Button bind;

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            service = ((MyService.MyBind) iBinder).getService();
            service.setName("韩开"+i);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };


    private MyService service;

    private int i=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bind = findViewById(R.id.bind);

        Intent intent = new Intent(MainActivity.this,MyService.class);
        bindService(intent,serviceConnection,BIND_AUTO_CREATE);

        bind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,ServiceActivity.class));
            }
        });

        //计时器
        setTimer();


    }

    private void setTimer(){
        Timer timer=new Timer();

        TimerTask task=new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        i++;
                        service.setName("韩开"+i);
                    }
                });
            }
        };
        timer.schedule(task,1000,1000);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(serviceConnection);
    }


}

Service代码实列

package com.example.myapplication;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;

public class MyService extends Service{

    private String name="这是一次测试";

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MyBind();
    }

    public class MyBind extends IMyAidlInterface.Stub{

        @Override
        public String getString() throws RemoteException {
            return name;
        }

        public MyService getService() {
            return MyService.this;
        }

    }

    public void setName(String name){
        this.name =name;
    }

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }
}

接受的跨进程Activity

package com.example.myapplication;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class ServiceActivity extends AppCompatActivity{

    private IMyAidlInterface iMyAidlInterface;

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {

            iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);
            try {
                String string = iMyAidlInterface.getString();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service);

        tv = findViewById(R.id.tv);


        Intent intent = new Intent(ServiceActivity.this, MyService.class);
        bindService(intent,serviceConnection,BIND_AUTO_CREATE);

        findViewById(R.id.but).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                try {
                    String string = iMyAidlInterface.getString();
                    tv.setText(string);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }

            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(serviceConnection);
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值