2015年6月16日 星期二

[Android] TextView 自述定義修改 Layout

文字超過換行
或者是
...取代

                android:singleLine="true"
                android:maxEms="8"


參考網址:
http://bbs.51cto.com/thread-873816-1.html

2015年6月15日 星期一

[Android] ViewPager Point 小圓點切換

View pager 小圓點的切換
很好的範例

參考網址: http://blog.csdn.net/lyhdream/article/details/8707293

2015年5月28日 星期四

[Android] layout 圖片點擊切換 xml 設計

圖片點擊切換,由xml 的方式設計

layout
1
2
3
4
5
        <ImageView
            android:id="@+id/imgListChartSwitch"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/btn_measure" />


btn_measure.xml
1
2
3
4
5
6
7
xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/btn_chart" android:state_pressed="true" android:state_selected="true"/> <item android:drawable="@drawable/btn_chart" android:state_selected="true"/> <item android:drawable="@drawable/btn_list" android:state_pressed="true"/> <item android:drawable="@drawable/btn_list"/> </selector>

2015年5月20日 星期三

[Android] 隱藏百度地圖3.x SDK 縮放鍵

一個function就搞定


1
2
3
4
5
6
7
8
9
10
11
12
13
  //隐藏 缩放控件和  百度logo  
    private void hideZoomCtler()  
    {  
        if(mMapView==null)  
            return ;  
        int count = mMapView.getChildCount();  
        for (int i = 0; i < count; i++) {  
            View child = mMapView.getChildAt(i);  
            // 隐藏百度logo ZoomControl  
            if (child instanceof ImageView || child instanceof ZoomControls)  
                child.setVisibility(View.INVISIBLE);  
        }  
    }





參考文章
http://blog.csdn.net/weizongwei5/article/details/39178243

2015年5月5日 星期二

[Android] 自動開啟GPS - 需ROOT




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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
 /**

  * 强制帮用户打开GPS

  *

  * @param context

  */

    public static final void openGPS(Context context) { 

        Intent GPSIntent = new Intent(); 

        GPSIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 

        GPSIntent.addCategory("android.intent.category.ALTERNATIVE"); 

        GPSIntent.setData(Uri.parse("custom:3")); 

        try { 

            PendingIntent.getBroadcast(context, 0, GPSIntent, 0).send(); 

        } catch (CanceledException e) { 

            e.printStackTrace(); 

        } 

    } 



 /**

  * 判断GPS是否开启,GPS或者AGPS开启一个就认为是开启的

  *

  * @param context

  * @return true 表示开启

  */

 public static final boolean isOPen(final Context context) { 

        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 

        // 通过GPS卫星定位,定位级别可以精确到街(通过24颗卫星定位,在室外和空旷的地方定位准确、速度快) 

        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 

        // 通过WLAN或移动网络(3G/2G)确定的位置(也称作AGPS,辅助GPS定位。主要用于在室内或遮盖物(建筑群或茂密的深林等)密集的地方定位) 

        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

        if (gps || network) { 

            return true; 

        } 



        return false; 

    }
         
參考網站:
http://blog.csdn.net/android_ls/article/details/8605931

2015年5月4日 星期一

[Android] 聲音 SoundPool

1
2
3
4
5
6
//BB叫
int alertId;
SoundPool soundPool;
soundPool = new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);
alertId = soundPool.load(this, R.raw.bb, 1);
soundPool.play(alertId, 1.0f, 1.0f, 0, 0, 1.0f);
聲音BB聲下載:
https://drive.google.com/open?id=0B2WSxYpFVHkaS0t6LTRGN1JzZm8&authuser=0

參考網址:
http://blog.csdn.net/pku_android/article/details/7625868
http://style77125tech.pixnet.net/blog/post/152442867-%5Bandroid%5D-soundpool-%E9%9F%B3%E6%95%88-(%E4%B8%80)

[Android] 震動功能

1
2
3
4
5
6
setVibrate(1000); // 震動 1 秒

public void setVibrate(long time){
    Vibrator myVibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
    myVibrator.vibrate(time);
}

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
// 短震動20次
countdowntimer = new CountDownTimer(10500, 500) {

 @Override
 public void onTick(long millisUntilFinished) {
                // TODO Auto-generated method stub
  setVibrate(100);
 }
 @Override
 public void onFinish() {
                // TODO Auto-generated method stub
 }
}.start();

權限
android:name="android.permission.VIBRATE" />


參考網頁:
http://lolikitty.pixnet.net/blog/post/173532249-android-%E5%85%A5%E9%96%80%E6%95%99%E5%AD%B8%EF%BC%9A%E9%9C%87%E5%8B%95

[Android] 判斷否有SD卡

1
2
3
4
5
6
7
8
9
10
11
12
13
// 判斷否有SD卡
if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){
 output = new FileOutputStream("/sdcard/output.txt");
}else{
 output = new FileOutputStream("/data/data/output.txt");
}
String bt = "輸入訊息";
try {
 // 轉byte
 output.write(bt.getBytes());
} catch (IOException e) {
 e.printStackTrace();
}



1
2
3
4
  
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>  
  
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>



參考網站:
http://blog.csdn.net/xinzheng_wang/article/details/7827775

2015年4月29日 星期三

[Android] 倒數計時功能

第一個參數是總共計時幾秒,以0.001為單位,因此要讓它倒數5秒就要乘上5*1000,
第二個參數是每隔幾秒跳一次,一樣以0.001為單位,因此每隔一秒跳一次就是傳入1000。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
new CountDownTimer(5000, 1000) {

 @Override

 public void onTick(long millisUntilFinished) {
                //倒數秒數中要做的事
  textView.setText("倒數時間:" + new SimpleDateFormat("s").format(millisUntilFinished));
 }

 @Override
 public void onFinish() {
                 //倒數完成後要做的事
 }
}.start();
參考網站:
http://givemepass.blogspot.tw/2011/12/blog-post_08.html

2015年4月10日 星期五

[Android] SQLite

  一般在撰寫SQL的時候會把 Main 與 SQLite 分開
會先把SQLite給編寫好  好讓  撰寫Main的時候可以直接呼叫使用
需要繼承extends SQLiteOpenHelper

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class QRcodeDB extends SQLiteOpenHelper {

//資料庫版本關係到App更新時,資料庫是否要調用onUpgrade()
private static final String DB_NAME = "qrcode_db";
public static final String CONTACTS_COLUMN_IMEI = "IMEI";

public QRcodeDB(Context context) {
               //修改後
              //super(context, name, factory, version);
super(context, DB_NAME, null, 1);

// TODO Auto-generated constructor stub
}

 
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try{
db.execSQL(
"create table contacts"
+ " (ITEM Integer PRIMARY KEY AUTOINCREMENT,"
+ " IMEI TEXT,"
+ " WIFIMAC TEXT,"
+ " RESULT TEXT,"
+ " PRODUCT TEXT,"
+ " APPVERSION TEXT)");
}catch(Exception e){
db.execSQL("DROP TABLE IF EXISTS contacts");
}
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
                //可有可無一般在更新的時候才會驅動,所以若是把他寫在onCreate的catch會更好
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}

public boolean insertContact(String IMEI, String WIFIMAC, String RESULT, String PRODUCT, String APPVERSION){
//新增資料
SQLiteDatabase db = this.getWritableDatabase();
                //通常都要搭配 ContentValues作使用
ContentValues contentValues = new ContentValues();
contentValues.put("IMEI", IMEI);
contentValues.put("WIFIMAC", WIFIMAC);
contentValues.put("RESULT", RESULT);
contentValues.put("PRODUCT", PRODUCT);
contentValues.put("APPVERSION", APPVERSION);

db.insert("contacts", null, contentValues);
db.close();
return true;
}

public Cursor getData(String IMEI) {
SQLiteDatabase db = this.getReadableDatabase();
                                          //(搜尋語法,條件,搜尋參數)
                                          //("contacts", "IMEI = ? ", new String[] { Integer.toString(IMEI) });
                                          //兩個都可以
Cursor res = db.rawQuery("select * from contacts where IMEI=" + IMEI + "", null);
return res;
}

public int numberOfRows() {
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, CONTACTS_TABLE_NAME);
db.close();
return numRows;
}

public boolean updateContact(String IMEI, String WIFIMAC, String RESULT, String PRODUCT, String APPVERSION){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("IMEI", IMEI);
contentValues.put("WIFIMAC", WIFIMAC);
contentValues.put("RESULT", RESULT);
contentValues.put("PRODUCT", PRODUCT);
contentValues.put("APPVERSION", APPVERSION);
db.update("contacts", contentValues, "IMEI = ? ", new String[] { IMEI });
return true;
}

          //刪除
public Integer deleteContact (String IMEI) {
     SQLiteDatabase db = this.getWritableDatabase();
     Integer dbDelete = db.delete("contacts", "IMEI = '" + IMEI + "'" , null);
     return dbDelete;
}

public ArrayList getAllCotacts() {
ArrayList array_list = new ArrayList();
// hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from contacts", null);
res.moveToFirst();
while (res.isAfterLast() == false) {
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_IMEI)));
res.moveToNext();
}
return array_list;
}

}



參考網站:http://www.tutorialspoint.com/android/android_sqlite_database.htm

2015年3月26日 星期四

[Android] 強制更新Ui畫面

Activity.runOnUiThread




1
2
3
4
5
6
7
Activity().runOnUiThread(new Runnable() {
    public void run()
    {
        //要讓 UI Therad 執行的動作
        txt.setText("我改變囉!");
    }
});





參考資料:
http://www.allenj.net/archives/2711