Android SharedPreference


context.getSharedPreferences(“prefName”, Context.MODE_PRIVATE) get SharedPreferecnes instance
sharedPref.getXXX(“keyName”, defaultValue) get value from SharedPreferences
sharedPref.edit() get editor(SharedPreference.Editor)
editor.putXXX(“key-name”, value) put value
editor.commit() commit the changes

Android 连接 WiFi

redirects to

Android 6.0 changes

working methods

get real ip
connect to wifi

android added support to eap wifi in API 18 (4.3), a lot of tutorials online are outdated.

use WifiEnterpiresConfiguration.

Android 开发的一些 tips

Prefer Maven dependency resolution instead of importing jar files. If you explicitly include jar files in your project, they will be of some specific frozen version, such as 2.1.1. Downloading jars and handling updates is cumbersome, this is a problem that Maven solves properly, and is also encouraged in Android Gradle builds. For example:

dependencies {
    compile "com.squareup.okhttp:okhttp:2.2.0"
    compile "com.squareup.okhttp:okhttp-urlconnection:2.2.0"

Use different package name for non-release builds Use applicationIdSuffix for debug build type to be able to install both debug and release apk on the same device (do this also for custom build types, if you need any). This will be especially valuable later on in the app’s lifecycle, after it has been published to the store.

android {
    buildTypes {
        debug {
            applicationIdSuffix ".debug"
            versionNameSuffix "-DEBUG"
release {
            // ...

Use different icons to distinguish the builds installed on a device—for example with different colors or an overlaid “debug” label. Gradle makes this very easy: with default project structure, simply put debug icon in app/src/debug/res and release icon in app/src/release/res. You could also change app name per build type, as well as versionName (as in the above example).

use stetho



Go to Settings > Security > Install from storage.

install programatically can be achived by from command line, by moving certs to

replacing bks file solely is useless, it has to be combined with the password

set proxy programatically

  1. set mannully for one
  2. pull /data/misc/wifi/ipconfig.txt
  3. push to other devices


make certs like this tutorial,
but push it to /data/misc/user/0/cacerts-added/





public class DumpLocationLog extends Thread {
    LocationManager lm;
    LocationHelper loc;
    public DumpLocationLog(Context context) {
        loc = new LocationHelper();
        lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    public void run() {
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, loc);


new DumpLocationLog(this);

if you are in a fragment, use getAcitvity()

if you are in an anoynmous onclicklistener, this is MainActivity.this



这里主要是需要在安卓上生成 host key,以及把自己的公钥传到安卓上

dropbearkey -t rsa -f /data/local/dropbear_host_key # 在安卓上生成key
adb push ~/.ssh/ /data/local/authorized_keys # 在宿主机把自己的密钥传过去
dropbear -F -E -r /data/local/dropbear_host_key -A -N root -C jk -R /data/local/authorized_keys # 按照给定的key启动dropbear
dropbear -P /data/local/ -r /data/local/dropbear_host_key -A -N root -C jk -R /data/local/authorized_keys # 以daemon形式启动dropbear

如何为64位的安卓机器编译 dropbear


addnewvar("LD_LIBRARY_PATH", "/system/lib");


addnewvar("LD_LIBRARY_PATH", "/system/lib64");


service sshd /system/xbin/dropbear -s
   user  root
   group root



setprop service.adb.tcp.port 5555
stop adbd
start adbd

关闭 ssh key 验证

Host *
    StrictHostKeyChecking no

mount -o remount,rw /system



安卓的 AsyncTask

asynchronusally run task without explicitly creating thread.



Here is an example of subclassing:

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
    protected Long doInBackground(URL... urls) {
        int count = urls.length;
        long totalSize = 0;
        for (int i = 0; i < count; i++) {
            totalSize += Downloader.downloadFile(urls[i]);
            publishProgress((int) ((i / (float) count) * 100));
            // Escape early if cancel() is called
            if (isCancelled()) break;
        return totalSize;

    protected void onProgressUpdate(Integer... progress) {

    protected void onPostExecute(Long result) {
        showDialog("Downloaded " + result + " bytes");

Once created, a task is executed very simply:

new DownloadFilesTask().execute(url1, url2, url3);

template parameters can be Void, Void, Void


android public key pinning

What is public key pinning?

to view https traffic, you could sign your own root CA, and perform mitm attack to view the traffic. HPKP (http public key pinning) stops this sniffing by only trust given CA, thus, your self-signed certs will be invalid. To let given app to trust your certs, you will have to modify the apk file.

How to break it?

Introducing Xposed

decompile, modify and then recompile the apk file can be very diffcult. so you’d better hook to some api to let the app you trying to intercept trust your certs. xposed offers this kind of ability. moreover, a xposed module called JustTrustMe have done the tedious work for you. just install xposed and JustTrustMe and you are off to go. Here are the detaild steps:

  1. Install Xposed Installer

for android 5.0 above, use the xposed installer.

NOTE: 对于 MIUI,需要搜索 Xposed 安装器 MIUI 专版。

  1. Install Xposed from xposed installer, note, you have to give root privilege to xposed installer

  2. Install JustTrustMe

android intent

You should use intent to communicate between activities and services

intent extra

package data from activity

create a intent instance

Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra(“key”, value);

package data from service

Intent intent = new Intent(context, SignoutActivity.class);
intent.putExtra(“key”, value);

Access data in activity

Access that intent on next activity
String s = getIntent().getStringExtra(“EXTRASESSIONID”);
The docs for Intents has more information (look at the section titled “Extras”).

Access data in Service

just use the Intent parameter from onStartCommand

public int onStartCommand (Intent intent, int flags, int startId) {
String userID = intent.getStringExtra(“UserID”);

intent plags


intent action


android.os.handler 用于向每个线程的 MessageQueue 发送消息。

There are two main uses for a Handler:

(1) to schedule messages and runnables to be executed as some point in the future; and

(2) to enqueue an action to be performed on a different thread than your own.