2014年7月18日 星期五

Android share 分享功能

分享至facebook,line之類的
1.activity_main.xml
建立一個分享按鈕

2.MainActivity.java
 private Button shareButton;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  shareButton = (Button) findViewById(R.id.shareButton);
  shareButton.setOnClickListener(new Button.OnClickListener() {
   public void onClick(View v) {
    shareDialog();
   }
  });
 }

 // 分享app
 void shareDialog() {
  
  String shareText = "與你分享的快樂勝過獨自擁有"; //
  //Uri imageUri = Uri.parse("android.resource://" + getPackageName() + "/drawable/" + "ic_launcher"); //分享圖片至gmail、twitter可,line、facebook不行
  //Log.i("imageUri:", imageUri + "");
   Intent shareIntent = new Intent();
   shareIntent.setAction(Intent.ACTION_SEND);
   shareIntent.setType("text/plain"); //文字檔類型
   shareIntent.putExtra(Intent.EXTRA_TEXT, shareText); //傳送文字
   shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
   //shareIntent.setType("image/png"); //圖片png檔類型
  // shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri); //傳送圖片
   startActivity(Intent.createChooser(shareIntent, "分享"));
 }
參考來源:
http://hscc.cs.nctu.edu.tw/~lincyu/Android/ShareText.pdf
http://stackoverflow.com/questions/20333186/how-to-share-imagetext-using-action-send-in-android

2014年7月9日 星期三

Android google map 結合 spinner 下拉式選單

利用下拉式選單選定某個項目後,直接在google map上移到定點
1.activity_main.xml 在地圖下方加入下拉式選單




2.MainActivity.java 以基隆市、台北市85度c為例
private float coordinate[][][] = {
   { { 0, (float) 25.1288160, (float) 121.7404860 },
     { 0, (float) 25.1286310, (float) 121.7594120 },
     { 0, (float) 25.0972380, (float) 121.7122780 } },
   { { 1, (float) 25.0574640, (float) 121.5554840 },
     { 1, (float) 24.9836380, (float) 121.5673320 },
     { 1, (float) 25.0310780, (float) 121.5522090 } },
    }; //設定定點座標
private String[] type = new String[] { "基隆市", "台北市" }; //第一個選單
 private String[] locationName = new String[] { "85度c基隆孝一店", "85度c基隆東信店","85度c七堵明德店" }; //初始選單
 private String[][] type2 = new String[][] {
   { "85度c基隆孝一店", "85度c基隆東信店", "85度c七堵明德店" },
   { "85度c台北光復北店", "85度c木柵木新店", "85度c台北安和店" } };//第二個選單
 private Spinner sp1;// 第一個下拉選單
 private Spinner sp2;// 第二個下拉選單
 private Context context;
 ArrayAdapter adapter;
 ArrayAdapter adapter2;
@Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  context = this;
  // 程式剛啟動時載入第一個下拉選單
  adapter = new ArrayAdapter(this,
    android.R.layout.simple_spinner_item, type);
  adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  sp1 = (Spinner) findViewById(R.id.type);
  sp1.setAdapter(adapter); //將adapter加入sp1選單
  sp1.setOnItemSelectedListener(selectListener); //第一個選單監聽動作

  // 程式剛啟動時載入第一個下拉選單,所以第二選單要載入第一個選單的子項目
  adapter2 = new ArrayAdapter(this,
    android.R.layout.simple_spinner_item, locationName);
  adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  sp2 = (Spinner) findViewById(R.id.type2);
  sp2.setAdapter(adapter2); //將adapter加入sp2選單
  sp2.setOnItemSelectedListener(selectListener2); //第二個選單監聽動作
 }

 // 第一個下拉選單監聽動作
 private OnItemSelectedListener selectListener = new OnItemSelectedListener() {
  public void onItemSelected(AdapterView parent, View v, int position,
    long id) {
   // 讀取第一個下拉選單是選擇第幾個
   int pos = sp1.getSelectedItemPosition();
   // 重新產生新的Adapter,用的是二維陣列type2[pos]
   adapter2 = new ArrayAdapter(context,
     android.R.layout.simple_spinner_item, type2[pos]);
   // 載入第二個下拉選單Spinner
   sp2.setAdapter(adapter2);

  }

  public void onNothingSelected(AdapterView arg0) {

  }

 };

 // 第二個下拉選單監聽動作
 private OnItemSelectedListener selectListener2 = new OnItemSelectedListener() {
  public void onItemSelected(AdapterView parent, View v, int position,
    long id) {
   setMapLocation(); //移動到選定項目位置的動作
  }

  public void onNothingSelected(AdapterView arg0) {

  }

 };

 public void setMapLocation() {
  try {
   int iSelect1 = sp1.getSelectedItemPosition(); //第一個下拉選單被選到的第幾個項目
   int iSelect2 = sp2.getSelectedItemPosition(); //第二個下拉選單被選到的第幾個項目
   double dLat = coordinate[iSelect1][iSelect2][1]; // 抓取座標,南北緯
   double dLon = coordinate[iSelect1][iSelect2][2]; // 抓取座標,東西經
   LatLng gg = new LatLng(dLat, dLon); //將座標位置輸入設定
   map.moveCamera(CameraUpdateFactory.newLatLng(gg)); //移動到定點
   map.animateCamera(CameraUpdateFactory.zoomTo(15)); //縮放
  } catch (NullPointerException e) {
   Log.i("myLocation", "NullPointException");
  }
 }
參考來源: http://jim690701.blogspot.tw/2012/07/androidspinner_15.html

2014年7月4日 星期五

Android ClassCastException 幽靈錯誤

小技巧
當LogCat出現
Caused by: java.lang.ClassCastException: xxxx cannot be cast to xxxx
這種錯誤時,可以先執行eclips上的Project->clean...
就可以移除這些幽靈的錯誤
參考來源: http://stackoverflow.com/questions/12465551/android-widget-textview-cannot-be-cast-to-android-widget-button