Android

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

https://stackoverflow.com/questions/4249911/android-how-to-create-eap-wifi-configuration-programmatically

redirects to

https://stackoverflow.com/questions/4374862/how-to-programmatically-create-and-read-wep-eap-wifi-configurations-in-android

https://stackoverflow.com/questions/19170260/how-to-connect-to-wpa-eap-wifi-on-android-with-4-3-api

Android 6.0 changes

https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-network

working methods

https://stackoverflow.com/questions/12486441/how-can-i-set-proxysettings-and-proxyproperties-on-android-wi-fi-connection-usin

get real ip https://stackoverflow.com/questions/39335525
connect to wifi https://stackoverflow.com/questions/8818290

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

不要在application中存储全局变量
尽量少使用globalobject,最好使用intent

安卓证书与代理自动配置

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

https://github.com/danzeeeman/meerkat-decompiled/blob/master/io/fabric/sdk/android/services/network/PinningInfoProvider.java

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, http://forum.xda-developers.com/google-nexus-5/help/howto-install-custom-cert-network-t2533550
but push it to /data/misc/user/0/cacerts-added/

安卓开发中的Context

在安卓当中,Context几乎是无处不在的,每一个Activity是一个Context,每一个Service也是一个Context。

但是如果你新起了一个线程的话,你需要显式地把Context传递进去。

比如下面的例子:

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);
    }
}

然后使用这个线程的时候,把this,也就是一个context的实例传递进去

new DumpLocationLog(this);

if you are in a fragment, use getAcitvity()

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

为安卓编译64位的dropbear

如何使用dropbear

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

dropbearkey -t rsa -f /data/local/dropbear_host_key # 在安卓上生成key
adb push ~/.ssh/id_rsa.pub /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/dropbear.pid -r /data/local/dropbear_host_key -A -N root -C jk -R /data/local/authorized_keys # 以daemon形式启动dropbear

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

需要更改如下代码(svr-chansession.c):

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

to:

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

使用AIL把dropbear添加为服务

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

试过了,但是没有成功

如何重启adb(wifi)

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

关闭 ssh key 验证

Host *
    StrictHostKeyChecking no

mount -o remount,rw /system

参考

  1. http://forum.xda-developers.com/nexus-7-2013/general/guide-compiling-dropbear-2016-73-t3351671
  2. http://forum.xda-developers.com/nexus-7-2013/general/guide-compiling-dropbear-2015-67-t3142412/page3

安卓的 AsyncTask

asynchronusally run task without explicitly creating thread.

Usage

doInBackground(Params...)
onProgressUpdate(Progress...)
onPostExecute(Result)

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) {
        setProgressPercent(progress[0]);
    }

    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

see https://developer.android.com/reference/android/os/AsyncTask.html

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);
startActivity(intent);

package data from service

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

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”);
return START_STICKY;
}

intent plags

see http://stackoverflow.com/questions/21833402/difference-between-intent-flag-activity-clear-task-and-intent-flag-activity-task

intent action

android.os.handler

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.