Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

Saturday, May 7, 2016

Sử dụng HttpUrlConnection

Từ Android 6, google đã bỏ thư viện HttpClient và thay bằng HttpUrlConnection, trong ví dụ này chúng ta sử dụng thư viện trên để kết nối với server web:

Một ví dụ nhỏ:

String urlParameters =
"username=" + URLEncoder.encode(user, "UTF-8") +
"&password=" + URLEncoder.encode(pass, "UTF-8");

URL url = new URL(address);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
//urlConnection.setRequestProperty("USER-AGENT", "Mozilla/5.0");urlConnection.setRequestProperty("ACCEPT-LANGUAGE", "en-US,en;0.5");
urlConnection.setDoOutput(true);
DataOutputStream dStream = new DataOutputStream(urlConnection.getOutputStream());
dStream.writeBytes(urlParameters);
dStream.flush();
dStream.close();
InputStream is = urlConnection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();




Wednesday, October 28, 2015

Android - List and kill background process

Android OS is a multitasking operating, there are a lot of running background process, these process make your device will be slower. Android SDK provides set of API allow developer can list all background process and kill (stop) them.
In this post, we will discussing about how listing and kill background process in Android device.

[caption id="attachment_1855" align="alignnone" width="168"]List all android background processes List all android background processes[/caption]

We need two permission are KILL_BACKGROUND_PROCESSES and to GET_TASKS.
Declare two permission as
[sourcecode language="xml"]
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
<uses-permission android:name="android.permission.GET_TASKS" />
[/sourcecode]

Declare 2 variable
[sourcecode language="java"]
List<ActivityManager.RunningAppProcessInfo> processes;
ActivityManager amg;
[/sourcecode]

Register service with Android get all running processes
[sourcecode language="java"]
// using Activity service to list all process
amg = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
// list all running process
processes = amg.getRunningAppProcesses();
[/sourcecode]

Create MyAdapter class extend BaseAdapter class to populate process's information on ListView as
[sourcecode language="java"]
public class MyAdapter extends BaseAdapter {

List<ActivityManager.RunningAppProcessInfo> processes;
Context context;

public MyAdapter(List<ActivityManager.RunningAppProcessInfo> processes, Context context) {
this.context = context;
this.processes = processes;
}

@Override
public int getCount() {
return processes.size();
}

@Override
public Object getItem(int position) {
return processes.get(position);
}

@Override
public long getItemId(int position) {
return processes.get(position).pid;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

Process pro;

if(convertView == null)
{
convertView = new TextView(context);
pro = new Process();
pro.name = (TextView)convertView;

convertView.setTag(pro);
}else
{
pro = (Process)convertView.getTag();
}

pro.name.setText(processes.get(position).processName);

return convertView;
}

class Process
{
public TextView name;
}
}
[/sourcecode]
Display list of process on listview, display name only (just demo).
[sourcecode language="java"]
adp = new MyAdapter(processes, MainActivity.this);
lst.setAdapter(adp);
[/sourcecode]

When user longclick on process name, this process will be kill
[sourcecode language="java"]
lst.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if (load == 1) {
for (ActivityManager.RunningAppProcessInfo info : processes) {
if (info.processName.equalsIgnoreCase(
((ActivityManager.RunningAppProcessInfo)parent.getItemAtPosition(position)).processName)) {
// kill selected process
android.os.Process.killProcess(info.pid);
android.os.Process.sendSignal(info.pid, android.os.Process.SIGNAL_KILL);
amg.killBackgroundProcesses(info.processName);
}
}
load = 0;
// refresh list of process
skill_Load_Process();
}
return true;
}
});
[/sourcecode]

You only kill user process, with system process you need a rooted device.

Ok, download sourcecode Here

Thursday, October 8, 2015

Hiệu chỉnh lỗi Genymotion không load VirtualBox trên window 10

Bài này chúng ta cùng fix lỗi "Virtualization engine not found" trên Genymotion
Tham khảo bài hướng dẫn cài đặt genymotion chi tiết
Khi chạy VirtualBox 5x trên window 10 thi mặc nhiên Genymotion không còn gọi được VirtualBox nữa dẫn đến lỗi "Virtualization engine not found". Chỉ cần hiệu chỉnh lại một trong các card mạng ảo của VirtualBox về dãy địa chỉ cục bộ 192.168.x.x (lớp C) là giải quyết được lỗi này.

  • Mở VirtualBox -> File -> Preferences

  • Chọn NetWork -> Host only Networks ->chọn một card mạng bất ky

  • Hiệu chỉnh lại như sau:


Bước 1:
GenyCanLoad
Bước 2:
GenyCanLoad_1
Bước 3:
GenyCanLoad_2
Bước 4:
GenyCanLoad_3

Cài đặt plugin Genymotion vào AndroidStudio
[embed]https://youtu.be/alHc903yA94[/embed]

Vậy là xong !

Friday, September 11, 2015

Android Notification (căn bản về thông báo trong Android)

Notification là dạng thông báo xuất hiện ở phần trên cùng của màn hình, dạng thông báo này hiện giời được hỗ trợ trên cả 3 nền tảng phổ biến hiện này: Android, iOS và WindowPhone.



Android cung cấp thư viện NotificationCompat.Builder, Notification để tạo các thông báo này.

  1. NotificationCompat.Builder.build(): để tạo một thông báo

  2. Phương thức notify() của Notification: để cập nhật thông báo

  3. setSmalIcon: chỉ định icon của thông báo

  4. setContentTitle: Tiêu đề thông báo

  5. setContentText: Nội dung thông báo



Ví dụ tạo một thông báo:
[sourcecode language="java"]
Notification.Builder mBuilder = new Notification.Builder(this);
mBuilder.setContentTitle("Tin nhắn mới");
mBuilder.setContentText("Có điểm thực hành môn Android.");
mBuilder.setTicker("Thông báo!"); mBuilder.setSmallIcon(R.drawable.ic_launcher);
[/sourcecode]

Cập nhật thông báo
[sourcecode language="java"]
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
/* cập nhật thông báo */
mNotificationManager.notify(notificationID, mBuilder.build());
[/sourcecode]

Thông thường các ứng dụng cấu hình khi người dùng chạm lên thông báo thì hệ thống sẽ mở lại ứng dụng đã phát sinh thông báo. Để Android có thể mở lại một ứng dụng đã thông báo (có thể là bất kỳ ứng dụng nào) hợp lệ thì chúng ta cần phải thông qua đối tượng hỗ trợ là PendingIntent

Ví dụ sau đây thực hiện theo mô tả bên trên.

[sourcecode language="java"]
/* Tạo đối tượng chỉ đến activity sẽ mở khi chọn thông báo */
Intent resultIntent = new Intent(this, NotificationView.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(NotificationView.class);
// kèm dữ liệu theo activity để xử lý
resultIntent.putExtra("events",new String[] { "Có điểm thực hành môn Android" });
resultIntent.putExtra("id",notificationID);
/* Đăng ký activity được gọi khi chọn thông báo */
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
[/sourcecode]

Sau khi mở ứng dụng (NotificationView) đã phát sinh thông báo qua hỗ trợ của PendingIntent chúng ta có thể xóa thông báo báo với phương thức cancel từ NotificartioManager

Ví dụ như sau:
[sourcecode language="java"]
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(i.getIntExtra("id", 0));
[/sourcecode]

Notification có 2 dạng cơ bản là dạng đơn và dạng danh sách.
Ví dụ:
Snap 2015-09-10 at 16.57.29
Mã nguồn ở
đây

Thursday, September 10, 2015

Direct access to SQL Server From Android

Sử dụng thư viện JTDS --> http://sourceforge.net/projects/jtds/  để truy cập SQL Server từ Android

[embed]https://youtu.be/8cp8ykUNmL4[/embed]

Download thư viện từ đây

Kết nối với SQL Server như sau:
[sourcecode language="java"]
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
conn = DriverManager
.getConnection(""
+ "jdbc:jtds:sqlserver://172.16.160.81/northwind;instance=SQL2008;"
+ "user=sa;password=sa;");
[/sourcecode]

net.sourceforge.jtds.jdbc.Driver là dạng Drive truy cập SQL Server.

Thêm dữ liệu

[sourcecode language="java"]
comm = conn.prepareStatement("insert into Employees("
+ "firstname, lastname) values(?,?)");
comm.setString(1, etFirst.getText().toString());
comm.setString(2, etLast.getText().toString());
comm.executeUpdate();
[/sourcecode]

Đọc dữ liệu

[sourcecode language="java"]
comm = conn.createStatement();
ResultSet rs = comm.executeQuery("Select EmployeeID, Firstname From Employees");
String msg = "";
while (rs.next()) {
msg += "\nID: " + rs.getInt("EmployeeID") + " Name: "
+ rs.getString("Firstname");
[/sourcecode]

Giao diện

[sourcecode language="html"]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >

<EditText
android:id="@+id/etFirstName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp"
android:background="#eeeeee"
android:hint="Firstname"
android:textColor="#000000"
android:textSize="24dp" >

<requestFocus />
</EditText>

<EditText
android:id="@+id/etLastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#eeeeee"
android:hint="Lastname"
android:padding="4dp"
android:textColor="#000000"
android:textSize="24dp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp" >

<Button
android:id="@+id/btnConnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Connect" />

<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add new" />
</LinearLayout>

<TextView
android:id="@+id/tvDs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:padding="8dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
[/sourcecode]

Mã nguồn tham khảo https://drive.google.com/file/d/0B2F9IAasWwaNYVZSc2tiZDNlNTA/view?usp=sharing

Tuesday, September 8, 2015

Http/Https Client cho Android

Thư viện giao tiếp qua http và https cho Android
https://github.com/loopj/android-async-http/wiki

Tuesday, December 16, 2014

Xác định mật độ điểm ảnh trên Android

Đoạn code sau giúp chúng ta xác định mật độ điểm ảnh và qui đổi sang đơn vị đo px (tương đương 50DP)

Tỉ lệ mật độ điểm ảnh

[sourcecode language="java"]
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
switch (metrics.densityDpi) {
case DisplayMetrics.DENSITY_LOW:
pxSize = 38;
break;
case DisplayMetrics.DENSITY_MEDIUM:
pxSize = 50;
break;
case DisplayMetrics.DENSITY_HIGH:
pxSize = 75;
break;
case DisplayMetrics.DENSITY_XHIGH:
pxSize = 100;
break;
case DisplayMetrics.DENSITY_XXHIGH:
pxSize = 150;
break;
case DisplayMetrics.DENSITY_XXXHIGH:
pxSize = 200;
break;
}
[/sourcecode]

Với sự hỗ trợ của thư viện này chúng ta xác định được tỉ lệ giữa độ đo DP và PX khá đơn giản.

Saturday, August 16, 2014

Tài liệu về thiết kê giao diện ứng dụng trên Android (android design guidelines)

Một tài liệu giới thiệu căn bản về thiết kế giao diện trên Android ngắn gọn và hữu ích cho người mới bắt đầu

Download -> android design guidelines

:)

Saturday, May 24, 2014

Ví dụ đơn gian game 2D

 

Các bạn cùng tôi học làm game 2D đơn giản qua ví dụ sao

Giới thiệu: GAME BẮN MÁY BAY

Trò chơi máy bay bắn thiên thạch: người chơi điều khiển máy bay né thiên thạch và có thể bắn các thiên thạch này để tính điểm, khi máy bay chạm thiên thạch sẽ bị nổ và xem như thua(viết trên nền java thuần).

1. Tại màn hình chờ máy bay sẽ nhấp nháy liên tục với âm thanh máy bay, chạm màn hình chờ để bắt đầu chơi







clip_image002clip_image004

2. Khởi tạo với 05 thiên thạch bay từ phải qua trái, thiên thạch bị bắn sẽ khởi tạo ngay lại một thiên thạch mới từ bên phải và có tốc độ bay qua trái ngẫu nhiên.

3. Mỗi viên bắn trúng được 1 điểm

4. Trò chơi sử dụng cảm biến gia tốc để điều khiển máy bay theo bốn hướng







clip_image006clip_image008

5. Chạm màn hình để bắn ra đạn mỗi giây bắn tối đa được 02 viên đạn.

6. Khi thiên thạch chạm máy bay thì máy bay nổ và tro chơi kết thúc.

clip_image010

7. Máy bay, thiên thạch và đạn được kiểm tra chạm nhau bằng đương tròn bao quanh đối tượng

Test thử cho Android :D (Phản hồi giúp tôi nhé – cảm ơn các bạn) ==>[embed]https://play.google.com/store/apps/details?id=vn.cusc.banmaybay[/embed]

Bài tiếp theo tôi sẽ trình bài phần nguyên lý cơ bản của một game cơ bản :D và mã giải thích mã nguồn của game nay.

Monday, May 12, 2014

PHP to JSON – Chuyển dữ liệu sang JSON trong PHP

JSON đang dần thay thế XML trong việc làm định dữ liệu trung gian giữa các hệ thống khác nhau. Đối với PHP phiên bản lớn hơn 5.4.0 cũng đã hỗ trợ thư viện chuyển đối sang JSON và ngược lại.

JsonSerializable::jsonSerialize là thư việc chuyển từ PHP sang JSON với sự hỗ trợ của hàm json_encode

Ví dụ với dữ liệu chuỗi đơn giản

1 <?php
2 // tạo lớp chuyển đối
3 class StringValue implements JsonSerializable {
4 public function __construct($string) {
5 $this->string = (string) $string;
6 }
7
8 public function jsonSerialize() {
9 return $this->string;
10 }
11 }
12 // in ra với client có thể lấy dữ liệu này về
13 echo json_encode(new StringValue('Hello!'), JSON_PRETTY_PRINT);
14 ?>


Ví dụ với số nguyên


1 <?php
2 class IntegerValue implements JsonSerializable {
3 public function __construct($number) {
4 $this->number = (integer) $number;
5 }
6 public function jsonSerialize() {
7 return $this->number;
8 }
9 }
10 echo json_encode(new IntegerValue(1), JSON_PRETTY_PRINT);
11 ?>

Kết quả


1


Với định dạng mảng


1 <?php
2 class ArrayValue implements JsonSerializable {
3 public function __construct(array $array) {
4 $this->array = $array;
5 }
6
7 public function jsonSerialize() {
8 return $this->array;
9 }
10 }
11 $array = [1, 2, 3];
12 echo json_encode(new ArrayValue($array), JSON_PRETTY_PRINT);
13 ?>

Kết quả


1 [
2 1,
3 2,
4 3
5 ]


Tương tự như vậy cho mảng chuỗi


1 <?php
2 class ArrayValue implements JsonSerializable {
3 public function __construct(array $array) {
4 $this->array = $array;
5 }
6
7 public function jsonSerialize() {
8 return $this->array;
9 }
10 }
11
12 $array = ['foo' => 'bar', 'quux' => 'baz'];
13 echo json_encode(new ArrayValue($array), JSON_PRETTY_PRINT);
14 ?>

Và kết quả chúng ta có là


1 {
2 "foo": "bar",
3 "quux": "baz"
4 }

Ví dụ chúng ta lấy dữ liệu từ MySQL và chuyển sang JSON


http://www.w3resource.com/JSON/JSON-example.php


Ví dụ chúng ta có bảng


sanpham(id,ten,gia)















idtengia
1Nokia 5253000000
2Apple Macbook Pro20000000

1 $sql="select * from sanpham";
2 $result=mysql_query($sql);
3 $json=array();
4 while($row=mysql_fetch_array($result))
5 {
6 $sanpham=array();
7 $sanpham["id"]=$row["id"];
8 $sanpham["ten"]=$row["ten"];
9 $sanpham["gia"]=$row["gia"];
10
11 array_push($json["sanpham"],$sanpham);
12 }
13 echo json_encode($json);

Và kết quả là

1 "sanpham":
2 [
3 {
4 "id":"1",
5 "ten":"Nokia 525",
6 "gia":"3000000",
7 },
8 {
9 "id":"2",
10 "ten":"Apple Macbook Pro",
11 "gia":"20000000"
12 }
13 ]


Cũng hay đó chứ !


Kết hợp với bài này sẽ giúp xử lý JSON trên Android nhẹ nhàng hơn

Kiểm tra trạng thái mạng Android (network state checking)

Trong lập trình giao tiếp mạng trên Android thì việc kiểm tra sự sẵn sàng của khả năng kết nối mạng (Wifi, 3g), GPS là cần thiết.

Screenshot_2014-05-12-10-31-47 Screenshot_2014-05-12-10-31-56 Screenshot_2014-05-12-10-32-05 Screenshot_2014-05-12-10-32-12

Việc kiểm tra này cũng rất nhẹ nhàng bởi Android cung cấp cho chúng ta nhiều thư viện để kiểm tra các loại kết nối và cấu hình kết nối.

Tuy nhiên, ứng dụng cần phải có quyền kiểm tra trạng thái kết nối và sử dụng kết nối.

1 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
2 <uses-permission android:name="android.permission.INTERNET" />
3 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


Đối tượng ConnectivityManager và NetworkInfo là đối tượng chính giúp chúng ta kiểm tra trạng thái mạng



1 ConnectivityManager conManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
2 NetworkInfo _wifi = conManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
3 NetworkInfo _3g = conManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
Sau đó chúng ta có thể gọi hộp thoại bật wifi hay 3g để mở mạng nếu chưa có


1 if(!_wifi.isAvailable()&&!_3g.isAvailable())
2 {
3 showNetworkSettingsAlert();
4 }

Nội dung hàm hiển thị hộp thoại như sau


1 public void showNetworkSettingsAlert() {
2 AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
3 alertDialog.setTitle("C?u hình m?ng");
4 alertDialog.setMessage("Chua b?t k?t n?i m?ng.\nDi chuy?n d?n giao di?n c?u hình?");
5 alertDialog.setPositiveButton("C?u hình",
6 new DialogInterface.OnClickListener() {
7 public void onClick(DialogInterface dialog, int which) {
8 Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
9 MainActivity.this.startActivity(intent);
10 }
11 });
12 alertDialog.setNegativeButton("B? qua",
13 new DialogInterface.OnClickListener() {
14 public void onClick(DialogInterface dialog, int which) {
15 dialog.cancel();
16 }
17 });
18 alertDialog.show();
19 }

Thử xem sao nhe ! good luck

Tuesday, May 6, 2014

Ứng dụng hiệu ứng để chuyển activity trong Android





Một ví dụ nhỏ để hình dung animation trong Android

Tạo hiệu ứng chuyển activity

- Khi chuyển activity thay vì lật đơn giản chúng ta tạo hiệu ứng zoom-in và zoom-out để chuyển activity.

Bước 1. Tạo các hoạt cảnh với XML file

Tạo file in.xml và out.xml trong thư mục anim (lưu ý chính xác tên thư mục)

- Nôi dung in.xml

1 <set xmlns:android="http://schemas.android.com/apk/res/android" >
2 <alpha
3 android:duration="3000"
4 android:fillAfter="true"
5 android:fromAlpha="0.0"
6 android:toAlpha="1.0" >
7 </alpha>
8 </set>

- Nội dung out.xml


1 <set xmlns:android="http://schemas.android.com/apk/res/android" >
2 <alpha
3 android:duration="4000"
4 android:fillAfter="true"
5 android:fromAlpha="1.0"
6 android:toAlpha="0.0" >
7 </alpha>
8
9 <rotate
10 android:duration="3000"
11 android:fillAfter="true"
12 android:fromDegrees="0"
13 android:interpolator="@android:anim/accelerate_decelerate_interpolator"
14 android:pivotX="50%"
15 android:pivotY="50%"
16 android:toDegrees="360" >
17 </rotate>
18 </set>

Bước 2. Gán hình nền cho màn hình chào

Mở lại layout màn hình chào và gán thuộc tính background và đặt tên cho RelativeLayout. Hình s_1 của background là hình nằm trong thư mục drawable

1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:id="@+id/bg"
4 android:background="@drawable/s_1"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent"
7 android:paddingBottom="@dimen/activity_vertical_margin"
8 android:paddingLeft="@dimen/activity_horizontal_margin"
9 android:paddingRight="@dimen/activity_horizontal_margin"
10 android:paddingTop="@dimen/activity_vertical_margin"
11 tools:context="vn.cusc.animation.Chao$PlaceholderFragment" >
12 </RelativeLayout>

Bước 3. Gán hiệu ứng rõ dần cho màn hình chào và hiệu ứng chuyển sang giao diện chính


1 public class Chao extends Activity {
2 RelativeLayout rl;
3 @Override
4 protected void onCreate(Bundle savedInstanceState) {
5 super.onCreate(savedInstanceState);
6 setContentView(R.layout.activity_chao);
7 // tham chi?u d?n background activity
8 rl = (RelativeLayout)findViewById(R.id.bg);
9 // load ho?t hình cho màn hinh n?n
10 Animation a = AnimationUtils.loadAnimation(this, R.anim.in);
11 // ch?y hi?u ?ng rõ d?n
12 rl.setAnimation(a);
13 CountDownTimer t = new CountDownTimer(2000,1000) {
14 @Override
15 public void onTick(long millisUntilFinished) {
16 // c?u trúc màu ARGB trong dó A là alpha là d? trong su?t
17 }
18 @Override
19 public void onFinish() {
20 Intent i = new Intent(getApplicationContext(), Main.class);
21 startActivity(i);
22 overridePendingTransition(R.anim.in, R.anim.out);
23 finish();
24 }
25 };
26 t.start();
27 }
28 }
Bây giờ chạy lại ứng dụng quan sát hiệu ứng

Sunday, May 4, 2014

Giả lập Android với Genymotion

Trong quá trình phát triển ứng ụng trên Android chúng ta thương sử dụng máoy ảo (giả lập) hệ điều hành Android để kiểm thử ứng dụng hay có thể chạy trên máy thật.

Tuy nhiên đối với bộ giả lập tích hợp trong ADT hay ADT plugin trên eclipse rất nặng nề và chậm chạm còn đối với thiết bị thật thì rất khó đế có thể có nhiều thiết bị để test với các kích thước khác nhau của màn hình.

Genymotion là một giải pháp tốt: nhanh và nhẹ giúp việc phát triển và kiểm thử ứng dụng trở nên nhẹ nhành hơn.

Tổng quan genymotion (Video)

  1. Đăng ký/ đăng nhập vào genymotion
  2. Download file cài đặt cho window 64 bits (with VirtualBox) v2.2.0
  3. Cài đặt (VirtualBox và genymotion)
  4. Tạo máy ảo Android

image Nhấn Add để tạo (cần đăng nhập tài khoản tạo ở bước 1)

image Chọn phiên bản phù hợp và nhấn Next

image

Đặt tên và nhấn Next

 image

Đợi download xong sex thấy máy ở trong danh sách như hình 1. Khởi động máy ảo bằng cách chọn 1 máy ảo rồi nhấn Play

image Đợi một tí máy ảo sẽ khởi động xong (nhanh hơn máy ảo tích hợp trong ATD nhiều)

imageVà đây là kết quả. Tuy nhiên, để có được google play services thì cần phải làm một số bước nữa

  1. Download thư viện
  2. Kéo thả file download vào máy ảo ( cứ nhấn OK cho đến khi xong)

image 

image

 image Reboot lại máy ảo để cập nhật.

Đăng nhập với tài khoản google và test các dịch vụ của goole trên máy ảo genymotion

image Nhấn Yes nếu đã có tài khoản, không thì đăng ký nhé

image Nhấn OK và nhấn chấp nhận các điều khoản cho đến khi hoàn thành

imageGiờ thì đã thấy Play Store rồi đó :).

image  Và đây là cài đặt từ play store

imageChạy YouTube trên máy ảo genymotion.

Tới đây thì các bạn có thể tự tạo các máy ảo với các phiên bản Android khác nhau để kiểm thử ứng dụng của mình.

 

GOOD LUCK.

Friday, May 2, 2014

JSON format and visual display - http://jsonviewer.stack.hu/

Trang web miễn phí giúp việc phân tích json trở nên đơn giản hơn

Ví dụ với playlist của YouTube trả về https://gdata.youtube.com/feeds/api/playlists/PLXImJ6jTTEi4JYaz0NrO0_bCEfn-eaCwz?v=2&alt=jsonc

Thay vì

1 {"apiVersion":"2.1","data":{"id":"PLXImJ6jTTEi4JYaz0NrO0_bCEfn-eaCwz","author":"ntdanable","title":"Videos winform C#","description":"","thumbnail":{"sqDefault":"https://i.ytimg.com/vi/OYV9xsZSFgA/default.jpg","hqDefault":"https://i.ytimg.com/vi/OYV9xsZSFgA/hqdefault.jpg"},"content":{"5":"http://www.youtube.com/p/PLXImJ6jTTEi4JYaz0NrO0_bCEfn-eaCwz"},"totalItems":10,"startIndex":1,"itemsPerPage":25,"items":[{"id":"PL7Dmo3mHp_ePvPxuTq_LDHK6PrZ9Ec3eGW8jZwOqehVg","position":1,"author":"ntdanable","video":{"id":"OYV9xsZSFgA","uploaded":"2012-12-27T12:25:13.000Z","updated":"2013-12-31T10:05:51.000Z","uploader":"ntdanable","category":"Education","title":"Bu?i 00","description":"T?ng quan v? window form","thumbnail":{"sqDefault":"https://i1.ytimg.com/vi/OYV9xsZSFgA/default.jpg","hqDefault":"https://i1.ytimg.com/vi/OYV9xsZSFgA/hqdefault.jpg"},"player":{"default":"https://www.youtube.com/watch?v=OYV9xsZSFgA&feature=youtube_gdata_player","mobile":"https://m.youtube.com/details?v=OYV9xsZSFgA"},"content":{"5":"https://www.youtube.com/v/OYV9xsZSFgA?version=3&f=playlists&app=youtube_gdata","1":"rtsp://r8---sn-a5m7zu7d.c.youtube.com/CiULENy73wIaHAkAFlLGxn2FORMYDSANFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp","6":"rtsp://r8---sn-a5m7zu7d.c.youtube.com/CiULENy73wIaHAkAFlLGxn2FORMYESARFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp"},"duration":5323,"viewCount":552,"favoriteCount":0,"commentCount":1,"accessControl":{"comment":"allowed","commentVote":"allowed","videoRespond":"moderated","rate":"allowed","embed":"allowed","list":"allowed","autoPlay":"allowed","syndicate":"allowed"}}},{"id":"PL7Dmo3mHp_ePvPxuTq_LDHC7bijtgLtjeNAKrbqDHgU8","position":2,"author":"ntdanable","video":{"id":"0upGEc3Ij1k","uploaded":"2012-12-27T12:16:55.000Z","updated":"2013-12-31T10:05:51.000Z","uploader":"ntdanable","category":"Education","title":"OnWFC P1","description":"","thumbnail":{"sqDefault":"https://i1.ytimg.com/vi/0upGEc3Ij1k/default.jpg","hqDefault":"https://i1.ytimg.com/vi/0upGEc3Ij1k/hqdefault.jpg"},"player":{"default":"https://www.youtube.com/watch?v=0upGEc3Ij1k&feature=youtube_gdata_player","mobile":"https://m.youtube.com/details?v=0upGEc3Ij1k"},"content":{"5":"https://www.youtube.com/v/0upGEc3Ij1k?version=3&f=playlists&app=youtube_gdata","1":"rtsp://r2---sn-a5m7zu7k.c.youtube.com/CiULENy73wIaHAlZj8jNEUbq0hMYDSANFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp","6":"rtsp://r2---sn-a5m7zu7k.c.youtube.com/CiULENy73wIaHAlZj8jNEUbq0hMYESARFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp"},"duration":1403,"viewCount":298,"favoriteCount":0,"commentCount":2,"accessControl":{"comment":"allowed","commentVote":"allowed","videoRespond":"moderated","rate":"allowed","embed":"allowed","list":"allowed","autoPlay":"allowed","syndicate":"allowed"}}},{"id":"PL7Dmo3mHp_ePvPxuTq_LDHPnX3WIaCI7jfK987P_Pbxs","position":3,"author":"ntdanable","video":{"id":"kG3qahrmhOU","uploaded":"2012-12-27T12:23:54.000Z","updated":"2013-12-31T10:05:51.000Z","uploader":"ntdanable","category":"Education","title":"OnWFC P2","description":"Vdeo hu?ng d?n t?o vào s? d?ng ADO.NET khai khác d? li?u Sql server s? d?ng visual studiio 2005.","thumbnail":{"sqDefault":"https://i1.ytimg.com/vi/kG3qahrmhOU/default.jpg","hqDefault":"https://i1.ytimg.com/vi/kG3qahrmhOU/hqdefault.jpg"},"player":{"default":"https://www.youtube.com/watch?v=kG3qahrmhOU&feature=youtube_gdata_player","mobile":"https://m.youtube.com/details?v=kG3qahrmhOU"},"content":{"5":"https://www.youtube.com/v/kG3qahrmhOU?version=3&f=playlists&app=youtube_gdata","1":"rtsp://r2---sn-a5m7zu7l.c.youtube.com/CiULENy73wIaHAnlhOYaauptkBMYDSANFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp","6":"rtsp://r2---sn-a5m7zu7l.c.youtube.com/CiULENy73wIaHAnlhOYaauptkBMYESARFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp"},"duration":2963,"viewCount":294,"favoriteCount":0,"commentCount":2,"accessControl":{"comment":"allowed","commentVote":"allowed","videoRespond":"moderated","rate":"allowed","embed":"allowed","list":"allowed","autoPlay":"allowed","syndicate":"allowed"}}},{"id":"PL7Dmo3mHp_ePvPxuTq_LDHJ0vy_-UoOTFSW2Mt7EMF0Q","position":4,"author":"ntdanable","video":{"id":"YMZ5gYc9g3o","uploaded":"2012-12-27T12:25:02.000Z","updated":"2013-12-31T10:05:51.000Z","uploader":"ntdanable","category":"Education","title":"OnWFC P3","description":"","thumbnail":{"sqDefault":"https://i1.ytimg.com/vi/YMZ5gYc9g3o/default.jpg","hqDefault":"https://i1.ytimg.com/vi/YMZ5gYc9g3o/hqdefault.jpg"},"player":{"default":"https://www.youtube.com/watch?v=YMZ5gYc9g3o&feature=youtube_gdata_player","mobile":"https://m.youtube.com/details?v=YMZ5gYc9g3o"},"content":{"5":"https://www.youtube.com/v/YMZ5gYc9g3o?version=3&f=playlists&app=youtube_gdata","1":"rtsp://r4---sn-a5m7zu7s.c.youtube.com/CiULENy73wIaHAl6gz2HgXnGYBMYDSANFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp","6":"rtsp://r4---sn-a5m7zu7s.c.youtube.com/CiULENy73wIaHAl6gz2HgXnGYBMYESARFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp"},"duration":1251,"viewCount":174,"favoriteCount":0,"commentCount":0,"accessControl":{"comment":"allowed","commentVote":"allowed","videoRespond":"moderated","rate":"allowed","embed":"allowed","list":"allowed","autoPlay":"allowed","syndicate":"allowed"}}},{"id":"PL7Dmo3mHp_ePvPxuTq_LDHGDgBXE7TNsK2yc_9NIM4d4","position":5,"author":"ntdanable","video":{"id":"S778YynB308","uploaded":"2012-12-27T12:28:48.000Z","updated":"2013-12-31T10:05:51.000Z","uploader":"ntdanable","category":"Education","title":"OnWFC P4","description":"","thumbnail":{"sqDefault":"https://i1.ytimg.com/vi/S778YynB308/default.jpg","hqDefault":"https://i1.ytimg.com/vi/S778YynB308/hqdefault.jpg"},"player":{"default":"https://www.youtube.com/watch?v=S778YynB308&feature=youtube_gdata_player","mobile":"https://m.youtube.com/details?v=S778YynB308"},"content":{"5":"https://www.youtube.com/v/S778YynB308?version=3&f=playlists&app=youtube_gdata","1":"rtsp://r8---sn-a5m7zu76.c.youtube.com/CiULENy73wIaHAlP38EpY_y-SxMYDSANFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp","6":"rtsp://r8---sn-a5m7zu76.c.youtube.com/CiULENy73wIaHAlP38EpY_y-SxMYESARFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp"},"duration":1536,"viewCount":207,"favoriteCount":0,"commentCount":0,"accessControl":{"comment":"allowed","commentVote":"allowed","videoRespond":"moderated","rate":"allowed","embed":"allowed","list":"allowed","autoPlay":"allowed","syndicate":"allowed"}}},{"id":"PL7Dmo3mHp_ePvPxuTq_LDHKI3jap7XpsvUL45gGFZWo8","position":6,"author":"ntdanable","video":{"id":"XgNE9zkCjog","uploaded":"2012-12-27T12:35:59.000Z","updated":"2013-12-31T10:05:51.000Z","uploader":"ntdanable","category":"Education","title":"OnWFC P6","description":"","thumbnail":{"sqDefault":"https://i1.ytimg.com/vi/XgNE9zkCjog/default.jpg","hqDefault":"https://i1.ytimg.com/vi/XgNE9zkCjog/hqdefault.jpg"},"player":{"default":"https://www.youtube.com/watch?v=XgNE9zkCjog&feature=youtube_gdata_player","mobile":"https://m.youtube.com/details?v=XgNE9zkCjog"},"content":{"5":"https://www.youtube.com/v/XgNE9zkCjog?version=3&f=playlists&app=youtube_gdata","1":"rtsp://r2---sn-a5m7zu7k.c.youtube.com/CiULENy73wIaHAmIjgI590QDXhMYDSANFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp","6":"rtsp://r2---sn-a5m7zu7k.c.youtube.com/CiULENy73wIaHAmIjgI590QDXhMYESARFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp"},"duration":1742,"viewCount":234,"favoriteCount":0,"commentCount":0,"accessControl":{"comment":"allowed","commentVote":"allowed","videoRespond":"moderated","rate":"allowed","embed":"allowed","list":"allowed","autoPlay":"allowed","syndicate":"allowed"}}},{"id":"PL7Dmo3mHp_ePvPxuTq_LDHCw8uQHFuWESr_dm2jmzjPQ","position":7,"author":"ntdanable","video":{"id":"WSYD-XUr1a8","uploaded":"2012-12-27T12:33:21.000Z","updated":"2013-12-31T10:05:51.000Z","uploader":"ntdanable","category":"Education","title":"OnWFC P5","description":"Vdeo hu?ng d?n t?o vào s? d?ng crystal report v?i C# s? d?ng visual studio 2005","thumbnail":{"sqDefault":"https://i1.ytimg.com/vi/WSYD-XUr1a8/default.jpg","hqDefault":"https://i1.ytimg.com/vi/WSYD-XUr1a8/hqdefault.jpg"},"player":{"default":"https://www.youtube.com/watch?v=WSYD-XUr1a8&feature=youtube_gdata_player","mobile":"https://m.youtube.com/details?v=WSYD-XUr1a8"},"content":{"5":"https://www.youtube.com/v/WSYD-XUr1a8?version=3&f=playlists&app=youtube_gdata","1":"rtsp://r1---sn-a5m7zu7z.c.youtube.com/CiULENy73wIaHAmv1St1-QMmWRMYDSANFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp","6":"rtsp://r1---sn-a5m7zu7z.c.youtube.com/CiULENy73wIaHAmv1St1-QMmWRMYESARFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp"},"duration":2559,"viewCount":305,"favoriteCount":0,"commentCount":0,"accessControl":{"comment":"allowed","commentVote":"allowed","videoRespond":"moderated","rate":"allowed","embed":"allowed","list":"allowed","autoPlay":"allowed","syndicate":"allowed"}}},{"id":"PL7Dmo3mHp_ePvPxuTq_LDHALFxzLmar49wl_V7mHZt5Y","position":8,"author":"ntdanable","video":{"id":"6isPeiXcFx4","uploaded":"2012-12-28T09:56:35.000Z","updated":"2013-12-31T10:05:51.000Z","uploader":"ntdanable","category":"Education","title":"OnWFC P7","description":"Vdeo hu?ng d?n t?o vào s? d?ng usercontrol và custom control v?i C# s? d?ng visual studiio 2005.","thumbnail":{"sqDefault":"https://i1.ytimg.com/vi/6isPeiXcFx4/default.jpg","hqDefault":"https://i1.ytimg.com/vi/6isPeiXcFx4/hqdefault.jpg"},"player":{"default":"https://www.youtube.com/watch?v=6isPeiXcFx4&feature=youtube_gdata_player","mobile":"https://m.youtube.com/details?v=6isPeiXcFx4"},"content":{"5":"https://www.youtube.com/v/6isPeiXcFx4?version=3&f=playlists&app=youtube_gdata","1":"rtsp://r1---sn-a5m7zu7s.c.youtube.com/CiULENy73wIaHAkeF9wleg8r6hMYDSANFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp","6":"rtsp://r1---sn-a5m7zu7s.c.youtube.com/CiULENy73wIaHAkeF9wleg8r6hMYESARFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp"},"duration":2587,"viewCount":152,"favoriteCount":0,"commentCount":0,"accessControl":{"comment":"allowed","commentVote":"allowed","videoRespond":"moderated","rate":"allowed","embed":"allowed","list":"allowed","autoPlay":"allowed","syndicate":"allowed"}}},{"id":"PL7Dmo3mHp_ePvPxuTq_LDHHiP4d82uww8rGK6GINEnNo","position":9,"author":"ntdanable","video":{"id":"p4T5UmUvGD8","uploaded":"2012-12-28T09:51:38.000Z","updated":"2013-12-31T10:05:51.000Z","uploader":"ntdanable","category":"Education","title":"OnWFC P8","description":"On tap winform P8","thumbnail":{"sqDefault":"https://i1.ytimg.com/vi/p4T5UmUvGD8/default.jpg","hqDefault":"https://i1.ytimg.com/vi/p4T5UmUvGD8/hqdefault.jpg"},"player":{"default":"https://www.youtube.com/watch?v=p4T5UmUvGD8&feature=youtube_gdata_player","mobile":"https://m.youtube.com/details?v=p4T5UmUvGD8"},"content":{"5":"https://www.youtube.com/v/p4T5UmUvGD8?version=3&f=playlists&app=youtube_gdata","1":"rtsp://r8---sn-a5m7zu7r.c.youtube.com/CiULENy73wIaHAk_GC9lUvmEpxMYDSANFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp","6":"rtsp://r8---sn-a5m7zu7r.c.youtube.com/CiULENy73wIaHAk_GC9lUvmEpxMYESARFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp"},"duration":641,"rating":5.0,"likeCount":"2","ratingCount":2,"viewCount":144,"favoriteCount":0,"commentCount":0,"accessControl":{"comment":"allowed","commentVote":"allowed","videoRespond":"moderated","rate":"allowed","embed":"allowed","list":"allowed","autoPlay":"allowed","syndicate":"allowed"}}},{"id":"PL7Dmo3mHp_ePvPxuTq_LDHGR96xIFPXq11HLesHWMmtc","position":10,"author":"ntdanable","video":{"id":"75kdIx-9E9E","uploaded":"2013-02-20T13:43:11.000Z","updated":"2014-04-24T18:55:14.000Z","uploader":"ntdanable","category":"Education","title":"Form Login - User Authentication with database visaul c#","description":"Form Login - User Authentication with user from sql server 2005 - c#","thumbnail":{"sqDefault":"https://i1.ytimg.com/vi/75kdIx-9E9E/default.jpg","hqDefault":"https://i1.ytimg.com/vi/75kdIx-9E9E/hqdefault.jpg"},"player":{"default":"https://www.youtube.com/watch?v=75kdIx-9E9E&feature=youtube_gdata_player","mobile":"https://m.youtube.com/details?v=75kdIx-9E9E"},"content":{"5":"https://www.youtube.com/v/75kdIx-9E9E?version=3&f=playlists&app=youtube_gdata","1":"rtsp://r8---sn-a5m7zu7r.c.youtube.com/CiULENy73wIaHAnRE70fIx2Z7xMYDSANFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp","6":"rtsp://r8---sn-a5m7zu7r.c.youtube.com/CiULENy73wIaHAnRE70fIx2Z7xMYESARFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp"},"duration":848,"rating":5.0,"likeCount":"2","ratingCount":2,"viewCount":2752,"favoriteCount":0,"commentCount":2,"accessControl":{"comment":"allowed","commentVote":"allowed","videoRespond":"moderated","rate":"allowed","embed":"allowed","list":"allowed","autoPlay":"allowed","syndicate":"allowed"}}}]}}

thông qua website này chúng ta có như sau


image


Đối với tôi nó giúp tôi đọc json bằng mắt nhanh hơn.

Translate