android IPC 进程间通讯

参考资料:

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那么麻烦。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值