顯示具有 Android 標籤的文章。 顯示所有文章
顯示具有 Android 標籤的文章。 顯示所有文章

2013年12月6日

[Android]使用BlueStacks、Eclipse進行開發除錯




前言


使用Android模擬器進行開發與除錯時,Windows作業系統上的執行時間會比較長。
對於某些專案在分秒必爭的情況下,模擬器反而造成許多開發上的不便。

解決方案

使用BlueStacks與Eclipse進行開發與除錯。


關於


  1. BlueStacks:一套可以模擬一般Android手機或平板APP正常執行的軟體,相信許多人都使用這個軟體來玩一些APP遊戲,如神魔之塔...等。
  2. Eclipse:一套用java開發的好用程式編輯軟體。(作者就是使用此軟體來開發Android的)

 如何使用

  1. 先進行BlueStacks 及 Eclipse安裝,並且都順利安中與執行。
  2. 打開Eclipse的Device(查詢目前有哪些裝置在線上),如圖1。開啟位置:Windows>Show View>Others>Device。
    圖1.Eclpse的Device列表
如果沒有找到裝置,請重新啟動adb,如圖2。
圖2.Restart adb

2013年1月3日

[Android]平版安裝不了驅動


問題:


開發Android時,使用模擬器是一件使人磨耐性的事。而且模擬器的功能始終有限,平版市場充斥。許多USB-Driver一直安裝不上去。

解決:

(適用 Win7 & WinXP)

修改Google USB Driver 的 [android_winusb.inf]
讓您的平版也可以安裝上去。

Step 1:
打開android_winusb.inf 檔案。

筆者的路徑為:C:\Android\android-sdk\extras\google\usb_driver\

Step 2:
打開裝置管理員,未知裝置->(右鍵)內容->詳細資料->選擇硬體辨識碼

Step 3:
確定電腦為x64 or x86 ->尋找


[Google.NTx86]

[Google.NTamd64]

按照上面的方式,將資訊填入


;Android
%SingleAdbInterface%        = USB_Install, USB\VID_18D1&PID_0003&REV_0230&MI_01
%SingleAdbInterface%        = USB_Install, USB\VID_18D1&PID_0003&MI_01

Step 4:
重新更新驅動,會發現安裝成功。

Step 5:
清除adb並重新啟動
清除adb:開始->執行->CMD->輸入 cd C:\Android\android-sdk\platform-tools\adb kill-server
重新啟動:開始->執行->CMD->輸入 cd C:\Android\android-sdk\platform-tools\adb start

Done.

2013年1月1日

[Android]製作縮圖

問題:
在使用Android的ImageView元件放入圖檔時,發現圖檔尺寸比ImageView元件的尺寸來的大很多。

解決:
使用Android製作縮圖,



/**
 原始圖檔 bitmap
*/

//取得圖檔寬度
int bmpWidth  = bitmap.getWidth(); 

//取得圖檔高度
int bmpHeight  = bitmap.getHeight(); 

//設定縮圖寬度
float scaleWidth  = (float) sWidth / bmpWidth;     
//按固定大小缩放sWidth,要多大有多大

//設定縮圖高度
float scaleHeight = (float) sHeight / bmpHeight;  

//轉換矩陣
Matrix matrix = new Matrix(); 
matrix.postScale(scaleWidth, scaleHeight); 

//產生縮圖
Bitmap resizeBitmap = 
Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight,matrix, false); 

//記得做資源回收,不然會發生溢位
bitmap.recycle(); 


參考網站:Android中图片缩放方法

2012年9月20日

[Android]Android連不到WebAPI

問題:在開發Android時,連不上Web的API,該怎麼辦?

/*
step 1:先檢查是否有加入權限
*/
//AndroidManifest.xml


//*.java
@Override
    public void onCreate(Bundle savedInstanceState) {
  //加入以下兩行
     StrictMode
.setVmPolicy(new StrictMode
.VmPolicy
.Builder()
.detectAll()
.penaltyLog()
.penaltyDeath()
.build());
     StrictMode.setThreadPolicy(
new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites()
.detectNetwork().penaltyLog().build());
  
  /*
  StrictMode
  This class is not available with API level 8.
  所以要用在2.3以上喔
  */
     
  super.onCreate(savedInstanceState);


參考網站:Android™ 2.1 android.R.drawable Icon Resources

[Android]Android內建的圖示

問題:在開發Android時,沒有美工的頭腦時,網路上下載圖檔又擔心版權問題,所以直接使用Android 內建的圖示

//*.java
ImageView btnImg_Help = (ImageView)findViewById(R.id.Img_Help); 
btnImg_Help.setImageResource(android.R.drawable.ic_menu_help);

//*.xml
android:icon="@android:drawable/ic_menu_save"

參考網站:Android™ 2.1 android.R.drawable Icon Resources

2011年12月9日

[Android] 開發小問題

1. indexOf判斷中文字串
//indexOf的句子,不知道為什麼第一個字不會去找,怪災。
replaced=" "+result.get(i);

//年
if(replaced.indexOf("年")>0)
{
score[i]=score[i]+1;
}

2.使用正規表示法判斷數字
//判斷數字
     Pattern p=Pattern.compile("[0-9]{1,2}");
     Matcher m=p.matcher(replaced);
     if(m.find()){
      //正確
     }

//{n,m} 表示前一個字元或者前一個RE出現n到m次
//[0-9] 0-9的集合

2011年12月5日

[Android]Activity返回上一頁

page1

package Demo;
 
/* import相關class */
import java.text.DecimalFormat;
import java.text.NumberFormat;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
public class Page1 extends Activity 
{
  Intent intent;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    /* 載入mylayout.xml Layout */
    setContentView(R.layout.myalyout);
     
     
    Button b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(new Button.OnClickListener()
    {
      public void onClick(View v)
      {
       /* 回傳result回上一個activity */
       Page1.this.setResult(RESULT_OK, intent);
        
       /* 關閉activity */
       Page1.this.finish();
      }
    });
  }
}

page2
package Demo;

/* import相關class */
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class Page2 extends Activity 
{
    
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    /* 載入main.xml Layout */
    setContentView(R.layout.main);
   
        /*new一個Intent物件,並指定class*/
     Intent intent = new Intent();
        intent.setClass(Page2.this,Page1.class);
     
     /*呼叫Activity EX03_11_1*/
     startActivityForResult(intent,0);
      }
  
  /* 覆寫 onActivityResult()*/
  @Override
  protected void onActivityResult(int requestCode, int resultCode,
                                  Intent data)
  {
    switch (resultCode)
    { 
      case RESULT_OK:
//回上一頁囉
        break;       
      default: 
        break; 
     } 
   } 
}

2011年10月2日

[Android]直向、橫向的設定


問題:
Android 螢幕的直向?橫向?




一、程式碼

Activity.java

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //強置為橫向
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); //強置為直向
        

[Android] 動畫的實現





一、程式碼

Activity.java

package tw.G000E004;

import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class G000E004Activity extends Activity {
    Animation myanimation;
    ImageView img;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        myanimation=AnimationUtils.loadAnimation(this, R.anim.myanim);
        img=(ImageView)findViewById(R.id.img);
        
        img.startAnimation(myanimation);
    }
}


myanim.xml(記得加入)


android:fillafter="false"
android:fromxscale="0.0"
android:fromyscale="0.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotx="50%"
android:pivoty="50%"
android:toxscale="1.4"
android:toyscale="1.4">
android:fromxdelta="30"
android:fromydelta="30"
android:toxdelta="0"
android:toydelta="50">
android:fromdegrees="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotx="50%"
android:pivoty="50%"
android:todegrees="+350">

main.xml(記得加入)

android:layout_width="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
android:layout_width="fill_parent"
android:text="@string/hello">
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/icon">


2011年8月28日

[Android]OnItemClick的使用方法

一、簡介
有人發問說「OnItemClick的使用方法,如何換頁」。
其實方式不難,我們需要了解的地方在於
(1)Activity 和 Activity 之間需要怎麼換頁和傳遞資料。
(2)OnItemClick的使用方式。

二、方法
(1)  Intent:Intent 有兩種基本的用法,似乎取決於使用者需要的是哪一種。

a.顯示的Intent
在建構Intent時已經指定接收者,類似函式調用的方式。


b.隱藏式的Intent
在建構Intent時並沒有指定接收者,之後在傳送。



(2)OnItemClick:一個監聽事件。當項目被點選時,就會啟動此函式。


三、程式碼

Activity.java

package tw.test2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.Toast;

public class Test2Activity extends Activity {
	private Gallery gallery;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        gallery=(Gallery)findViewById(R.id.gallery);
        
        //希宣告一個類別,用來設定圖片資訊
        ImageAdapter imageAdapter=new ImageAdapter(this);
        
        //加入圖片
        Integer[] mImageIds={R.drawable.icon,R.drawable.ha};
        
        //設定圖片位置
        imageAdapter.setmImageIds(mImageIds);
        
        //圖片高度
        imageAdapter.setHeight(100);
        //圖片寬度
        imageAdapter.setWidth(200);
        
        gallery.setAdapter(imageAdapter);
        
        //當按下圖片時,所觸發的事件
        gallery.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView parent, View view, int position,
					long id) {
				// TODO Auto-generated method stub
				Toast.makeText(Test2Activity.this, "您選的是第"+position+"張圖", Toast.LENGTH_LONG).show();
				
				//換頁
				Intent intent = new Intent();
				intent.setClass(Test2Activity.this, Report.class);
				/*
				 * 這邊是你要帶過去的資料
				 */
				intent.putExtra("test", "第二頁囉");
				startActivity(intent);
			}});
    }
}
ImageAdapter.java
package tw.test2;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private Integer width;
    private Integer height;
    private Integer[] mImageIds;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        //設定圖片來源
        imageView.setImageResource(mImageIds[position]);
        //設定圖片的寬、高
        imageView.setLayoutParams(new Gallery.LayoutParams(width, height));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        return imageView;
    }

    public Integer getHeight() {
        return height;
    }

    public void setHeight(Integer height) {
        this.height = height;
    }

    public Integer[] getmImageIds() {
        return mImageIds;
    }

    public void setmImageIds(Integer[] mImageIds) {
        this.mImageIds = mImageIds;
    }

    public Integer getWidth() {
        return width;
    }

    public void setWidth(Integer width) {
        this.width = width;
    }

    public int getCount() {
        return mImageIds.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }
}
Report.java
package tw.test2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class Report extends Activity {
      /** Called when the activity is first created. */
 @Override
       public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.report);
               
               Intent intent = this.getIntent();
               String showtest=intent.getStringExtra("test");
               
               Toast.makeText(Report.this,showtest.toString(), Toast.LENGTH_LONG).show();          
               
 }
}
記得加入(AndroidManifest.xml)

2011年8月26日

[Android]以背景服務的方式錄製聲音

一、簡介
現在大部份使用者所看見的執行畫面都是前端的程式(Activity),但是還有很多的服務需要在背景中執行,這個功能則需要使用到Service。
由於背景執行所以是要寫在Service而不是Activity,因此需要在權限頁面(AndroidManifest)中新增一個Service的服務。

二、方法
(1)  Service:這個服務本身是無介面,也並非一個獨立的Process,只是有需要長時間執行的獨立程序為了不影響主程式的運行,就會使用Service。



(2)   MediaRecorder:這個類別主要是 Android錄音錄影所使用到的,詳細資料可以參詳官方網站。

三、程式碼

Activity.java

package tw.Recording;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class RecordingActivity extends Activity {
	
	private Button start,stop;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        start=(Button)findViewById(R.id.start); 
        stop=(Button)findViewById(R.id.stop);
        
        start.setOnClickListener(new OnClickListener(){
        	//執行背景作業
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(RecordingActivity.this, RecordingService.class);
				           startService(intent);
			}});
        stop.setOnClickListener(new OnClickListener(){
        	
        	//停止背景作業
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(RecordingActivity.this, RecordingService.class);
	            stopService(intent);
			}});
    }
}

Service.java
package tw.Recording;

import java.io.File;
import java.io.IOException;

import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.IBinder;

public class RecordingService extends Service {
	private MediaRecorder mediaRecorder = null;

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	//錄音
	public void onStart(Intent intent,int startId)
	{
		//設定錄音檔名	
		String fileName = "test.amr";
		try{
			File SDCardpath=Environment.getExternalStorageDirectory();
			File myDataPath=new File(SDCardpath.getAbsolutePath()+"/download");
			
			if( !myDataPath.exists() ) myDataPath.mkdirs();
			
			File recodeFile = new File(SDCardpath.getAbsolutePath() + "/download/"+fileName);
			mediaRecorder = new MediaRecorder();
			
			//設定音源
			mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
			//設定輸出檔案的格式
			mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
			//設定編碼格式
			mediaRecorder
			.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
			//設定錄音檔位置
			mediaRecorder.setOutputFile(recodeFile.getAbsolutePath());
			mediaRecorder.prepare();
			//開始錄音
			mediaRecorder.start();
		}
		catch(IOException e){
		e.printStackTrace();
		}
		super.onStart(intent, startId);
	}

	@Override
	//停止背景時
	    public void onDestroy() {
		if(mediaRecorder != null) {
			    mediaRecorder.stop();
			    mediaRecorder.release();
			    mediaRecorder = null;
			    }
	        super.onDestroy();
	    }
		

}

AndroidManifest.xml(記得加入)