2013年9月5日

Android─開啟wifi與bluetooth及偵測wifi與bluetooth狀態

Wifi開啟及狀態偵測

首先需要在Manifest.xml 加入以下三個permission





然後在程式內加入

private WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE)
if (wifiManager.isWifiEnabled()) //判斷目前wifi狀態 開啟會回傳true
    wifiManager.setWifiEnabled(false);   //把wifi關閉
else
    wifiManager.setWifiEnabled(true); //把wifi開啟

bluetooth開啟及狀態偵測

首先需要在Manifest.xml 加入以下兩個permission

然後在程式內加入

private BluetoothAdapter mBluetoothAdapter  = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter.isEnabled())\\判斷目前bluetooth狀態 開啟會回傳true
    mBluetoothAdapter.disable();  //將bluetooth關閉
else
    mBluetoothAdapter.enable();  //將buletooth開啟

參考網頁
http://goo.gl/A6anzN 
http://goo.gl/11Qi7Y 
http://goo.gl/Slssm 
http://goo.gl/xgLsp

Android─保持不休眠狀態

在Android上,要讓你的app避免進入休眠模式
有兩種方式可以使用

第一種是在Manifest.xml文件裡加入user-permission

  • <uses-permission android:name="android.permission.WAKE_LOCK">

第二種是直接在所執行的Activity中加入程式碼

getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

上述程式碼需
加在 setContentView(R.layout.activity_network_testing);   setview的程式碼之前


2013年8月28日

Android─ Action bar Navigate Up


                                                               The Up button in the action bar.

原本的up button按了會返回上一個parent activity

back與home的差別

也可以更改為返回上一頁
範例 code如下

@Override
public boolean onOptionsItemSelected(MenuItem item) { 
        switch (item.getItemId()) {
        case android.R.id.home: //為up butoon的id 
            onBackPressed();
            return true;
        }
    return super.onOptionsItemSelected(item);
}

Java─getclass() 用途

Use Object.getClass(). It returns the runtime type of the object.

Object instance = new SomeClass();
instance.getClass().getName(); //will return the name (as String) (== "SomeClass")
instance.getClass(); //will return the SomeClass' Class object
參考連結

Android─取得系統時間 和比較時間

Calendar c = Calendar.getInstance(); 
取得系統日期:year = c.get(Calendar.YEAR);
              month = c.grt(Calendar.MONTH);
              day = c.get(Calendar.DAY_OF_MONTH);
取得系統時間:hour = c.get(Calendar.HOUR_OF_DAY); 
              minute = c.get(Calendar.MINUTE);

如何比較時間
Calendar c = Calendar.getInstance();
    c.set(Calendar.MONTH, Calendar.MARCH);
    c.set(Calendar.DAY_OF_MONTH, 3);
 long time2=   c.getTimeInMillis();
 c.set(Calendar.MONTH, Calendar.AUGUST);
 c.set(Calendar.DAY_OF_MONTH, 8);   
 long time3=   c.getTimeInMillis();
 if(time>time2){
     //Logic
     if(time>time3){
         //Logic
     }
 }

2013年8月14日

Java ─ Arraylist基本操作

宣告一個新的arraylist
 ArrayList arlist=new ArrayList(); 

 加入字串進入arraylist 
 arlist.add("First Element"); 
 arlist.add("Second Element"); 

 取得arraylist內容 
 arlist.get(0); 
 arlist.get(1);

 清空arraylist所有資料 arlist.clear();

參考來源

Android ─ 如何將string.xml資料變成字串

<string name="content">我是顯示內容</string>

透過layout 設定
<TextView<
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/content">
</TextView>

java程式碼 設定
text.setText(getString(R.string.content));

設定為字串變數

在activity下
this.getString(R.string.content);

在非activity下
context.getString(R.string.content);
application.getString(R.string.content);