您的位置:首页 > 移动开发 > Android开发

android studio AIDL跨进程通信

2016-02-16 17:51 495 查看
看了郭林大神的博客,自己用android studio写了个跨进程通信,问题还是听多了,作为笔记,记录下来吧

先看包含Service的应用

Service类

public class MyService extends Service {

[code]@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onCreate() {
    Log.d("MyService Thread", Thread.currentThread().getId() + "");
    Log.d("MyService process", android.os.Process.myPid() + "");
    super.onCreate();
    Log.d("onCreate", "onCreate");
    Notification.Builder notification = new Notification.Builder(getApplicationContext())
            .setSmallIcon(R.drawable.a)
            .setContentTitle("标题")
            .setContentText("內容內容");
    startForeground(1, notification.build());
}

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

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

@Override
public void onDestroy() {
    stopForeground(true);
    super.onDestroy();
    Log.d("onDestroy", "onDestroy");
}

MyAIDLService.Stub myBinder = new MyAIDLService.Stub() {
    @Override
    public void eat() throws RemoteException {
        Log.d("chi", "吃");
    }
};


}

在mainifast中记得配置:

[code] <service
            android:name=".MyService"
            android:process=":remote">
            <intent-filter>
                <action android:name="always" />
            </intent-filter>
        </service>


MainActivity类

[code]public class MainActivity extends Activity {

    private Button bindBtn;
    private Button unbundBtn;
    private Button startBtn;
    private Button stopBtn;
    private Intent intentService;
    private MyAIDLService mAIDL;
    private ServiceConnection serviceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder myService) {
            mAIDL = MyAIDLService.Stub.asInterface(myService);
            try {
                mAIDL.eat();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("process", android.os.Process.myPid() + "");
        Log.d("Thread", Thread.currentThread().getId() + "");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bindBtn = (Button) findViewById(R.id.bind);
        unbundBtn = (Button) findViewById(R.id.unbind);
        startBtn = (Button) findViewById(R.id.start);
        stopBtn = (Button) findViewById(R.id.stop);
        intentService = new Intent(MainActivity.this, MyService.class);

        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startService(intentService);
            }
        });
        stopBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopService(intentService);
            }
        });
        stopBtn = (Button) findViewById(R.id.stop);
        bindBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bindService(intentService, serviceConnection, BIND_AUTO_CREATE);
            }
        });
        unbundBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unbindService(serviceConnection);
            }
        });
    }
}


AIDL

用android studio开发adil最好是切换到packages模式,这样好把AIDL从第一个应用,拷贝到第二个应用,包名也不比较容易控制



这样在aidl接口中定义自己想要的方法

下面第二个应用

目录如下:




让aidl的包名和一个应用的aidl包名相同

这样就可以在activity中找到MyAIDLService接口了

Activity

[code]public class MainActivity extends Activity {
    private MyAIDLService mAIDL;
    private Button unbind;
    private Button bind;
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mAIDL = MyAIDLService.Stub.asInterface(service);
            try {
                mAIDL.eat();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //隐式传递  android5.0后不可以使用隐式传递了
        intent = new Intent("always");
        /**
         *
         * 显式传递
         */
//        intent = new Intent();
//        intent.setComponent(new ComponentName("com.example.administrator.myservice", "com.example.administrator.myservice.MyService"));
        setContentView(R.layout.layout);
        bind = (Button) findViewById(R.id.bind);
        unbind = (Button) findViewById(R.id.unbind);
        bind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bindService(intent, connection, BIND_AUTO_CREATE);
            }
        });
        unbind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unbindService(connection);
            }
        });
    }
}


开始的时候出现找不到aidl接口类,没办法实例接口类,不要急,Rebuild project就好了

这样就完成了跨进程通信,本来只想重温下service的 顺便了解下aidl,好啦继续学习。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: