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

2014年6月25日 星期三

Android Button UI 按鈕 美化

改變基本灰色樣式按鈕

1.首先要在drawable-hdpi建立一個xml檔
 選擇Android XML File->Next
選擇Drawable->命名->選擇selector
 btn_black.xml加入程式碼
這裡舉一個黑色的按鈕,其他顏色請參考來源網站

    
        
         
            
            
            
        
    
    
        
            
            
            
            
        
    

2.在你的strings.xml裡加入

3.activity_main.xml
"@drawable/btn_black"->btn_black.xml名稱
"@style/ButtonText"->strings.xml裡style name="ButtonText"名稱
參考來源: http://androidtutorials4beginners.blogspot.tw/2013/04/android-colored-gradient-buttons-using.html

2014年6月24日 星期二

Android google map addMarker setOnMarkerClickListener 標記後動作

使用google map標記功能addMarker後,需要增加動作可以使用OnMarkerClickListener和OnMarkerDragListene
例如:點取標記後,跳去其他頁面。
前置作業:有使用google map,有使用addMarker
1.MainActivity.java
public class MainActivity extends FragmentActivity implements OnMarkerClickListener{ //需引用OnMarkerClickListener
 private GoogleMap map;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 map = ((SupportMapFragment) getSupportFragmentManager()
    .findFragmentById(R.id.map)).getMap();
  
  LatLng p1 = new LatLng(22.6297370, 120.3278820);
  if (map != null) {
   map.setOnMarkerClickListener(this); //點取Marker動作
   // google mark
   map.addMarker(new MarkerOptions()
     .position(p1)
     .title("多那之高雄中正門市")
     .snippet("咖啡.烘培"));
  }

 }

 /* 點選marker顯示 */
 @Override
   public boolean onMarkerClick(Marker marker) {
  /* 此處增加點取後想要的動作*/
  Toast.makeText(getApplicationContext(),"Marker Clicked: " + marker.getTitle(), Toast.LENGTH_LONG).show(); //顯示點取addMarker的標題
     return false;
   }

}

2014年6月19日 星期四

Android QRcode

掃描QRcode 讓資訊顯示出來
需要安裝QRDroid
1.activity_main.xml

            
2.MainActivity.java
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  info = (TextView) findViewById(R.id.info);
  scanner = (Button) findViewById(R.id.scanner);
  scanner.setOnClickListener(new Button.OnClickListener() {
   @Override
   public void onClick(View v) {

    // TODO Auto-generated method stub
    Intent iScaner = new Intent("la.droid.qr.scan"); // 使用QRDroid的掃描功能
    iScaner.putExtra("la.droid.qr.complete", true); // 完整回傳,不截掉scheme
    try {
     // 開啟 QRDroid App 的掃描功能,等待 call back onActivityResult()
     startActivityForResult(iScaner, 0);
    } catch (ActivityNotFoundException ex) {
     // 若沒安裝 QRDroid,則開啟 Google Play商店,並顯示 QRDroid App
     Intent intent = new Intent(Intent.ACTION_VIEW, Uri
       .parse("market://details?id=la.droid.qr"));
     startActivity(intent);
    }
   }
  });
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) { //不需要建立onActivityResult
  super.onActivityResult(requestCode, resultCode, data);

  if (0 == requestCode && null != data && data.getExtras() != null) {
   // 掃描結果存放在 key 為 la.droid.qr.result 的值中
   String result = data.getExtras().getString("la.droid.qr.result");

   info.setText(result); // 將結果顯示在 TextVeiw 中
  }
 }
3.建立QRcode的網站 http://www.quickmark.com.tw/Cht/qrcode-datamatrix-generator/default.asp?qrText
4.掃描圖片




參考來源:
http://elviselle.blogspot.tw/2013/06/android-app-qr-code.html

2014年6月16日 星期一

Android shortcut 桌面 捷徑

自動建立桌面捷徑的方法,使用shortcut
首先要宣告
1.AndroidManifest.xml

2.MainActivity.java
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  addShortcut();
 }
/* 建立桌面捷徑 */
 private void addShortcut() {
  Intent shortcutIntent = new Intent(getApplicationContext(),
    MainActivity.class); // 啟動捷徑入口,一般用MainActivity,有使用其他入口則填入相對名稱,ex:有使用SplashScreen
  shortcutIntent.setAction(Intent.ACTION_MAIN);
  Intent addIntent = new Intent();
  addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); // shortcutIntent送入
  addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
    getString(R.string.app_name)); // 捷徑app名稱
  addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
    Intent.ShortcutIconResource.fromContext(
      getApplicationContext(),// 捷徑app圖
      R.drawable.ic_launcher));
  addIntent.putExtra("duplicate", false); // 只創建一次
  addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); // 安裝
  getApplicationContext().sendBroadcast(addIntent); // 送出廣播
 }
參考來源:
http://stackoverflow.com/questions/22829647/programmatically-add-the-widget-to-home-screen-in-android
http://stackoverflow.com/questions/21542409/home-screen-app-shortcut-is-not-working-android-app-isnt-installed