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

android IPC 进程间通讯

2016-12-30 15:04 483 查看
参考资料:

http://blog.csdn.net/birdsaction/article/details/39451849


在这里我说一下学习技术的方法,别人的博客,别人的东西,再简单,自己没有写过,永远是别人的。学习技术最快的方式就是参考别人的资料,然后自己动手写一个小例子,入门,hellowWorld 到大神。

有个好用的log类,写demo,还是很爽的。然后调试,看log,解决问题。我的log类如下,然后打印的时候会打印类,方法名,行数。

12-30 14:51:23.694 19372-19372/? I/Phoenix: [MainActivity:onServiceConnected:26]: 服务连接上了


package com.example.ipc;

import android.text.TextUtils;

public final class Logger {

private static final String TAG = "Phoenix";

/**
* Set true or false if you want read logs or not
*/
private static boolean logEnabled_d = true;
private static boolean logEnabled_i = true;
private static boolean logEnabled_e = true;

public static void d() {
if (logEnabled_d) {
android.util.Log.v(TAG, getLocation());
}
}

public static void d(String msg) {
if (logEnabled_d) {
android.util.Log.v(TAG, getLocation() + msg);
}
}

public static void i(String msg) {
if (logEnabled_i) {
android.util.Log.i(TAG, getLocation() + msg);
}
}

public static void i() {
if (logEnabled_i) {
android.util.Log.i(TAG, getLocation());
}
}

public static void e(String msg) {
if (logEnabled_e) {
android.util.Log.e(TAG, getLocation() + msg);
}
}

public static void e(String msg, Throwable e) {
if (logEnabled_e) {
android.util.Log.e(TAG, getLocation() + msg, e);
}
}

public static void e(Throwable e) {
if (logEnabled_e) {
android.util.Log.e(TAG, getLocation(), e);
}
}

public static void e() {
if (logEnabled_e) {
android.util.Log.e(TAG, getLocation());
}
}

private static String getLocation() {
final String className = Logger.class.getName();
final StackTraceElement[] traces = Thread.currentThread().getStackTrace();
boolean found = false;

for (StackTraceElement trace : traces) {
try {
if (found) {
if (!trace.getClassName().startsWith(className)) {
Class<?> clazz = Class.forName(trace.getClassName());
return "["
+ getClassName(clazz)
+ ":"
+ trace.getMethodName()
+ ":"
+ trace.getLineNumber()
+ "]: ";
}
} else if (trace.getClassName().startsWith(className)) {
found = true;
}
} catch (ClassNotFoundException ignored) {
}
}

return "[]: ";
}

private static String getClassName(Class<?> clazz) {
if (clazz != null) {
if (!TextUtils.isEmpty(clazz.getSimpleName())) {
return clazz.getSimpleName();
}

return getClassName(clazz.getEnclosingClass());
}

return "";
}
}


下面介绍ipc:

1.写一个service:

package com.example.ipc;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.support.annotation.Nullable;

/**
* Created by caoxinyu on 2016/12/30.
* <p>
* 类的作用:
*/

public class MyService extends Service {
Handler mHandler=new Handler(){
@Override
public void handleMessage(Message msg) {
//            super.handleMessage(msg);
Logger.i((String) msg.obj);

//            switch (msg.what){
//                case 1:
//                    Logger.i(msg.obj.toString());
//                   break;
//            }
}
};
private Messenger mMessenger;

public MyService() {
}

@Override
public void onCreate() {
super.onCreate();
mMessenger = new Messenger(mHandler);
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
return mMessenger.getBinder();
}
}


别忘了在androidMesfest.xml 里面注册

2.写一个activity:

package com.example.ipc;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

private Messenger mMessenger;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindService(new Intent(this, MyService.class), new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
mMessenger = new Messenger(iBinder);
Logger.i("服务连接上了");
//服务连上之后才可以发送消息
sendMessage();
}

@Override
public void onServiceDisconnected(ComponentName componentName) {

}
}, Context.BIND_AUTO_CREATE);

}

private void sendMessage() {
if (mMessenger != null) {
Message message= Message.obtain();
message.what=1;
message.obj="我是从mainActivity 发送来的";
try {
mMessenger.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
}


其实就是进程间发消息。bindService。 不用写aidl那么麻烦。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: