2015年8月24日 星期一

SharedPreferences 用法 判斷是否第一次使用本APP (類似'導覽教學頁面)

public class GtSharedPreferences {

public static final String NAME = "GtdSharedPrefernces";

//首頁-是否第一次使用public static final String KEY_FIRST_USED = "isFirstUsed";
public static void saveIsFirstUsed(Context context) {
   SharedPreferences sp = context.getSharedPreferences(NAME, Activity.MODE_PRIVATE);
   sp.edit().putBoolean(KEY_FIRST_USED, false).commit();
}

public static boolean getIsFirstUsed(Context context) {
   SharedPreferences sp = context.getSharedPreferences(NAME, Activity.MODE_PRIVATE);
   return sp.getBoolean(KEY_FIRST_USED, true);
}

}

MainActiviy


boolean isFirstUse = GtSharedPreferences.getIsFirstUsed(this);
if (isFirstUse) {
    new AlertDialog.Builder(this)
            .setTitle("123")
            .setMessage("456")
            .setPositiveButton("789", new DialogInterface.OnClickListener() {
                @Override                public void onClick(DialogInterface dialog, int which) {
                    GtSharedPreferences.saveIsFirstUsed(IndexActivity.this);
                    dialog.dismiss();
                }
            }).show();
}

2015年8月19日 星期三

取得目前螢幕上的經緯度 LatLngBounds bounds 使用方法

private  LatLngBounds bounds;
先宣告成全域變數


mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() 
{
    @Override  public void onCameraChange(CameraPosition cameraPosition)    {
      bounds = mMap.getProjection().getVisibleRegion().latLn      gBounds;
        Log.e("bounds",bounds+"");

    }
});

這個方法指的是 我地圖攝影機移動的時候,我跟著計算螢幕上的經緯度,
bounds 這個值 其實包含了 兩個經緯度 等於是四個值

bounds.northeast 東北角地方
bounds.northeast.latitude   = lat_up
bounds.northeast.longitude  = lon_right



bounds.southwest   西南角地方
bounds.southwest.latitude   =  lat_down
bounds.southwest.longitude  = lon_left




參考網址:https://developers.google.com/maps/documentation/business/mobile/android/reference/com/google/android/m4b/maps/model/LatLngBounds.html#southwest

2015年8月2日 星期日

如何 判斷從json 撈回來的日期時間資料 跟本機時間是前天還是今天 Android

先判斷本機日期

  private  Date myDate;

 myDate = new Date();

這樣就取得本機今天的日期了
為何要設定全域變數 因為這樣 在撈資料的時候就不用再設定一次

  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

先設定 日期格式   因為設定的跟取的 不一定一樣格式


String strDate = sdf.format(myDate);

設定本機 格式 是我們想要的格式
                    Date d1 = null;
                    Date d2 = null;

                    try {
                        d1  =  sdf.parse(strDate);
                        d2 = sdf.parse(happendate);

                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

之後就可以判斷兩者之間的差距


 if(d1.equals(d2)){   這邊指的是兩者一樣

                        }

                       else if(d1.before(d2)){  d1 在 d2之前

}

                       else{                        
                           Log.d("Jack","after");    d1 在 d2之後
                         continue;

                      }