获取当前界面的Activity名称

adb logcat | grep ActivityManager

关于aidl

在Android工程中, 创建xxxx.aidl文件,编译后,会生成一个xxxx.java文件,这个文件里生成Stub抽象类继承android.os.Binder、实现aidl中声明的方法。然后工程里创建一个service类

Android Activity生命周期

进入到一个Activity
onCreate -> onStart -> onResume

App进入后台
onPause -> onStop

关闭Activity
onPause -> onStop -> onDestroy

App页面跳转
onPause -> onStop

重回Activity
onRestart -> onStart -> onResume

总结就是: 界面出现 -> onStart, 界面消失 -> onStop
         界面创建 -> onCreate, 界面摧毁 -> onDestroy

Android App启动时类的加载顺序

在工程的AndroidManifest.xmlapplication中定义android:name属性,到具体的类,App启动时就首先加载这个类。然后再加载在AndroidManifest.xml中定义为android.intent.category.LAUNCHER的类

BroadcastReceiver 注册有两种方式

1. 静态注册

需要过滤的filtAction写在AndroidManifest.xml里
比如:

<receiver android:name=".MyBroadcastReceiver"
          android:exported="false" >
<!--intent过滤器,指定可以匹配哪些intent, 一般需要定义action 可以是自定义的也可是系统的-->
    <intent-filter>
        <action android:name="com.app.broadcast"/>
    </intent-filter>
</receiver>
2. 动态注册
MyBroadcastReceiver myReceiver = new MyBroadcastReceiver();
IntentFilter fileter = new IntentFilter();
fileter.addAction("com.app.broadcast");
MainActivity.this.registerReceiver(myReceiver, fileter);
3. 发送通知:
Intent intent = new Intent("");
this.sendBroadcast(intent);

截图

adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > screen.png

关于Handler

应用启动时,Android FrameWork创建一个Looper对象,处理MessageQueue里的Message
创建一个Hander后,MessageQueue对象会持有这个Handler的引用
Looper处理Handler时,就触发Handler的handleMessage(Message)方法

内部匿名类和内部非静态类会有一个对外部类的隐式引用

如果Handler长时间不执行,当前类的内存不会被释放

事例代码
public class SampleActivity extends Activity {

  /**
   * Instances of static inner classes do not hold an implicit
   * reference to their outer class.
   */
  private static class MyHandler extends Handler {
    private final WeakReference<SampleActivity> mActivity;

    public MyHandler(SampleActivity activity) {
      mActivity = new WeakReference<SampleActivity>(activity);
    }

    @Override
    public void handleMessage(Message msg) {
      SampleActivity activity = mActivity.get();
      if (activity != null) {
        // ...
      }
    }
  }

  private final MyHandler mHandler = new MyHandler(this);

  /**
   * Instances of anonymous classes do not hold an implicit
   * reference to their outer class when they are "static".
   */
  private static final Runnable sRunnable = new Runnable() {
      @Override
      public void run() { /* ... */ }
  };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Post a message and delay its execution for 10 minutes.
    mHandler.postDelayed(sRunnable, 1000 * 60 * 10);
    
    // Go back to the previous Activity.
    finish();
  }
}

走路放慢脚步
107 声望3 粉丝

It is what you do that defines you