2015年12月28日 星期一

[Android] 產生超大檔進行硬碟塞滿測試


dd if=/dev/zero of=test00.tmp bs=1m count=20480

用這個指令把 sdcard 塞滿 可方便做實驗
不過實測 android 單檔最大只到 7.85 GB (8,435,793,920.00 位元組)
要產生兩次

2015年12月23日 星期三

[Android] 關掉虛擬鍵

開啟在system資料夾底下的build.prop檔

在最後一行加入後面這個指令qemu.hw.mainkeys=1

儲存後重開機,底下的三個虛擬按鍵就不見了

youtube有完整影片,照著做就ok了

https://www.youtube.com/watch?v=wt0x3kQR2cc

2015年11月27日 星期五

[Android] 如何设置xml中的product的读取值

与sd卡相关的字符串都有两个 只区分product属性,
    <bool name="config_externalStorageRemovable" product="nosdcard">false</bool>
    <bool name="config_externalStorageRemovable" product="default">true</bool>

ubuntu下用命令  grep -rl nosdcard android_sourcecode/*
搜索到 在device文件夹下有几个device.mk
里面有一段
PRODUCT_CHARACTERISTICS:=nosdcard
将nosdcard改为default

2015年11月19日 星期四

[Android] 拔掉 SD 卡的 intent 發送流程

1.  直接拔掉 sdcard













2.  再次将 sdcard 插入卡槽










3.  在通知栏卸载 sdcard








紧接着,从卡槽拔出 sdcard(必须拔出,才会接收到下面的 action)









出處還有更多內容 ,以上為 debug 較常用的


出處
http://blog.csdn.net/veryitman/article/details/7603428

2015年11月4日 星期三

[Android] 讓動畫可以長出去外面android:clipChildren

之前有幾次專案的經驗
常常有需要把畫面畫出去 view 的邊界的 case
這時候把
android:clipChildren 設 false 就好了
預設是 true 會限制 child view 無法畫超過  parent


下面是例圖:

2015年10月21日 星期三

[Android] 常見的 Monkey 指令查詢


可在官方網頁查詢


http://developer.android.com/intl/zh-tw/tools/help/monkey.html


Basic Use of the Monkey


You can launch the Monkey using a command line on your development machine or from a script. Because the Monkey runs in the emulator/device environment, you must launch it from a shell in that environment. You can do this by prefacingadb shell to each command, or by entering the shell and entering Monkey commands directly.
The basic syntax is:
$ adb shell monkey [options] <event-count>
With no options specified, the Monkey will launch in a quiet (non-verbose) mode, and will send events to any (and all) packages installed on your target. Here is a more typical command line, which will launch your application and send 500 pseudo-random events to it:
$ adb shell monkey -p your.package.name -v 500

Command Options Reference


The table below lists all options you can include on the Monkey command line.
CategoryOptionDescription
General--helpPrints a simple usage guide.
-vEach -v on the command line will increment the verbosity level. Level 0 (the default) provides little information beyond startup notification, test completion, and final results. Level 1 provides more details about the test as it runs, such as individual events being sent to your activities. Level 2 provides more detailed setup information such as activities selected or not selected for testing.
Events-s <seed>Seed value for pseudo-random number generator. If you re-run the Monkey with the same seed value, it will generate the same sequence of events.
--throttle <milliseconds>Inserts a fixed delay between events. You can use this option to slow down the Monkey. If not specified, there is no delay and the events are generated as rapidly as possible.
--pct-touch <percent>Adjust percentage of touch events. (Touch events are a down-up event in a single place on the screen.)
--pct-motion <percent>Adjust percentage of motion events. (Motion events consist of a down event somewhere on the screen, a series of pseudo-random movements, and an up event.)
--pct-trackball <percent>Adjust percentage of trackball events. (Trackball events consist of one or more random movements, sometimes followed by a click.)
--pct-nav <percent>Adjust percentage of "basic" navigation events. (Navigation events consist of up/down/left/right, as input from a directional input device.)
--pct-majornav <percent>Adjust percentage of "major" navigation events. (These are navigation events that will typically cause actions within your UI, such as the center button in a 5-way pad, the back key, or the menu key.)
--pct-syskeys <percent>Adjust percentage of "system" key events. (These are keys that are generally reserved for use by the system, such as Home, Back, Start Call, End Call, or Volume controls.)
--pct-appswitch <percent>Adjust percentage of activity launches. At random intervals, the Monkey will issue a startActivity() call, as a way of maximizing coverage of all activities within your package.
--pct-anyevent <percent>Adjust percentage of other types of events. This is a catch-all for all other types of events such as keypresses, other less-used buttons on the device, and so forth.
Constraints-p <allowed-package-name>If you specify one or more packages this way, the Monkey will only allow the system to visit activities within those packages. If your application requires access to activities in other packages (e.g. to select a contact) you'll need to specify those packages as well. If you don't specify any packages, the Monkey will allow the system to launch activities in all packages. To specify multiple packages, use the -p option multiple times — one -p option per package.
-c <main-category>If you specify one or more categories this way, the Monkey will only allow the system to visit activities that are listed with one of the specified categories. If you don't specify any categories, the Monkey will select activities listed with the category Intent.CATEGORY_LAUNCHER or Intent.CATEGORY_MONKEY. To specify multiple categories, use the -c option multiple times — one -c option per category.
Debugging--dbg-no-eventsWhen specified, the Monkey will perform the initial launch into a test activity, but will not generate any further events. For best results, combine with -v, one or more package constraints, and a non-zero throttle to keep the Monkey running for 30 seconds or more. This provides an environment in which you can monitor package transitions invoked by your application.
--hprofIf set, this option will generate profiling reports immediately before and after the Monkey event sequence. This will generate large (~5Mb) files in data/misc, so use with care. See Traceview for more information on trace files.
--ignore-crashesNormally, the Monkey will stop when the application crashes or experiences any type of unhandled exception. If you specify this option, the Monkey will continue to send events to the system, until the count is completed.
--ignore-timeoutsNormally, the Monkey will stop when the application experiences any type of timeout error such as a "Application Not Responding" dialog. If you specify this option, the Monkey will continue to send events to the system, until the count is completed.
--ignore-security-exceptionsNormally, the Monkey will stop when the application experiences any type of permissions error, for example if it attempts to launch an activity that requires certain permissions. If you specify this option, the Monkey will continue to send events to the system, until the count is completed.
--kill-process-after-errorNormally, when the Monkey stops due to an error, the application that failed will be left running. When this option is set, it will signal the system to stop the process in which the error occurred. Note, under a normal (successful) completion, the launched process(es) are not stopped, and the device is simply left in the last state after the final event.
--monitor-native-crashesWatches for and reports crashes occurring in the Android system native code. If --kill-process-after-error is set, the system will stop.
--wait-dbgStops the Monkey from executing until a debugger is attached to it.

2015年10月19日 星期一

[Android] repo sync 的小工具

repo sync 常常會不正常斷線,這個 script 可以不斷自動重試



#!/bin/bash 
while true
do
    if repo sync
      then
       echo "repo sync successfully."
      break
    else
       echo "repo sync failed. retry..."
       repo sync
    fi
done

[Android][SystemUI] 改變狀態欄電池狀態的幾種方法

1. 使用 demo mode


https://github.com/android/platform_frameworks_base/blob/marshmallow-release/packages/SystemUI/docs/demo_mode.md

adb shell settings put global sysui_demo_allowed 1
adb shell am broadcast -a com.android.systemui.demo -e command battery -e level 0 -e plugged false


2. 發 battery change,但是很容易被改掉

adb shell am broadcast -a android.intent.action.BATTERY_CHANGED --ei level 9

3. 直接改 driver ,利用 存取節點,這個方法的好處是,連系統狀態都會猶如低電量

echo 10 > /sys/class/power_supply/battery/capacity

2015年10月1日 星期四

[Android] 如何在中國手機裝 GMS (Google mobile services)

以 Android L 手機為例子

1. adb push com.google.android.maps.jar system/framework/
2. adb push com.google.android.maps.xml system/etc/permissions/
3. adb shell
cd system/priv-app/
mkdir  GoogleServicesFramework
adb push GoogleServicesFramework.apk system/priv-app/GoogleServicesFramework/
4. adb shell
cd system/priv-app/
mkdir GooglePlay
(Google Play app) adb push Vending.apk system/priv-app/GooglePlay/Vending.apk
5. (Google Play Service) adb install com.google.android.gms-8.1.15_(2250156-846)-8115846-minAPI19
6. adb install GoogleCalendarSyncAdapter
7. adb install GoogleContactsSyncAdapter

這樣就有 Google 的軟體服務可以用了

2015年7月3日 星期五

[Android] 如何弄 account

http://blog.udinic.com/2013/04/24/write-your-own-android-authenticator/

這篇寫得很完整
我不用多說了

2015年6月29日 星期一

[Android]如何從 APK 拿到 SHA1 key

解開 apk 檔案取得
META-INF\CERT.SF

keytool -printcert -file CERT.RSA
就可得到如下結果

擁有者: EMAILADDRESS=oneplus@oneplus.cn, CN=OnePlus, OU=SW, O=OnePlus, L=Shenzhen, ST=Guangdong, C=CN
發出者: EMAILADDRESS=oneplus@oneplus.cn, CN=OnePlus, OU=SW, O=OnePlus, L=Shenzhen, ST=Guangdong, C=CN
序號: 8abe4e1ae1d847f1
有效期自: Thu May 07 15:25:19 CST 2015 到: Mon Sep 22 15:25:19 CST 2042
憑證指紋:
         MD5:  62:08:F4:ED:35:9F:F6:60:43:A0:31:A7:A7:9B:F6:06
         SHA1: B7:91:45:D7:9F:8F:14:C2:6C:68:EC:BB:27:8D:56:AE:43:65:B1:61
         SHA256: 46:81:AD:50:CA:FC:58:0E:DF:E0:27:BD:3F:E5:93:25:4E:72:CD:2D:EF:1B:35:1F:EA:30:6C:CF:62:20:CF:07
         簽章演算法名稱: SHA1withRSA
         版本: 3

擴充套件:

#1: ObjectId: 2.5.29.35 Criticality=false
AuthorityKeyIdentifier [
KeyIdentifier [
0000: 54 61 E2 B1 A9 15 E1 4D   9F 0F 92 DD 48 01 13 31  Ta.....M....H..1
0010: A8 49 AE 64                                        .I.d
]
]

#2: ObjectId: 2.5.29.19 Criticality=false
BasicConstraints:[
  CA:true
  PathLen:2147483647
]

#3: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: 54 61 E2 B1 A9 15 E1 4D   9F 0F 92 DD 48 01 13 31  Ta.....M....H..1
0010: A8 49 AE 64                                        .I.d
]
]














2015年6月14日 星期日

[Android]產生keystore

release:


$ keytool -genkey -v -keystore my-key.keystore -alias alias_name -keyalg RSA -keysize 2048 -validity 10000


debug:


$ keytool -genkey -v -keystore debug.keystore -storepass android -alias androiddebugkey -keypass android -keyalg RSA -keysize 2048 -validity 10000


get release finger print:


$ keytool -list -v -keystore [keystore path] -alias [alias-name] -storepass [storepass] -keypass [keypass]



get debug finger print:

$ keytool -list -v -keystore debug.keystore -alias androiddebugkey -storepass android -keypass android

2015年5月30日 星期六

[Android] google Map API usage 使用方法



google play service 常常變來變去
最近的用法是要滿足以下步驟

1. 加權限

AndroidManifest.xml
//  google map
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
//  location service
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

2. 加 meta data
   <application
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name" >
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="xxxxxxx_your_key" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />


3. 加 code 這是版本改變導致最大的變化


final GoogleMap map = mapView.getMap();
    final float zoomLevel = 16.0f;

m_GoogleApiClient = new GoogleApiClient.Builder(PageNear.this.getActivity())
        .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks(){

@Override
public void onConnected(Bundle connectionHint) {
// TODO Auto-generated method stub
Log.d("", "");
Location location = LocationServices.FusedLocationApi.getLastLocation(m_GoogleApiClient);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(
                        new LatLng(location.getLatitude(), location.getLongitude()), zoomLevel));

}

@Override
public void onConnectionSuspended(int cause) {
// TODO Auto-generated method stub
Log.d("", "");
}})
        .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener(){

@Override
public void onConnectionFailed(ConnectionResult result) {
// TODO Auto-generated method stub
Log.d("", "");
}})
        .addApi(LocationServices.API)
        .build();

        m_GoogleApiClient.connect();




2015年5月20日 星期三

[Android] MediaStore 的 databases 存在哪

/data/data/com.android.providers.media/databases/internal.db
/data/data/com.android.providers.media/databases/external.db


看你的資料存在哪,sdcard 是在 external.db



2015年5月6日 星期三

[Android]如何正常結束一個 HandlerThread

mHandlerThread.getLooper().quit()or
mHandlerThread.getLooper().quitSafely()

better

細節可直接攻讀 android 程式碼
http://developer.android.com/reference/android/os/Looper.html#quitSafely()


2015年4月18日 星期六

[IT] 解決 Chrome瀏覽器的異常,像是玉山 webatm不能用,旺旺 百度等 plugin

發生原因:Chrome 不在預設支援 NPAPI


Chrome 的 NPAPI 支援

Web 瀏覽器的 Java 外掛程式有賴於所有主要 Web 瀏覽器長久以來一直支援的跨平台外掛程式架構NPAPI。Google 於 2013 年 9 月宣布計畫將在 2014 年底終止 Chrome 的 NPAPI 支援,進而實際上停止對 Silverlight、Java、Facebook 影片以及其他類似 NPAPI 形式之外掛程式的支援。但最近 Google 修改了其計畫,現在宣佈他們計畫在 2015 年下旬完全移除 NPAPI。在 2015 年 4 月,Google 從 Chrome 版本 42 開始,新增了額外的步驟,以設定執行以 NPAPI 為基礎的外掛程式 

解決方法 :

在 Chrome 版本 42 及更新版本啟用 NPAPI

在 Chrome 版本 42 中,需要額外的組態步驟以繼續使用 NPAPI 外掛程式。
  1. 在您的 URL 列輸入:
    chrome://flags/#enable-npapi 
  2. 按一下啟用 NPAPI 組態選項的啟用連結
  3. 按一下組態頁面底端出現的立即重新啟動按鈕

2015年4月13日 星期一

[JAVA] java memory mode with "volatile" & "synchronized"

As a matter of fact, the Java memory model is such that a modification
to a variable in one thread may not immediately be visible to other threads. Actually, it
may never be visible. Consider the code in Listing 5–11: if one thread calls
MyClass.loop(), and at some point in the future another thread calls
Myclass.setValue(100), the first thread may still not terminate; may carry on looping
forever and always print out a value other than 100, simply because of the Java
language’s memory model.
Listing 5–11. Java Memory Model Impact
public class MyClass {
     private static final String TAG = "MyClass";
     private static int mValue = 0;
     public static void setValue(int n) {
          mValue = n;
     }
     public static void loop () {
          while (mValue != 100) {
               try {
                    Log.i(TAG, “Value is ” + mValue);
                    Thread.sleep(1000);
               } catch (Exception e) {
                    // ignored
               }
          }
     }
}
You have two options to fix that:
Use the synchronized keyword, as shown in Listing 5–12.
Use the volatile keyword, as shown in Listing 5–13.
Listing 5–12. Adding the Synchronized Keyword
public class MyClass {
     private static final String TAG = "MyClass";
     private static int mValue = 0;
     public static synchronized void setValue(int n) {
          mValue = n;
     }
     public static synchronized int getValue() {
          return mValue;
     }
     public static void loop () {
          int value;
          while ((value = getValue()) != 100) {
          try {
               Log.i(TAG, “Value is ” + value);
               Thread.sleep(1000);
               } catch (Exception e) {
              // ignored
               }
          }
     }
}

Listing 5–13. Adding the Volatile Keyword
public class MyClass {
     private static final String TAG = "MyClass";
     private static volatile int mValue = 0; // we add the volatile keyword and remove
     the synchronize keyword
     public static void setValue(int n) {
          mValue = n; // you’d still have to use synchronized if that statement were mValue += n (not atomic)
     }
     public static void loop () {
          while (mValue != 100) {
               try {
               Log.i(TAG, “Value is ” + mValue);
               Thread.sleep(1000);
               } catch (Exception e) {
              // ignored
               }
          }
     }
}

2015年4月12日 星期日

[Android] Handlers and Loopers

Android defines two classes in the android.os package that will often be the
cornerstones of the interthread communication in your multithreaded applications:
Handler
Looper
While creating an AsyncTask object hides the Handler and Looper details from you, in
some cases you need to use handlers and loopers explicitly, for example when you need
to post a Runnable to a thread other than the main thread.

you use a
Handler object to post a Runnable in a Looper’s message queue. Your application’s main
thread already has a message queue, so you don’t have to create one explicitly.
However, the threads you create do not come automatically with a message queue and
message loop, so you would have to create one yourself if needed. Listing 5-8  shows
how you can create a thread with a Looper.
Listing 5–8
public class MyThread extends Thread {
     private static final String TAG = “MyThread”;
     private Handler mHandler;
     public MyThread(String name) {
          super(name);
     }
     public Handler getHandler() {
          return mHandler;
     }
     @Override
     public void run() {
          Looper.prepare(); // binds a looper to this thread
          mHandler = new Handler() {
          @Override
          public void handleMessage(Message msg) {
               switch (msg.what) {
               // process messages here
               }
          }
     };
     // the handler is bound to this thread’s looper
     Looper.loop(); // don’t forget to call loop() to start the message loop
      // loop() won’t return until the loop is stopped (e.g., when Looper.quit() is called)
     }
}
Android provides an easier way to work with looper threads with the HandlerThread
class, which also makes it easier to avoid the potential race condition mentioned in
Listing 5–8, where getHandler() may still return null even after the thread has been
started. Listing 5–9 shows how to use the HandlerThread class.
Listing 5–9. Using the HandlerThread Class
     public class MyHandlerThread extends HandlerThread {
     private static final String TAG = "MyHandlerThread";
     private Handler mHandler;
     public MyHandlerThread(String name) {
          super(name);
     }
     public Handler getHandler() {
          return mHandler;
     }
@Override
     public void start() {
          super.start();
          Looper looper = getLooper(); // will block until thread’s Looper object is initialized
          mHandler = new Handler(looper) {
               @Override
                    public void handleMessage(Message msg) {
                         switch (msg.what) {
                         // process messages here
                    }
               }
          };
     }

}

2015年4月7日 星期二

[Android][Java] Java define 4 reference type

Strong
Soft
Weak
Phantom

it is important to avoid memory leak
Android’s LruCache uses strong references

2015年3月26日 星期四

[Design Partern] Strategy pattern case 1: Android load NDK library

在 Android 的開發中使用 NDK 最佳化的時候,可能會因為平台的差異,導致 native code 無法執行,這時候必須提供 Java 版本的 solution。

Design Pattern 裡面有一個Strategy pattern,他的特性如下:
In computer programming, the strategy pattern (also known as the policy pattern) is a software design pattern that enables an algorithm's behavior to be selected at runtime. The strategy pattern

1. defines a family of algorithms,
2. encapsulates each algorithm, and
3. makes the algorithms interchangeable within that family.

載入 native library 的時機點也是在 runtime,剛好符合 Startegy pattern 的 case:
例子如下:

2015年3月20日 星期五

[Performance] Fibonacci演算法探討演算法最佳解



這是基本的解法,但是會 overflow,遞迴會造成 stack 爆掉, performance很差

Fibonacci的演算公式,可減少程式的複雜度





接近完美的解法,倒入 cache 機制和 Android 特有的 SparseArray: