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

2013年2月21日 星期四

Add radioboxes and checkboxes

final CharSequence[] items = {"Red", "Green", "Blue"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
        Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
    }
});
AlertDialog alert = builder.create();

2013年2月20日 星期三

Playing android games on PC?

Youwave for Android works great! My son wanted to play Robodefense on the PC so I found this little piece of software that's simple and uses .apk files I had on my G-Tablet.

http://www.youwave.com/

Thanks, how do you get the android files from the market to the pc to begin with? Playing robo defense on the phone will wipe out the battery quick

try 4shared

This hosts warez items which is not allowed here (neither the discussion).

R.menu cannot be resolved


You need to import the R.java class.
import [package_name].R;

like
import com.HennighanDev.IncomeTax.R

Unable to resolve target 'android-5'

問題描述:


當程式import進來後,執行前下方出現Unable to resolve target'android-X'

原因:
安裝2.1或2.2 version ,API level:7裡面沒有"API level5的AVD"可對應


解決方式:
開啟default.properties文件,將target=android-5改成target=android-7即可執行了
(高版本可以向下相容)

2012年9月15日 星期六

Error generating final archive: Debug certificate expired on xxxxxx(date)

When you build a project in Eclipse, it prompts you an error message.
Error generating final archive: Debug certificate expired on xxxxxx

Solution
  1. Go to directory C:\Documents and Settings\Administrator\.android
  2. Delete the file debug.keystore and ddms.cfg

2011年11月10日 星期四

2d draw example

Android 2D Graphics Example

This example shows how to use onDraw() method and create a simple drawing program. The only significant files are Draw activity and the DrawView.

Draw.java
This activity creates the DrawView and sets it as activity's main content. It also sets the borderless window.

package com.example;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class Draw extends Activity {
    DrawView drawView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set full screen view
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                                         WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        drawView = new DrawView(this);
        setContentView(drawView);
        drawView.requestFocus();
    }
}
DrawView.java
DrawView is a view. It listens to mouse click events and draws a point at the point that it was clicked on.

package com.example;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class DrawView extends View implements OnTouchListener {
    private static final String TAG = "DrawView";

    List points = new ArrayList();
    Paint paint = new Paint();

    public DrawView(Context context) {
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);

        this.setOnTouchListener(this);

        paint.setColor(Color.WHITE);
        paint.setAntiAlias(true);
    }

    @Override
    public void onDraw(Canvas canvas) {
        for (Point point : points) {
            canvas.drawCircle(point.x, point.y, 5, paint);
            // Log.d(TAG, "Painting: "+point);
        }
    }

    public boolean onTouch(View view, MotionEvent event) {
        // if(event.getAction() != MotionEvent.ACTION_DOWN)
        // return super.onTouchEvent(event);
        Point point = new Point();
        point.x = event.getX();
        point.y = event.getY();
        points.add(point);
        invalidate();
        Log.d(TAG, "point: " + point);
        return true;
    }
}

class Point {
    float x, y;

    @Override
    public String toString() {
        return x + ", " + y;
    }
}
Results
You should see a black screen and be able to draw on it with your finger. Expand the features and enjoy it!

2011年6月30日 星期四

取得系統基本資訊

package com.demo.android.info;

import com.demo.android.info.R;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;

public class Info extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//android.os.Build.VERSION
setContentView(com.demo.android.info.R.layout.main);

TextView _tv = (TextView)findViewById(R.id.main_textview);
_tv.append("CPU type + ABI convention: "+android.os.Build.CPU_ABI+"\n");
_tv.append("Board: "+android.os.Build.BOARD+"\n");
_tv.append("Brand: "+android.os.Build.BRAND+"\n");
_tv.append("Device: "+android.os.Build.DEVICE+"\n");
_tv.append("Display: "+android.os.Build.DISPLAY+"\n");
_tv.append("Fingerprint: "+android.os.Build.FINGERPRINT+"\n");
_tv.append("Host: "+android.os.Build.HOST+"\n");
_tv.append("ID: "+android.os.Build.ID+"\n");
_tv.append("Manufacturer: "+android.os.Build.MANUFACTURER+"\n");
_tv.append("Model: "+android.os.Build.MODEL+"\n");
_tv.append("Product: "+android.os.Build.PRODUCT+"\n");
_tv.append("Tags: "+android.os.Build.TAGS+"\n");
_tv.append("Time: "+String.valueOf(android.os.Build.TIME)+"\n");
_tv.append("Type: "+android.os.Build.TYPE+"\n");
_tv.append("User: "+android.os.Build.USER+"\n");
_tv.append("The current development codename "+android.os.Build.VERSION.CODENAME+"\n") ;
_tv.append("Versoin incremental: "+android.os.Build.VERSION.INCREMENTAL+"\n");
_tv.append("The user visible version(release): "+android.os.Build.VERSION.RELEASE+"\n");
_tv.append("The user visible sdk version: "+android.os.Build.VERSION.SDK+"\n");
_tv.append("The user visible sdk version(int) : "+String.valueOf(android.os.Build.VERSION.SDK_INT)+"\n");

}
}

錄製mp4檔案

package com.demo.android.test;

import java.io.File;

import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;

public class Test extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

File file = new File("/storage/test.mp4");

try
{
if(!file.exists())
{
if(file.createNewFile())
{
Log.e("debug","create empty file successful.");
}
else
{
Log.e("debug","create empty file failure.");
}
}
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("/storage/test.mp4");
recorder.prepare();
Log.e("debug","recorder start ....");
recorder.start(); // Recording is now started
Thread.sleep(60000);
Log.e("debug","reorder stop....");
recorder.stop();
}
catch(Exception ex)
{
Log.e("debug",ex.toString());
}
}
}

2011年3月29日 星期二

Unable to execute dex: Java heap space

最近要用到第三方的庫android-sdk-windows\platforms\android-8\data\layoutlib.jar,但出現Conversion to Dalvik format failed: Unable to execute dex: null這個問題。且出現對話方塊

下面是解法:
(註: 紅色標記表示有試過但不行,綠色標記表示有試過可行。)

As Android developer, I have met a strange problem when I want use a third party jar, it remained me that:   
Dex Loader] Unable to execute dex: null   
Conversion to Dalvik format failed: Unable to execute dex: null   
out of memory error…An internal error occurred during: “Build Project”.   
Java heap space   
  
This is the DEX Error,I find the solutions by Google, one way is to modify the eclipse.ini, you can increase the memory allocated in eclipse.ini to this: -Xms128m -Xmx512m or something higher, but this didn’t work for me. After modify the eclipse.ini, I cannot start the eclipse. By StackOverflow, I find a solution:   
  
eclipse.ini file must have -vm on first line and path on second line. don’t try to put everything into one line!   
-vm   
C:\Program Files\Java\jdk1.6.0_07\bin\javaw.exe   
  
After add the -vm and the path in my eclipse.ini, I can start my Eclipse and the problem has been resolved.   
  
Another way to solve the ” Conversion to Dalvik format failed: Unable to execute dex: null” problem is using the user library, the concrete steps are as follows:   
1. Right-click the project in Eclipse and select “Build Path -> Add Libraries…”.   
2. Select User Library from the list and click Next.   
3. Click the “User Libraries…” button.   
4. Click “New…” in the User Libraries dialog.   
5. Give the user library a name and select the System library checkbox and click OK.   
6. Highlight the newly added user library in the list and click the “Add JARs…” button and add the desired jar files.   
7. Click OK on the User Libraries dialog.   
8. Make sure the new user library is checked in the Add Library dialog and …   
  
Posted by CuteAndroid  

2011年3月17日 星期四

Robotium資源

How do I run Robotium tests from the command line

By using this command:
adb shell am instrument -w com.android.foo/android.test.InstrumentationTestRunner

Reference:
http://code.google.com/p/robotium/wiki/QuestionsAndAnswers

RobotiumForPreInstalledApps

  1. Log in as root: adb root
  2. Remount: adb remount
  3. adb pull /system/app/X.apk (Replace X with the name of the application)
  4. Resign X.apk so that it has the same certificate signing as the test project (測試APK的sign需與被測試APK的sign一致。)
  5. adb pull /data/system/packages.xml
  6. Open packages.xml and remove:

    .....
  7. Push packages.xml back to device: adb push packages.xml /data/system
  8. Restart your device
  9. Push the resigned X.apk back to the device: adb push X.apk /system/app

Reference:
http://code.google.com/p/robotium/wiki/RobotiumForPreInstalledApps

白箱黑箱測試

白箱測試(White-Box Testing)
白箱測試也稱為結構性測試(Structural Testing),有時候因為牽涉到內部機密的問題,這種測試路線大都是在公司內部進行很少委外給其他公司或個人。 嚴格來說,白箱測試有兩大層面;資料流程面(Data Flow Coverage)以及控制流程面(Control Flow Coverage),資料流程面就是測試資料在系統的進出入於程式內所經過的流程,控制流程面就是測試程式在執行過程中每個階段的流程。

黑箱測試(Black-Box Testing)
測試人員並不需要對軟體的結構性有足夠深層的瞭解,所進行的測試是著重於軟體的功能面,所以也有人稱之為功能測試。 這樣的測試除了在自己公司內部進行之外,同樣的也可以委外給其他人員或是公司去執行。 為了要控制黑箱測試的執行,測試人員必須要按照測試案例(Test Cases)來逐一進行,所以Test Cases設計的好壞就會直接影響到測試結果。

Reference:
http://stevenhsu.myweb.hinet.net/softwaretesting.htm  

How to resign the Android APK

  1. 用壓縮軟體解壓縮檔案test.apk 至資料夾test,移除資料夾 “META-INF”
  2. 準備一個key store用來作簽證
    keytool -genkey -v -keystore android.keystore -alias android.keystore -keyalg RSA -validity 20000
  3. 用壓縮軟體壓縮資料夾test,將壓縮檔重新命名為test.apk.
  4. 重新對檔案test.apk作簽證
    "C:\Program Files\Java\jdk1.6.0_10\bin\jarsigner" -verbose -keystore android.keystore -storepass 123456 -signedjar ./testNew.apk ./test.apk android.keystore
  5. 得到重新簽證後的檔案testNew.apk
參考:
http://blog.csdn.net/sooner01/archive/2010/12/27/6101018.aspx

2011年3月2日 星期三

Creating Toast

Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast
.show();

2011年2月23日 星期三

java.lang.NoSuchMethodError:

寫程式測試手機reboot功能
程式碼
pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
pm.reboot(null);


權限


java.lang.Object cannot be resolved

在Eclipse中建制專案,總是報下面的錯誤。
The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files
解決辦法:引入需要的android.jar即可。 (Project->properties->Java Bulid Path )
android.jar location: 
D:\android-sdk-windows\platforms\android-#\android.jar

Eclipse找不到proguard.cfg解決辦法


這可能是sdk更新不完全造成的,需要重新手動更新一下。
  1. Eclipse--->Window--->Android SDK And AVD Manager(或者直接在Android解壓目錄底下運行SDK Setup.exe)
  2. 選擇installed packages,在右邊列表中選擇SDK tools ,點擊update all

等待更新完畢就OK了!

2011年2月16日 星期三

用logcat除錯,將log寫入檔案

  1. 在android裝置中啟動測試程式。
  2. 在PC設定裝置檔案系統權限 adb shell mount -o remount,rw rootfs /,因為之後要寫入檔案。
  3. 在PC將log寫入檔案中 adb logcat -b main -f /data/mains 。

    FORMAT: adb logcat -b main -f <remote device file name>
  4. 當測試程式執行完畢,執行 adb pull /data/mains mains_test 來取得log檔案。

    FORMAT: adb pull <remote device file name> <local file name>

2011年2月15日 星期二

Error - java.lang.VerifyError

是說當“校驗器”檢測到一個類文件雖然格式正確,但包含著一些內部不一致性或安全性問題時,拋出該錯誤。從你列出的信息這能看出這些信息具體的你依據提示信息檢查一下你的程序
補充:很可能從1,4 版本以後jdk加強了安全機製或者增進了改善這是jdk的版本問題 沒有什麼好鬱悶的



肯定是jdk版本不一致比如你用1.5的編譯出來,然後去1.4運行就可能出現這樣的問題。1.4編譯出來的,到1.5上運行就不會有這樣的問題