Android那些知识点

当点击多任务健之后,Activity的生命周期是怎么样的

实际测试结果,会走到onStop里面,然后再次回到前台,会走到onRestart onStart

2021-04-02 12:54:40.386 9251-9251/com.plbear.leakcanary E/yanlog: onPause
2021-04-02 12:54:40.426 9251-9251/com.plbear.leakcanary E/yanlog: onStop
2021-04-02 12:54:45.769 9251-9251/com.plbear.leakcanary E/yanlog: onRestart
2021-04-02 12:54:45.772 9251-9251/com.plbear.leakcanary E/yanlog: onStart
2021-04-02 12:54:45.773 9251-9251/com.plbear.leakcanary E/yanlog: onResume
2021-04-02 12:54:45.774 9251-9251/com.plbear.leakcanary E/yanlog: onPostResume

onPostCreate

谷歌的注解如下:

/**
 * Called when activity start-up is complete (after {@link #onStart}
 * and {@link #onRestoreInstanceState} have been called).  Applications will
 * generally not implement this method; it is intended for system
 * classes to do final initialization after application code has run.
 *
 * <p><em>Derived classes must call through to the super class's
 * implementation of this method.  If they do not, an exception will be
 * thrown.</em></p>
 *
 * @param savedInstanceState If the activity is being re-initialized after
 *     previously being shut down then this Bundle contains the data it most
 *     recently supplied in {@link #onSaveInstanceState}.  <b><i>Note: Otherwise it is null.</i></b>
 * @see #onCreate
 */

大概意思就是,这个是在应用完全启动之后创建,一般不建议实现这个方法,注意这个方法是在onStart和onRestroeInstnaceState之后调用的

onPostResume

谷歌的注解如下

/**
 * Called when activity resume is complete (after {@link #onResume} has
 * been called). Applications will generally not implement this method;
 * it is intended for system classes to do final setup after application
 * resume code has run.
 *
 * <p><em>Derived classes must call through to the super class's
 * implementation of this method.  If they do not, an exception will be
 * thrown.</em></p>
 *
 * @see #onResume
 */

大意就是这个是在Activity完全出现在前台之后回调,也不建议我们实现。

多个App能否运行在同一个进程里面

当然可以,可以通过shareUid的方式,具体就是在AndroidManifiest中做出如下修改,但是这么改其实也是有前提的,就是要确保两个apk的签名一致。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mythou.serviceID"
android:versionCode="1"
android:versionName="1.0"
android:sharedUserId="com.mythou.share"

>

一个App能否运行在多个进程里面

当然是可以的,可以在AndroidManfiest中为四大组件设置process属性,然后就可以了。

当然也可以通过在JNI中去fork一个新的进程,但是这种方式会比较少见,暂时忽略不计。

AIDL的原理

首先使用可以参看这一篇文章,讲的非常好,很简洁。https://www.jianshu.com/p/d1fac6ccee98

然后AIDL文件编译出来的结构是这样的,假如说AIDL的结构是这样

(以下代码为手写,不保证编译通过,只表达一个意思

interface IBookManager{
    void addBook(in Book book)
}

然后编译出来的结构是这样的

public interface IBookManager extends android.os.IInterface {
    void addBook(Book book);

    public static abstract class Stub extends android.os.Binder implements IBookManager{
        public static IBookManager asInterface(){
            .....
            return Proxy()
        }

        public IBinder asBinder(){
            return this;
        }
        
        public bookean onTransact(xxx){
            .....
            这里调用真正的实现,比如说addBook类的实现
        }
    }

    public static class Proxy implements IBookManager{
        public void addBook(Book book){
            //mRemote就是一个binder对象
            mRemote.transact(xxxx)
        }
    }
}

其中,Stub是给服务端用的,我们需要自己定义类去继承实现Stub,提供接口的具体实现

Proxy是给客户端用的,我们调用里面的方法,实际上会调用binder.transact,然后服务端回走到onTransact,将逻辑分配给正确的方法里面去实现

猜你喜欢

转载自blog.csdn.net/weixin_43662090/article/details/115396020