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

Sunday, October 20, 2013

Create custom button with round corner and shadow by XML

To create a button in Android app like this:


The layout XML:
<Button

    android:layout_width="match_parent"

    android:layout_height="80dip"

    android:background="@drawable/hi"

    android:text="@string/click_button"

    android:textColor="#999"

    android:textSize="18dip"

    android:textStyle="bold" />
The drawable file hi.xml:
<?xml version="1.0" encoding="utf-8"?>

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- Bottom 2dp Shadow -->

    <item android:bottom="11px" android:top="19px" android:left="10px" android:right="10px">

        <shape android:shape="rectangle" >

            <solid android:color="#ccc" />

            <corners android:radius="7dp" />

        </shape>

    </item>

    <!-- White Top color -->

    <item android:bottom="15px" android:top="15px" android:left="10px" android:right="10px">

        <shape android:shape="rectangle" >

            <solid android:color="#FFFFFF" />

            <corners android:radius="7dp" />

        </shape>

    </item>

</layer-list>

Wednesday, October 16, 2013

Convert hex string to color in Android

I got a hex string from resource file, looks like this "#FF00BB00". I wanna to use it as a view's background color. In Java there is a function Color.decode(String), while it is not existed on Android.

But after read the android.graphics.Color of Android quickly there is a same and useful function Color.parseColor(String), just be careful the string shall format to "#FF123456" or "#123456".

Here is the code:
imageView.setBackgroundColor(Color.parseColor("#FF00BB00"));

Enable HP Zbook Thunderbolt 3 Dock on Ubuntu

As mention in previously post, I installed Ubuntu on my HP ZBook. After two days used all things work just fine, but the Thunderbolt 3 Dock ...