Tuesday, October 28, 2014

Get Android Kernel log

 To catch Kernel log, need a rooted Android device and adb shell worable, then use the adb logcat command:

$adb shell logcat -v time -f /dev/kmsg | cat /proc/kmsg

To  write the output to a file:
$adb shell logcat -v time -f /dev/kmsg | cat /proc/kmsg > /sdcard/log.txt

Thursday, April 24, 2014

Get navigation bar (TSB bar) height

The dim value is defined as com.android.internal.R.dimen.navigation_bar_height.

 final int naviHeight = mContext.getResources().getDimensionPixelSize(
                    com.android.internal.R.dimen.navigation_bar_height);

Monday, April 14, 2014

How to exclude activity from recent application list on Android

To forbidden your activity in the recent application list, just define this parameter in AndroidManifest.xml for the activity:

android:excludeFromRecents="true"

for example:
        <activity
            android:name="com.test.MainActivity"
            android:noHistory="true"
            android:screenOrientation="portrait"
            android:excludeFromRecents="true"
            android:label="@string/app_name">
            ......
         </activity>

Wednesday, February 26, 2014

Monitor screen ON and OFF

Has to register receiver via function registerReceiver(), so usually have to create a service for it.

public  class UpdateService extends Service {

    BroadcastReceiver mReceiver = new BroadcastReceiver {
         private boolean screenOff;

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                screenOff = true;
            } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                screenOff = false;
            }

            handleScreenAction(screenOff);
        }
    }

    private void handleScreenAction(boolean screenOff) {
        if (screenOff) {
            // your code
        } else {
            // your code
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        // register receiver that handles screen on and screen off logic
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        registerReceiver(mReceiver, filter);
    }

    @Override
    public void onDestory() {
        super.onDestory();
        unRegisterReceiver(mReceiver);
    }

    public void onStart(Context context, Intent intent, int startId) {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}

Tuesday, February 25, 2014

Detect headset / Bluetooth status

need this permission:
<uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>

and code snap:
                AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
                if (audioManager.isWiredHeadsetOn()
                        || audioManager.isBluetoothA2dpOn()
                        || audioManager.isBluetoothScoOn() ) {
      // headset connected
}

How to detect whether phone is in charging

Does not need register a broadcast receiver. Call function
registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)), and it will return anIntent with battery status immediately, because Intent.ACTION_BATTERY_CHANGED is a sticky broadcast.
Here is code snap:
        Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        boolean cableCharge = plugged == BatteryManager.BATTERY_PLUGGED_USB
                || plugged == BatteryManager.BATTERY_PLUGGED_AC;
        boolean wirelessCharge = plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;



Friday, February 21, 2014

Translate touch event coordinates to the parent

The touch event coordinates can be got in onTouch() by MotionEvent:getX() and

MotionEvent:getY(), the coordinates are based on currently view. To translate to the parent layout, just append the current view positions to them like this:

 

view.getLeft() + motionEvent.getX();
view.getTop() + motionEvent.getY(); 

 

Haptic feedback (vibration) on Android

Simple use View:performHapticFeedback() to play a vibration:

View view = findViewById(...)
view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);

Thrift fails on Windows - thrift failed error: The command line is too long.

Meet a thrift compiling failure on my project on Windows 7. Using Maven in IntelliJ so I choiced maven-thrift-pluging for thrift files. For ...