Messager与AIDL比较(转)


转载:http://glason.diandian.com/post/2011-09-03/4653170


Using a Messenger

Compared to AIDL

When you need to perform IPC, using a Messenger for your interface is simpler than implementing it with AIDL, because Messenger queues all calls to the service, whereas, a pure AIDL interface sends simultaneous requests to the service, which must then handle multi-threading.

For most applications, the service doesn't need to perform multi-threading, so using a Messenger allows the service to handle one call at a time. If it's important that your service be multi-threaded, then you should use AIDL to define your interface.

If you need your service to communicate with remote processes, then you can use a Messenger to provide the interface for your service. This technique allows you to perform interprocess communication (IPC) without the need to use AIDL.

Here's a summary of how to use a Messenger:

In this way, there are no "methods" for the client to call on the service. Instead, the client delivers "messages" (Message objects) that the service receives in its Handler.

Here's a simple example service that uses a Messenger interface:


public class MessengerService extends Service { 
    /** Command to the service to display a message */ 
    static final int MSG_SAY_HELLO = 1 ; 

    /**
     * Handler of incoming messages from clients.
     */ 
    class IncomingHandler extends Handler { 
        @Override 
        public void handleMessage ( Message msg ) { 
            switch ( msg . what ) { 
                case MSG_SAY_HELLO : 
                    Toast . makeText ( getApplicationContext (), "hello!" , Toast . LENGTH_SHORT ). show (); 
                    break ; 
                default : 
                    super . handleMessage ( msg ); 
            } 
        } 
    } 

    /**
     * Target we publish for clients to send messages to IncomingHandler.
     */ 
    final Messenger mMessenger = new Messenger ( new IncomingHandler ()); 

    /**
     * When binding to the service, we return an interface to our messenger
     * for sending messages to the service.
     */ 
    @Override 
    public IBinder onBind ( Intent intent ) { 
        Toast . makeText ( getApplicationContext (), "binding" , Toast . LENGTH_SHORT ). show (); 
        return mMessenger . getBinder (); 
    } 
}


展开阅读全文

比较另类的表达式计算(转)

03-01

请看下面的代码(转的,csdn的一个blog)rnrn// c# 公式计算问题rnrnrnusing System;rnusing System.CodeDom.Compiler;rnusing System.Reflection;rnusing System.Text;rnusing Microsoft.CSharp;rnrnnamespace Expressionrnrn /// rn /// 计算表达式的类 rn /// rn public class CalculateExpressionrn rn /// rn /// 接受一个string类型的表达式并计算结果,返回一个object对象,静态方法 rn /// rn /// rn /// rn public static object Calculate(string expression)rn rn string className = "Calc";rn string methodName = "Run";rn expression = expression.Replace("/", "*1.0/");rnrn // 创建编译器实例。 rn ICodeCompiler complier = (new CSharpCodeProvider().CreateCompiler());rnrn // 设置编译参数。 rn CompilerParameters paras = new CompilerParameters();rn paras.GenerateExecutable = false;rn paras.GenerateInMemory = true;rnrn // 创建动态代码。 rn StringBuilder classSource = new StringBuilder();rn classSource.Append("public class " + className + "\n");rn classSource.Append("\n");rn classSource.Append(" public object " + methodName + "()\n");rn classSource.Append(" \n");rn classSource.Append(" return " + expression + ";\n");rn classSource.Append(" \n");rn classSource.Append("");rnrn // 编译代码。 rn CompilerResults result = complier.CompileAssemblyFromSource(paras, classSource.ToString());rnrn // 获取编译后的程序集。 rn Assembly assembly = result.CompiledAssembly;rnrn // 动态调用方法。 rn object eval = assembly.CreateInstance(className);rn MethodInfo method = eval.GetType().GetMethod(methodName);rn object reobj = method.Invoke(eval, null);rn GC.Collect();rn return reobj;rn rn rnrn class CMainrn rn public static int Main()rn rn object result;rn for(int i=1;i<9999;i++)rn result = CalculateExpression.Calculate(string.Format("0/1",i,i+1));rn //Console.WriteLine(result);rn Console.ReadKey();rn return 0;rn rn rnrnrnMain是我加的测试,结果效率非常低,我都没=到出结果。毕竟计算过程中很多装箱,string,甚至编译。。。rn如果你还没有找到在c#中作表达式计算,可以暂时用一下这个,而且支持超级复杂的表达式:估计整个一应用程序都可以了。。。rn发现这个之前我经常用sql中执行compute等方法,这个效率可能比上面的高。rn另求.net中表达式计算的其他方法。 论坛

没有更多推荐了,返回首页