2014年3月25日 星期二

Change image when click the UI components

在按下按鈕時,時常都需要有兩張以上的圖片切換,而這篇文章,將紀錄以XML檔案的方式,按下按鈕時自動切換圖片。


首先,先將兩張圖片放置  (project)/res/drawable-mdpi/.. 的目錄下

btn.png

btn_down.png
 
 
接著在drawable-mdpi的目錄下按下右鍵 -> new Android XML,檔案名稱btn_selector,並選取selector,如下圖:
 
 
完成之後,會出現一個btn_selector.xml在drawable-mdpi目錄中,將以下程式碼貼入
 
btn_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/btn_down" />
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/btn_down" />
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/btn_down"/>
    <item android:drawable="@drawable/btn" />
</selector>
layout: activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.widget.Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_selector" />

</LinearLayout>
avtivity: MainActivity.xml:
package com.main;
import com.example.android_ui_changeimagewhenclick.R;
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 }
}




2014年3月24日 星期一

Get Signal Strength - GSM and CDMA


MainActivity.java

package com.example.sample_phonesignalstrength;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.widget.TextView;

public class MainActivity extends Activity {
 private TextView textView1;
 private TelephonyManager mTelephonyManager;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  textView1 = (TextView) this.findViewById(R.id.textView1);
  
  mTelephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
  mTelephonyManager.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
 } 
 private class MyPhoneStateListener extends PhoneStateListener{
  @Override
  public void onSignalStrengthsChanged(SignalStrength signalStrength) {
   super.onSignalStrengthsChanged(signalStrength);
   if(mTelephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM){
    textView1.setText("GSM Strength"+signalStrength.getGsmSignalStrength());
   }
   else if(mTelephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA){
    textView1.setText("CDMA Strength"+signalStrength.getCdmaDbm()+" dBm");
   }
   else{
    textView1.setText("Unknown PhoneType: "+mTelephonyManager.getPhoneType());
   }
  }
 }

}


activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="TextView"
        android:textSize="30sp" />

</RelativeLayout>


2014年3月21日 星期五

Debug Log - print className, methodName and lineNumber

StackTraceElement t = Thread.currentThread().getStackTrace()[2];
//fullClassName contains the package name.
String fullClassName = t.getClassName();            
String className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1);
String methodName = t.getMethodName();



int lineNumber = Thread.currentThread().getStackTrace()[2].getLineNumber();

Gallery use BaseAdapter

Gallery 模擬結果:
備註:向左向右均可滑動,滑動長度趨近於無限,大約可滑動2^30/選項次

MainActivity.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.app.Activity;

public class MainActivity extends Activity implements OnItemClickListener
{
 private Gallery gvCam;
 private ImageAdapter mAdapter;
// private int[] image = {R.drawable.dafault_video, R.drawable.dafault_video, R.drawable.dafault_video, R.drawable.dafault_video};
 private String[] imgText = {"CH1", "CH2", "CH3", "CH4"};
 private ArrayList<Map<String,Object>> List = new ArrayList<Map<String,Object>>();
 @Override
 protected void onCreate(Bundle savedInstanceState) 
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  //insert the image or the bitmap or the text
  for(int i = 0;i<4;i++)
  {
    Map<String, Object> map = new HashMap();
//    map.put("CamImage", image[i]);
    map.put("CamName", imgText[i]);
    List.add(map);
  }
  //
  
  //initial UI
  gvCam = (Gallery) findViewById(R.id.xml_gyCam);
  mAdapter = new ImageAdapter(this ,
                         List,
                         R.layout.gallery_view,
                         new String[]{"CamImage", "CamName"},
                         new int[]{R.id.xml_ivCamImage,
                         R.id.xml_tvCamName});
  gvCam.setOnItemClickListener(this);
  gvCam.setAdapter(mAdapter);
  gvCam.setSelection((Integer.MAX_VALUE / 2) - (Integer.MAX_VALUE / 2) % List.size());
  //
 }
 
 
 
 @Override
 public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) 
 {
  // TODO Auto-generated method stub
  //check click item
  Log.e("GalleryExample","Your clicked item = "+position % List.size());
  //
 }
}

ImageAdapter.java
import java.util.ArrayList;
import java.util.Map;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ImageAdapter extends BaseAdapter
{
    private LayoutInflater mInflater;  
    private ArrayList<Map<String, Object>> list;
    private int layoutID;
    private String flag[];  
    private int ItemIDs[];
 
    public ImageAdapter(Context context, ArrayList<Map<String, Object>> listItems, int layoutID, String flag[], int ItemIDs[]) 
    {     
        this.mInflater = LayoutInflater.from(context);  
        this.list = listItems;  
        this.layoutID = layoutID;  
        this.flag = flag;  
        this.ItemIDs = ItemIDs;
    }
 
 @Override
 public int getCount() 
 {
  // TODO Auto-generated method stub
  return Integer.MAX_VALUE;
 }

 @Override
 public Object getItem(int position) 
 {
  // TODO Auto-generated method stub
  if (position >= list.size()) 
  {
   position = position % list.size();
  }
  return position;
 }

 @Override
 public long getItemId(int position) 
 {
  // TODO Auto-generated method stub
  if (position >= list.size()) 
  {
   position = position % list.size();
  }
  return position;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) 
 {
  // TODO Auto-generated method stub
     if (position >= list.size()) 
     {
      position = position % list.size();
     }
        convertView = mInflater.inflate(layoutID, null); 
         for (int i = 0; i < flag.length; i++)
         {  
             if (convertView.findViewById(ItemIDs[i]) instanceof ImageView) 
             {  
                 ImageView iv = (ImageView) convertView.findViewById(ItemIDs[i]);
                 if(list.get(position).get(flag[i]) != null)
                 {
                  if(list.get(position).get(flag[i]) instanceof Bitmap)
                  {
                   iv.setImageBitmap((Bitmap) list.get(position).get(flag[i]));
                  }
                  else
                  {
                   iv.setImageResource((Integer) list.get(position).get(flag[i]));
                  }
                 }
             } 
             else if (convertView.findViewById(ItemIDs[i]) instanceof TextView) 
             {  
                 TextView tv = (TextView) convertView.findViewById(ItemIDs[i]);
                 tv.setText((String) list.get(position).get(flag[i]));
             }
         }
         return convertView;  
 }

}

activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:orientation="vertical" >

        <Gallery
            android:id="@+id/xml_gyCam"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

</RelativeLayout>

gallery_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:background="#000000" >

        <ImageView
            android:id="@+id/xml_ivCamImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_margin="10dp"
            android:src="@drawable/dafault_video" />

        <TextView
            android:id="@+id/xml_tvCamName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/xml_ivCamImage"
            android:layout_alignTop="@+id/xml_ivCamImage"
            android:layout_marginRight="15dp"
            android:text="CH"
            android:textColor="#00FF54"
            android:textSize="10sp" />

    </RelativeLayout>

</LinearLayout>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.gallery_example"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.gallery_example.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

default_video.png

2014年3月17日 星期一

Non-Blocking UDP Client Sample

Non-Blocking UDP Client 模擬結果:
備註:輸入IP與Port後啟動Client,發送任意訊息後,Client會收到Server的回覆

MainActivity.java

Non-Blocking UDP Server Sample

Non-Blocking UDP Server 模擬結果:
備註:選定Port後啟動Server等待Client發送,收到Client訊息後,Server會回覆給Client

Non-Blocking UDP Server範例如下:

Get the version name and version code from AndroidMenifest.xml


通常我們都會在AndroidManifest.xml中,添加程式版本名稱和版本資訊,如下圖



In the MainActivity.java
 
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 //toast version
 try {
  int versionCode = this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionCode;
  String versionName = this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName;
  String toastString = versionCode + "/" + versionName;
  Toast.makeText(this, toastString, Toast.LENGTH_SHORT).show();
 } catch (NameNotFoundException e) {
  Toast.makeText(this, "NameNotFoundException", Toast.LENGTH_SHORT).show();
 }
 //
}

2014年3月10日 星期一

ListView use SimpleAdapter

JTimer

Ping

Ping指令使用方法如下:


// -c : the times for execute ping.
// -w : timeout to wait for ping response
Process p = Runtime.getRuntime().exec("ping -c 1 -w 10 www.google.com");
// send 1 packet to "www.google.com", waiting for response in 10 seconds.
if(p.waitFor() == 0) {//blocking 
 //ping succeeded
 ...   
}
else {
 //ping failed
 ...   
}


張貼範例

在撰寫文章時,有"撰寫"和"HTML"兩個選項,
選用HTML編輯,
張貼程式碼時,
依照下面格式張貼:

<pre class="brush: java;" name="code">
 ....撰寫程式碼
</pre>

輸出結果會如下:
class DemoApp {
    public static void main(String[] args) {
        Demo d = new Demo();
        d.printMessage();
    }
}
 
class Demo {
    String message = "NTUT 214 無敵";
     
    public void printMessage() {
        System.out.println(message);
    }
}
其中比較常用到的特殊符號要置換成別的字元:
'&' (ampersand) becomes '&amp;'
"' (double quote) becomes '&quot;' when ENT_NOQUOTES is not set.     
''' (single quote) becomes '&#039;' only when ENT_QUOTES is set.     
'<' (less than) becomes '&lt;'
'>' (greater than) becomes '&gt;'