技术小黑屋

Set the Preferred Application in Android

A great application should provide a function to let users set it as the preferred or default application. In this post, I will make a description on PreferredApplication
Before an application is about to be set as default, it has two possibilities. One is that there is already a default application and the other is no default application.

日语文法:ください

##Nをください

  • 意思:请…
  • 例句:
  • りんごをください  请把苹果给我
  • ほんを私にください 请把书给我一下。

Fix Missing Command Takes Too Long to Respond in Fedora

I am getting well on with Fedora now. However when I was a fresher to Fedora, I have met a lot of problems.
One of them is that When I type some wrong commands It will take too long to respond. It’s totally different from Ubuntu,which I used before. However I like it could quickly reponse even through the command does not exist.

1
unset command_not_found_handle

Ok.Add the above code to .bashrc And it works.

##Others

Refer String Resources From Other Applications

The following code works. Here take getting String resource for example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void testUseAndroidString() {

    Context context = getContext();
    Resources res = null;
    try {
        //I want to use the clear_activities string in Package com.android.settings
        res = context.getPackageManager().getResourcesForApplication("com.android.settings");
        int resourceId = res.getIdentifier("com.android.settings:string/clear_activities", null, null);
        if(0 != resourceId) {
            CharSequence s = context.getPackageManager().getText("com.android.settings", resourceId, null);
            Log.i(VIEW_LOG_TAG, "resource=" + s);
        }
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    
}

###Others

Get Android System Available Features

An easy way to get system available features.

1
2
3
4
5
6
7
private void dumpSystemFeatures() {

    FeatureInfo[] features = this.getPackageManager().getSystemAvailableFeatures();
        for (FeatureInfo f : features) {
            Log.i(LOGTAG, "dumpSystemFeatures f" + f);
        }
}

Have a glance at the result

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d74fd0 android.hardware.wifi fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d75048 android.hardware.location.network fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d750d8 android.hardware.nfc fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d75150 com.google.android.feature.GOOGLE_BUILD fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d751f0 android.hardware.location fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d75270 android.hardware.sensor.gyroscope fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d75300 android.hardware.screen.landscape fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d75390 android.hardware.screen.portrait fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d75420 android.hardware.wifi.direct fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d754a8 android.hardware.usb.accessory fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d75530 android.hardware.bluetooth fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d755b0 android.hardware.touchscreen.multitouch.distinct fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d75660 android.hardware.microphone fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d756e8 android.hardware.sensor.light fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d75770 android.hardware.camera.autofocus fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d75800 android.software.live_wallpaper fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d75890 android.hardware.camera.flash fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d75918 android.hardware.telephony fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d75998 android.software.sip fl=0x0}
......

##The Other way You can also use adb shell to get into the shell of your device and the use pm list features to get the available features of a device.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
root@android:/ # pm list features
feature:reqGlEsVersion=0x20000
feature:android.hardware.bluetooth
feature:android.hardware.camera
feature:android.hardware.camera.autofocus
feature:android.hardware.camera.flash
feature:android.hardware.camera.front
feature:android.hardware.faketouch
feature:android.hardware.location
feature:android.hardware.location.gps
feature:android.hardware.location.network
feature:android.hardware.microphone
feature:android.hardware.nfc
feature:android.hardware.screen.landscape
feature:android.hardware.screen.portrait
feature:android.hardware.sensor.accelerometer
feature:android.hardware.sensor.compass
feature:android.hardware.sensor.gyroscope
feature:android.hardware.sensor.light
feature:android.hardware.sensor.proximity
feature:android.hardware.telephony
feature:android.hardware.telephony.gsm
feature:android.hardware.touchscreen
feature:android.hardware.touchscreen.multitouch
feature:android.hardware.touchscreen.multitouch.distinct
feature:android.hardware.touchscreen.multitouch.jazzhand
feature:android.hardware.usb.accessory
feature:android.hardware.usb.host
feature:android.hardware.wifi
feature:android.hardware.wifi.direct
feature:android.software.live_wallpaper
feature:android.software.sip
feature:android.software.sip.voip
feature:com.cisco.anyconnect.permissions.patch.htc
feature:com.htc.android.rosie.widget
feature:com.htc.lockscreen.fusion
feature:com.nxp.mifare

For a detailed understanding of use-filter,please read this post http://developer.android.com/guide/topics/manifest/uses-feature-element.html

###Others

Get an Application Required Features

How to get application’s required features? Actually the aapt really does a great help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#Usage:aapt dump badging apk_location | grep feature 

#Example
aapt dump badging /tmp/language_check_maxthon_99985f_2793_4.1.3.1000_remote_develop.apk | grep Features

#Result
uses-feature-not-required:'android.hardware.location'
uses-feature-not-required:'android.hardware.location.network'
uses-feature-not-required:'android.hardware.location.gps'
uses-feature-not-required:'android.hardware.screen.portrait'
uses-feature-not-required:'android.hardware.telephony'
uses-feature-not-required:'android.hardware.wifi'
uses-feature:'android.hardware.touchscreen'
uses-implied-feature:'android.hardware.touchscreen','assumed you require a touch screen unless explicitly made optional'

To understand the feature more detailed, please visit http://developer.android.com/guide/topics/manifest/uses-feature-element.html

###Others

Jar Mismatch! Fix Your Dependencies

There was a requirement of my work. It requires me to integrated my current project with Facebook SDK for measuring. However this came into my sights.

1
Jar mismatch! Fix your dependencies 

The fact is that Both my project and my library project which the former refers to have used android-support-v4.jar. However I realize the two android-support-v4.jar are different after making the md5 hash.
My solution:
I use the android-support-v4.jar in my project as the right one. And then replace the one in Facebook SDK with my project one. And then it works.
But my question remains; why it asks me for fix the dependencies to use the same lib jar file?
I guess android will keep only one file for all the references so this will ask developers to make all the same lib all the same. Sorry for the codeless post.

###Others

How to Create Facebook Key Hash

When I create a new application on Facebook, I meet the problem. Facebook asks me to provide the Key Hash. But it does not show the guidance about how to generate. After Googling, I have found this works for me.
Before executing the following command, you need install openssl

1
keytool -exportcert -alias your-alias-value -keystore your-keystore-path | openssl  sha1 -binary | openssl  base64

Replace your-alias-value and your-keystore-path with the real data.

###Others

Dump Table Structure in SQLite3

Best answer to the question
Use PRAGMA table_info

This pragma returns one row for each column in the named table. Columns in the result set include the column name, data type, whether or not the column can be NULL, and the default value for the column. The “pk” column in the result set is zero for columns that are not part of the primary key, and is the index of the column in the primary key for columns that are part of the primary key.

Reference:http://www.sqlite.org/pragma.html#pragma_table_info

1
2
3
4
5
6
7
8
sqlite> .header on
sqlite> PRAGMA table_info(password);
cid|name|type|notnull|dflt_value|pk
0|_id|INTEGER|0||1
1|host|TEXT|0||0
2|username|TEXT|0||0
3|password|TEXT|0||0
#password is the name of a table.

Another answer may also work
.schema [tablename] will show the CREATE statement(s) for a table or tables

1
2
sqlite> .schema password
CREATE TABLE password (_id INTEGER PRIMARY KEY, host TEXT, username TEXT, password TEXT, UNIQUE (host, username) ON CONFLICT REPLACE);

###Others

Check if a Python Module Is Installed

I was once stucked in How to check Whether a Python module has been installed or not. After Googling, I found this trick.
Python allows user to pass command from out of a python file.See here

1
-c cmd : program passed in as string (terminates option list)

The result if we import an installed module

1
2
3
4
20:15:45-androidyue~/osc_git/LnxClient (master)$ python -c "import os"
20:31:24-androidyue~/osc_git/LnxClient (master)$ echo $?
0
#0 means the module has been installed

Now if we import an module which is not installed.

1
2
3
4
5
6
7
20:31:41-androidyue~/osc_git/LnxClient (master)$ python -c "import aaa"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named aaa
20:31:46-androidyue~/osc_git/LnxClient (master)$ echo $?
1
#1 means that module is not installed.

###Others

Java Get Full Exception StackTrace

As a coder, I am always handling exceptions or errors,or in a word throwables. To impove the release build, I need to collect every throwable information.
And I need to get the information as string and post it to the Bug Collect Server. Now here is an easy trick to get stacktrace from a Throwable

1
2
3
4
5
6
7
private String getStackTrace(Throwable t) {

    final Writer result = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(result);
    t.printStackTrace(printWriter);
    return result.toString();
}

###Others

乘手扶梯的正确方法

####起因 之前一直和女朋友有一个讨论,就是搭乘扶梯时究竟时靠左站立,还是靠右站立。起因时从地铁北宫门出来的时候,我要求她站在左侧。
####我的观点

  • 右侧留出来行走,更符合国内靠右行驶的习惯
  • 之前看到过日本的介绍,日本是靠左侧的。

####她的观点

  • 现在很多人都是靠右站立
  • 她见过写着靠右站立的提示

####事实

  • 左行右立(香港普遍称为左行右企)是指在歐美、香港、台灣、西日本等地於乘坐電扶梯时电梯左边空出留给有急事的乘客行走,只是站立在电梯上的人靠右侧站立的行为。與之相反的是,東日本、澳洲、新加坡等地卻採用右行左立的方式。
  • 此方式為一約定俗成之法則。香港地鐵(今港鐵)及台北捷運於從前曾參考歐美等地的習慣,宣傳「左行右立」,疏通繁忙時段的人流,但不奏效。在香港停止宣傳後,「左行右立」卻反而流行起來。
  • 但在電扶梯上步行會導致容易失去平衡,釀成意外,亦容易導致重力分布不均,增加電扶梯容易損壞的機率。[2]香港政府提醒市民盡量不要在電梯上走動[3],而台北捷運公司目前也基於上述之理由,不再宣傳此措施,然而大部分乘客均已養成左行右立的搭乘習慣;相較之下,台灣的另一座大眾運輸系統——高雄捷運即未若台北一般廣泛宣傳左行右立的習慣。
  • 2011年北京地铁动物园站出现电动扶梯事故后,京华时报指出左行右立是不安全的行为,可能是造成电动扶梯事故的诱因之一。
  • 左行右立是上海市迎接中国2010年上海世界博览会宣传倡导的城市文明行为之一。為宣傳“左行右立”标准,上海轨道交通扶手电梯处常见「左行右立」的標語,並指即使乘客站立不穩向後倒下,後面的乘客也可以支撐,不會出現骨牌效應,有利安全。[5]但自2012年12月开始,因为扶梯台阶过大,行走不安全,上海轨道交通不再强调左行右立,一些原先粘贴在扶梯上倡导左行右立的贴纸已被移除 以上事实部分参考维基百科 http://zh.wikipedia.org/wiki/%E5%B7%A6%E8%A1%8C%E5%8F%B3%E7%AB%8B 或者 Google 搜索 左行右立

####总结

  • 就按我输了作为结果吧,哈哈。
  • 靠左靠右并无硬性规定,社会活动中多礼让,不妨碍他人

###Others

Read Output From Shell

Python provides a lot of method to read output from a just executed shell. However many of them has been deprecated(Not recommened). But subprocess works at present compared to other methods.

1
2
3
4
5
6
7
8
9
10
11
12
from subprocess import Popen,PIPE,STDOUT
 
def readFromCommand(command) :
    p = Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
    result = p.stdout.read().strip()
    return result
    
print readFromCommand('ls')
#result
#0001-.patch
#0001-.patch.zip
#0001-Replace-app_name-into-Browser.patch

A detailed description about subprocess has been written down here. http://docs.python.org/2/library/subprocess.html

###Others