九月 27, 2015
» How to detect foreground process name in Android with Lollipop

(Katwijk.Holland)

Android 一直改版,原本可以用的功能,因為安全性,因為有的 app 會亂搞,所以把許多流程和功能不是改得變複雜,就是直接拿掉了。

原本一個很單純的抓取前景正在執行的程式名稱的功能,也變得愈來愈複雜。之前只需要抓一下getRunningTasks() 就可以找到想要的結果,但是現在得要為 application 加上新的 permission PACKAGE_USAGE_STATS,然後還要要求使用者進到 Settings > Security > User Apps with access to usage data , 勾選該 application 後才可以。

這種小功能還需要使用者大費周章的做一堆事,實在是很麻煩。

下面的程式碼,是透過 user stats去取得這資料。另外,如果使用者還沒有在 Settings 中勾選 app 的話,出來的 runningTask 會是空的,這時應該要用程式碼中最下面的 startActivity 去把設定的畫面叫起來,叫使用者打勾。使用者不打勾,你還是抓不到資料的。



七月 22, 2014
» AutoScreenOnOff 再升級

(Dansui.Taiwan)

前幾天花了半天加入 app 黑名單的功能後,評價還不錯,至少止血了。今天趁著明天颱風要來早點回家,花了一個多小時,把一個單純的 screen off widget 給加了進去。

花的時間比預料的快很多,因為本來在 notification 的互動中就已經有這個 intent 和處理邏輯在了,今天只是照著原本的 widget 再生一個出來,然後設定個 pendingIntent 給它,就收工了。

另外,還隨手將 changelog 的輸出方式改了一下,改成每個版本都會是一個新的 string ,如果其他語言沒有翻譯的話,那自然會抓到預設英文的版本,省下我每次升級都要把同樣的字串貼到每個語系的changelog_html中。

六月 22, 2014
» 如何讓 Android 中的 Activity 不要顯示任何畫面

(ShinShan.Dream Lake.Taipei)

北部難得可以找到人少一點的景點。
之前來路跑竟然沒有看到。

*****
最近又在寫小 app,但總是在畫面上卡關,一直無法很順利的進行。
今天早上起床,順手又改了幾個自己在使用上覺得不夠方便的地方,
然後,就是這麼自然地,在網路上逛到了如何解決自己試了很久都沒成功的功能。

根據 app 的需求,我在 AndroidManifest.xml 中透過 intent filter來接收某些事件。當事件發生時,被叫起的 Activity 其實並不需要顯示畫面,我只是要將事件再傳給 Servic,讓 Service 把事情處理掉。但是卻老是卡在 Activity 或多或少會秀一下白畫面,或黑畫面,或是閃一下,才會乖乖的不見。即使在 onCreate() 中呼叫了 finish()也解決不了這個問題。

今天在網路上找到的解法,很簡單。只需要設定一個 theme 就好了。雖然這方法自己也試過,但應該是少了些什麼其他的設定吧。好吧,答案就是:

            android:theme="@android:style/Theme.NoDisplay"

REF:
http://stackoverflow.com/questions/4551868/how-to-completely-get-rid-of-an-activitys-gui-avoid-a-black-screen



五月 25, 2014
» Sqlite Commands in Terminal

(Shimen.Taiwan)

Recently, I need to use sqlite commands to check data for my android app. Thanks to the rooted android ROM, the shell environment is much useful than before. It's possible to use Tab key to auto complete filenames and directories, and I can use sqlite3 command directly in adb shell. It saved time from pulling the database file out back and forth.

However, it's been quite a while that I almost forgot how to deal with a sqlite database. The following link is a handy Url to help me on this:

http://www.sqlite.org/cli.html

To name a few:
.mode  // select different modes, eg, list, line, column
select * from tbl1; // select all from table tbl1
 



六月 10, 2013
» 在Lime HD中快速啟動語音輸入法

(富貴角.Taiwan)

剛剛試了一下谷歌的語音輸入,覺得有些時候應該蠻實用的。所以打算幫它在Lime HD中加個快速切換的功能。在Lime HD中原本就有開啟Google語音輸入法的方式,只是我覺得有點太麻煩了。原本的方式是:
1. 長按左下方的設定鈕,這時會跳出一個對話視窗,可以設定輸入簡繁中文的切換,分割鍵盤等一些選擇。畫面最下方則是切換成語音輸入法。

通常會想用語音輸入的時機,都是在手不方便慢慢點螢幕的時候,所以,操作步驟愈多,就愈麻煩。原本我是打算在長按(中/En)按鈕時,直接啟動語音輸入。目前這個按鈕的長按並沒有預設的作用,所以很適合。

可惜的是,我不知道怎麼debug InputMethodService,所以沒有辦法短時間內看清楚在LIMEService中的長按功能,是怎麼前後串起來的。一直找不到好的interception point。

無意中卻發現了,原來Lime有針對輸入的整個面版實作swipe的key listener!!做為一個多年的Lime HD愛用者,竟然不知道有這麼一回事。看了原始碼後,它的目前對應功能是:
滑上:開啟設定對話窗(跟長按設定按鈕一樣)
滑下:關閉輸入法
滑左:模擬刪除鍵
滑右:送出目前選擇的候選字

既然設定對話窗已經可以很方便地長按設定鍵來開啟,我把"滑上"給改成呼叫Google語音輸入法。程式碼很簡單,在LIMEService中的swipeUp(),把原本的code換成StartVoiceInput()就可以了。

六月 7, 2013
» Adding macros in Android Studio (Intellij) -- take Adding Javadoc comment for example

(Geneve.Swiss) Climbing is pleasant, but it leads you to better views.

 Some shortcuts can be integrated into ideaVIM, but some are more complicated and need other tricks to make it more handy. One of the technique is to use macros. As Mac's Automator, you can ask Intellij or Android Studio to record your actions, and make it available as a keyboard shortcut.

For example, if you want to add javadoc comment to a funtion, you need to move to the first line of a certain function, and type in /**. After pressing Enter, it will generate javadoc template for that specific function. Though it's already very convenient, it still takes time if you're not quite familiar with how to move the cursor to the first line of the function.

So, to add javadoc comment from anywhere in codes, you can record a macro to move to the first line of the func, and type /** , and Enter for you. Here's the actions:
  1. Start recording a a macro in Edit - Macros - Start Macro Recording
    1. Press Down
    2. Press Ctrl + Up
    3. Press ⌘ + Left
    4. Write /**
    5. Press Enter
  2. Stop the macro recording in Edit - Macros - Stop Macro Recording
  3. Give it a cool name
And then you can add a keyboard shortcut for this macro in Keymapping in Preferences.

REF:
http://tobiassodergren.blogspot.tw/2012/05/adding-javadoc-to-method-in-intellij.html

六月 6, 2013
» Some hacking articles about IdeaVIM under Intellij

 (Leiden.Netherlands)

IdeaVIM is a vim-like input plugin for intellij IDE, good for text editing but not so powerful if you want to use it for other IDE tasks, for example accessing menu actions by defining keyboard shortcuts.

Fortunately, someone in Japan branched the source codes on GitHub, and wrote some tweaks to make it possible to configure all kinds of Intellij menu items to keyboard shortcuts as you wish in a config file.

Tried downloading the modified codes and built it in Intellij; installed the plugin in my Android Studio environment. Working like a charm~

my settings in _ideavim.groovy


REF:
http://d.hatena.ne.jp/masanobuimai/20120720/1342764219
http://d.hatena.ne.jp/masanobuimai/20080323#1206277040


六月 4, 2013
» How to make testing work under Android Studio

(Geneve.Swiss)

It's such a pain to look for solutions for tasks on Android Studio. When can it be mature enough and more well documented for developers?

I would like to write some test cases for my small app, but I couldn't find a way for adding test cases into Android Studio project and have it run successfully.

After wandering around on the internet for a long time, eventually I found a link that did helped me out (see reference below). As Google I/O 2013 video said, a new android build system made of gradle is released, which will be more flexible, more powerful, more etc, etc. However, currently, it 's not well integrated into Android Studio. Some modification in Android Studio won't be directly reflected in gradle build scripts. Well... then, how do I know when I should modify build scripts my self? And to write it by myself, I have to learn groovy first, because that's the language Gradle used to write its build configuration files. How could I master these things in a short time...

Anyway, the solution for my pain point above is to modify build.gradle under the root directory of app project, and make sure all the instrument source folders are well set up. Now I can use cradle to run test cases and generate report files. However, I can't make it work in Android Studio still. orz...


REF:
http://blog.crowdint.com/2013/05/24/android-builds-on-travis-ci-with-gradle.html

六月 1, 2013
» Gradle build error in comman line for Android Studio app projects

I saw someone met the same problem on StackOverflow. Fortunately,  I found out how to make it work. So leave some notes here:
1. upgrade Gradle version to 1.6
2. Afterward, running "gradle build --stacktrace", you will see that ANDROID_HOME environment variable is required to be set up.
3. on Mac, add following line to your ~/.bash_profile
export ANDROID_HOME="/Applications/Android Studio.app/sdk/"

Now it should work like a charm!

五月 28, 2013
» OrientationEventListener on Android to get current rotation degree

(Central Park.New York)

Strangely, it's not possible to get current rotation angle in android directly, except for getting the fixed 4 rotation modes: 0, 90, 180, 270. In order to get precise rotation degrees instead of rotation modes, OrientationEventListener can be used. Once it's implemented, you can get the rotation degree in onOrientationChanged(). It's a lot easier than manipulating values from all the sensors like gyroscope, accelerator, etc.

REF:
http://android-er.blogspot.tw/2010/08/orientationeventlistener-detect.html





五月 24, 2013
» Tips for using AdMod module in Android Studio

(Geneva.Swiss)
It's pain in the ass to use Android Studio now, since it's just published, which implies bugs appear every now and then, and you're not sure it's due to your misunderstanding of this IDE, or it's really an issue. In addition, it's  relatively hard to find How-to answers comparing to ADT plugin with Eclipse.

While trying to set up AdMob module in my project on Android Studio, it took me some time to fix some external library import errors that should be clearly mentioned in online AdMob doc, or even better, just integrate it into "Android Studio".  Isn't Android Studio meant to make Android developers' life easier?

Problem 1: Add com.google.ads.AdView element  in my layout xml as doc says, but it generates compilation error.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
             
android:orientation="vertical"
             
android:layout_width="fill_parent"
             
android:layout_height="fill_parent">
 
<com.google.ads.AdView android:id="@+id/adView"
                         
android:layout_width="wrap_content"
                         
android:layout_height="wrap_content"
                         
ads:adUnitId="MY_AD_UNIT_ID"
                         
ads:adSize="BANNER"
                         
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
                         
ads:loadAdOnCreate="true"/>
</LinearLayout>
Anser: Pay attention!! You also have to add the red line attributes to the container layout!! It's not clearly mentioned in document. What I just did in adding com.google.ads.AdView component. Obviously, it told me that it can't compile.

Question 2: Don't know how to add the AdMob library to my project in Android Studio. The AdMob doc only has flows for eclipse. I always meet "NoClassDefFoundError" during runtime. This took me most time to wandering on the internet, looking for a right answer.

Answer: it's a bit tedious. Please just check detail information in reference link below. In brief:
1. Put the jar into the libs folder
2. Right click it and hit 'Add as library'
3. Do a clean build (you can probably do this fine in Android Studio, but to make sure I navigated in a terminal to the root folder of my app and typed gradlew clean (I'm on Mac OS X, the command might be different on your system)

Question 3: sometimes, even questions above are all fixed, you still can't get the AdMob View. Why? You have to declare a specific Activity for it including configChanges. This is mentioned in Introduction, but ... I just have ignored it, since who would have thought that creating a View in my own activity needs to declare a specific Activity for it?

    <activity android:name="com.google.ads.AdActivity"
             
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
 

REF:
Add a jar library








五月 20, 2013
» Enhance Reading Experience for Android Dev website


Recently I spend most of my time reviewing documents on google's android official site. It annoys me that the left side of the screen is always occupied by the content index; however, I just need to change articles once in a while by using it. It makes me feel bad that one third of the screen is wasted. In addition, it also distracts me while I was reading.

As a consequence, I searched internet to see if there's any extension, plugin, or script to help me hide the navigation panel when I want to do so. Out of luck, no. Maybe it's too tiny to have someone work on it.

Therefore, I decided to write one by myself, after reading a book about jQuery. My choice is to write a userscript for greasemonkey, which is supported on Chrome now as well. The logic is very simple and the lines of codes are few, although it still took me sometime to find right APIs to implement this feature.

Here's the screenshots:

The userscript is uploaded to following site:
http://userscripts.org/scripts/show/167980

五月 15, 2013
» Eclipse/Java code completion not working with Android development

(GreatWall.Beijin.China)

While watching some video tutorials about android development, I found that the lecturer can call out the class completion helper dialog during typing class names. However, I only have the function/variable autocompletion feature after typing a . after a class name in my eclipse. Without entering the class name correctly, I can not get any help from eclipse, which is quite strange comparing to what I've seen for others.

Well, it seems that many people encountered the same problem as I did. This is being discussed on various posts. One way to make it work is to check the "Java Proposal" in Eclipse's Preferences > Java > Editor > Content Assist > Advanced'.

But for me, it seems that something's wrong with my workspace configurations too. The answer from hoipolloi in following reference post helped me to fix this issue. Here's his suggestion:
  • Quit Eclipse
  • Go to workspace/.metadata/.plugins/org.eclipse.jdt.core
  • Remove *.index and savedIndexNames.txt
  • Restart Eclipse and search Ctrl+T for the offending type. The indexes will be rebuilt.

REF:
http://stackoverflow.com/questions/908489/eclipse-java-code-completion-not-working

四月 18, 2013
» vrapper

最近常遇到 limeHD 選字不對的問題。不知道能不能加個小方法,讓它可以按著使用者的意思,針對某種打法,強制某個字是自己要的第一候選字呢?所以去抓了limeHD的程式碼,想要來研究研究一下。

為了要在eclipse上順暢地閱讀程式碼,先裝上了仿vi的plugin--vrapper。官網在此:
http://vrapper.sourceforge.net/home/

四月 10, 2013
» How to de-compile android apk for studying purpose

(Redmond.Seattle.US)
Oh, How I miss it.

****
Due to interview purpose, I wanna know some inside implementation details about a certain apk. In order to do further investigation, I searched how to de-compile apk files on the internet. The reference article below is so wonderful that it lists necessary tools and also the commands how to trigger them.

In brief, you need dex2jar and  JD-GUI. If you want resource, or read smali files, apktool will do the trick too.

REF:
https://kkinder.com/2011/11/27/so-you-want-to-reverse-engineer-an-android-app-apk/

三月 27, 2013
» How to find and turn on USB debugging mode in Android 4.2 and higher

(Paris.France)
Tranquille dans la ville de belle Paris.

****
While trying a certain app recently released by a company that I had interviewed with, it crashed every time I triggered it. I wanna get the logs for them to do further investigation. Howevever, I couldn't find the developer option in my Galaxy Nexus with android 4.2.2. Not sure why; I did some search on the internet. Seemd that google thought it's not so useful for normal users so they just hide it from settings app by default. If this feature is required by developers or someone boring enough, just like me, they can always find the solution on the internet: You can enable/disable it whenever you desire by going to “Settings” -> “Developer Options” -> “Debugging” ->” USB debugging”.

Voila, I can successfully get logs by using adb logcat now.

REF:
http://dottech.org/87439/how-to-unlock-usb-debugging-mode-on-android-4-2-jelly-bean-and-higher-guide/

二月 28, 2013
» Android上的日文語音合成方案 SVOX 和 N2 TTS

(Rome.Italy)
快忘了自己曾經去過這個城市,這個國度。
在隨處可見古蹟的城市中游盪,照片怎麼拍,都很賞心悅目。

****
之前在這一篇中介紹到svox這個在android上,提供多國語音合成資料庫的軟體,拿來搭配moon+ reader確實很不錯;可惜的是,在台灣竟然沒有辦法購買它的語言資料庫,只能下載free trial version下來試用個一陣子。雖然網路上有提供怎麼可以變成永久試用版,但這始終不是個好的方法。

所以,又再找了一下,看是不是有可以在台灣購買的軟體,或是免費,但是品質又不會太差的替代方案。N2 TTS似乎是個不錯的選擇。這是由日本KDDI的某個lab製作的,除了有免費版可以使用外,還有官網和twitter帳號。就官網來看,它有以下幾點特色:
 - 不佔容量:有男聲,女聲,總共才5.4MB
 - 不需要網路,android單機即可使用
 - 有男聲也有女聲
 - 符合android標準的TTS 介面 (到Settings中可以做切換,並讓其他ap p使用)
 - 可以個別調整語音 (這功能就得付費才有囉)

馬上把它裝到android上測試,試用okay~~ 聽書去!

REF:
官網
網路上可以下載的地方

一月 23, 2013
» 如何在Moon+ Reader 靜讀天下 中設置直排閱讀以及語音閱讀

(山寺.日本)
幾乎快忘了當初有拍這張照片。記憶力真的愈來愈差了。
幾年前的藏王行,途中有在山寺稍做停留。
山寺是個小鎮,鎮上最有名的(或是目前我還記得的…),就是它建在山上的寺廟。
上山的路很陡,想必當初寺廟蓋得也很辛苦吧。
到了山腰或是山頂,就可以放眼往山谷和另一頭的山看過去。
冬天的時候,雖然顏色比較單調,到處都是白茫茫一片,
但是卻很容易讓人感受到那份靜謚的氣氛。
陽光隨著山巒的起伏,時暗時亮,
整座山的景色變化還是很多彩多姿的。

****
看Google Play Store中針對靜讀天下的介紹,
明明就有提到中文直排閱讀,和簡繁互換的功能,
可是偏偏我在所有的設定選項中就是找不到。
經過一番努力後,發現必須在Moon+ Reader的設定中,
先把語言改成中文(簡或繁),在可視選項中才會冒出相關的設定。
這樣子的差別,也太隱藏了吧。

將直排閱讀的選項勾起來後,雖然畫面還是portrait的,
但是所有中文字會轉90度,讓你得要把device轉成橫的,才可以看。
這時,就是所謂的直排閱讀了。
雖然這方式行得通,但畢竟沒有Kindle android app的直排好。
從日本Amazon下載的書,在Kindle android app中是可以真正的直式閱讀的。
在查單字時,也不用再轉畫面。
可惜在Kindle android app中,要塞書進去的步驟很麻煩,而且格式也不同,
所以目前也只能先用Moon+ Reader的直排閱讀頂著先。

****
再來是語音部分。
找資料的時候,剛好看到有人在介紹靜讀天下的語音朗讀,
所以這邊也記錄一下。
Nexus7官方的語音資料只有兩三個西方語系,
文章中是介紹要安裝另一個語音系統 -- SVOX。
試過之後的感覺,覺得比以前Kindle DX上的人聲朗讀還要真實。
拿來唸唸日文小說,應該很不錯。

參考網址如下:
http://jdev.tw/blog/2764/android-text-to-speech

一月 30, 2011
» Tunein Radio, Moon+ Reader, IM+

photo
Taipei.Taiwan

最近開始比較有在把Android的手機當成日用機,
所以找了一些平常比較需要使用到的軟體。
第一個經常會用到的是廣播軟體。
從很久以前就很喜歡聽網路廣播,除了可以學習語言外,
還可以挑點話少音樂多的電台,又不怕因為收訊不好而充滿雜音。
由於自己比較需要聽各地的電台,電台數的多寡成了我選擇軟體的重要考量。
以前使用的是TuneWiki,它可以連結Shoutcast,所以電台數還算多,
但是主要是線上的電台。
後來我找到了Tunein Radio這軟體,它的出現,讓我完全把TuneWiki丟到了一旁。
電台數量不僅多,而且可以依城市、語言分類。
每個電台還可以選擇想要的串流頻寬(如果原站台有支援的話)。
介面也不花俏,完全是以列表的方式呈現,很容易上手。
在Android上,是免費的。
強烈建議愛聽廣播的人安裝。

看Tunein Radio做得這麼好,想說在iPad或iPhone上不會也有版本吧。
果然沒猜錯!
而且在iPad上,針對介面有特別設計過,不再像Android上以便利簡潔為主,
電台的圖案都以更大圖示來表示,如果該電台有節目表的話,也可以在該軟體上呈現。
不過,在iPad上就花錢買才行了。才$0.99,二話不說我就買下去了。

****

Moon+ Reader,之前一直有在注意電子書軟體,
不過隔了好一陣子都沒有再接觸,只專心地使用iPad在看書。
因為Android手機不離手,所以在線上查了一下,
看是否有比較順手的軟體可以在短暫休息時能翻翻書解悶。
除了早期的Aldiko外,另外找到了Moon+ Reader。
一用之下,嚇了一跳,功能這麼好的軟體,竟然還是免費的!(當然也有付費版)
很多設計都是我想要的,所以用起來格外開心。
有點相見恨晚的感覺。
現在iPhone和android上的軟體,都是以幾十萬在計算的,
要從中找到適合自己的軟體,也要有點本事吧。

ps. 可以連結ColorDict查單字,這個功能終於有人做了~~
在Cool Reader上也行,不過,Cool Reader的操作方式一直讓我很困惑。

****

最近會躺在床上用iPad上網,偶爾也會msn一下。
把iPhone上買的Beejive IM裝到iPad上來,對話窗會怪怪的,
又不想再花另一筆錢買iPad專用版。
所以又去裝了個免費的IM+
IM+有針對iPad做設計,所以用起來順手多了,
而且還是免費的。


REF:
Tunein Radio
Moon+ Reader 介紹
IM+

二月 23, 2010
» Development on android

PB124730
Annecy.France

看了點文件,想說就來寫寫東西吧,從最簡單的開始。

之前在國外時,為了學法文,幫onlinedic寫了個Vocabulary Review的小程式在Windows Mobile上。讓自己可以用當時帶出去的dopod818加減看一下查過的單字。現在,當時的code已經都不見了,所以想說在android上也寫一個好了。反正onlinedic的export機制都還是okay的。

在寫程式時,也可以順便練習到android上的一些基本觀念,比方說:如何透過intent來叫起另一個activity;如果在叫起別的activity時,帶想要的參數給它;透過xml的UI建置方式,畫面橫轉正轉的處理;UI元件的動作指派;目錄結構的讀取,文字檔的讀取;簡單的webview應用;activity暫存狀態的記錄;功能權限的開啟;androidManifest.xml的基本設定;eclipse開發環境的熟悉等。

真的寫下去了,明明是個很簡單的程式,但也弄了一天多才寫好,花了許多時間在小細節上。如果不把除錯、追臭蟲方式弄熟,接下來的日子應該會很痛苦吧。

= = =
如果有空的話,乾脆一不做二不休,把onlinedict也搬到android,這樣子就不用每次都要裝一堆字典了。在網路上找到open source,可以讀取stardict字典檔的程式,有空再研究吧。先記下link:
http://code.google.com/p/toolkits/wiki/YAStarDict

另外,沒想到有人替eblib包了個python wrapper!!
當初在onlinedict裡亂寫的鄨腳command line程式,應該可以用這個wrapper把它換掉吧,省下暫存文字檔輸入輸出的廢工。
也是…改天有空再試吧。
Link: http://code.google.com/p/pyeb/

biggo.com.tw

A Django site.