自從寫了第一篇之后,我一直在考慮第二篇該如何寫,要是像書本一樣一個個介紹下去甚是無味也沒這個精力,所以再三思索決定第二篇寫個快速入門,讓初學者可以迅速上手Android(當然你必須有一定的JAVA基礎),然后從下一篇開始就寫實際開發中遇到的問題以及一些API的使用方法。言歸正傳,下面我們就開始今天的介紹:
打開你的Eclipse,點擊File--->New--->Android Project,出現如下界面,我們將項目名叫做:CnBlog:
點擊Next,出現如下界面,這里用來選擇我們使用的Android版本,這里說下,你所選擇的版本決定了你所支持的最低Android ROM的版本,也決定了你可以使用的方法,比如在2.3中出現的新方法,你選擇了1.6是無法使用的,如何選擇版本取決於你項目的要求,博主選擇2.1,因為這可以兼容主流的Android手機。
選擇好之后點擊Next,會出現如下頁面,這里你可以設置你的包名,我們就設為com.cnblog.activity,讓Eclipse自動為我們建一個Activity,我們就不改了,命名為CnBlogActivity,當然你可以改為自己的名字,一般我們都使用某個具有一定意義的單詞加Activity結尾。Application Name就是你創建程序后顯示的應用程序名。
設好之后就可以點擊完成了,你就可以在Eclipse中看到我們新建的項目:
在上圖我們可以看到一個Android項目的基本結構,src里面存放着我們的源代碼,gen中我們可以看到一個R.java的java文件,這個文件是Eclipse給我們自動產生,此文件不需要開發者自行維護。往下看,我們可以看到res資源文件夾,里面有好多的文件夾,以drawable開頭的是用來存放圖片文件的,后面的h,l,m我想大家猜猜也知道,是用來區分不同分別率的。再看layout文件夾,這里面主要用來存放布局文件,而values文件夾主要用來存放一些常量文件,比如顏色常量和字符串常量。再往下看我們可以看到AndroidManifest.xml,他存儲了應用程序的組件和節點,我們寫好的Actvity,Service都需要在這里面配置,運行時才可以調用。
好,下面我們就開始我們第一個項目,啟動界面網絡檢測+調用系統撥號程序:
首先我們來設計一下啟動界面的布局:此處采用的是線性布局,里面有一個ImageView控件和一個TextView控件,至於這些標簽的意思我就不再一一細說了,網上這些方面的講解鋪天蓋地。
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6
7 <ImageView
8 android:layout_width="fill_parent"
9 android:layout_height="fill_parent"
10 android:scaleType="fitXY"
11 android:src="@drawable/welcome_logo" />
12
13 <TextView
14 android:id="@+id/welcome_textview"
15 android:layout_width="wrap_content"
16 android:layout_height="wrap_content"
17 android:layout_gravity="center_horizontal"
18 android:layout_marginTop="-120.0dip"
19 android:text="@string/welcome"
20 android:textColor="@color/white"
21 android:textSize="22.0dip" />
22
23 </LinearLayout>
布局完之后我們就可以在我們的CnBlogActivity里面來進行邏輯操作了:
1 package com.cnblog.activity;
2
3 import android.app.Activity;
4 import android.app.AlertDialog;
5 import android.app.Dialog;
6 import android.content.DialogInterface;
7 import android.content.DialogInterface.OnClickListener;
8 import android.content.Intent;
9 import android.os.Bundle;
10 import android.os.Handler;
11 import android.provider.Settings;
12 import android.widget.TextView;
13
14 import com.cnblog.utils.ProNetworkManager;
15
16 public class CnBlogActivity extends Activity {
17 private Handler mHandler;
18 private TextView welcomeTextView;
19
20 @Override
21 public void onCreate(Bundle savedInstanceState) {
22 super.onCreate(savedInstanceState);
23 // 設置Activity的布局
24 setContentView(R.layout.main);
25 // 初始化變量
26 mHandler = new Handler();
27 welcomeTextView = (TextView) findViewById(R.id.welcome_textview);
28 // 網絡檢測
29 networkCheck();
30 }
31
32 private void networkCheck() {
33 new Thread(new Runnable() {
34 @Override
35 public void run() {
36 // 休眠500毫秒
37 try {
38 Thread.sleep(500);
39 } catch (InterruptedException e) {
40 e.printStackTrace();
41 }
42 mHandler.post(new Runnable() {
43 @Override
44 public void run() {
45 welcomeTextView.setText("正在檢測網絡......");
46 if (ProNetworkManager.IsExistNet(CnBlogActivity.this)) {
47 access();
48 } else {
49 postDialog();
50 }
51 }
52 });
53 }
54 }).start();
55 }
56
57 // 進入主界面
58 private void access() {
59 Runnable mDelay = new Runnable() {
60 @Override
61 public void run() {
62 startActivity(new Intent(CnBlogActivity.this,
63 HomeActivity.class));
64 finish();
65 }
66 };
67 mHandler.postDelayed(mDelay, 1000);
68 }
69
70 // 設置網絡提示框
71 private void postDialog() {
72 mHandler.post(new Runnable() {
73 @Override
74 public void run() {
75 Dialog dialog = new AlertDialog.Builder(CnBlogActivity.this)
76 .setTitle("注意").setMessage("檢測到當前無網絡連接,是否進行設置")
77 .setPositiveButton("設置", new OnClickListener() {
78 @Override
79 public void onClick(DialogInterface dialog,
80 int which) {
81 startActivity(new Intent(
82 Settings.ACTION_WIRELESS_SETTINGS));
83 finish();
84 }
85 }).create();
86 dialog.show();
87 }
88 });
89 }
90
91 }
這個里面涉及到了判斷網絡的操作,所以我們要在Mainifest文件中添加權限:<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />至於上段代碼中涉及到的一些Android API我在這兒就不細講了,后面有機會會再講。
好了,下面我們就來寫我們的主界面,撥號程序,我們還是從布局文件開始寫:
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6
7 <TextView
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:text="@string/phone_num_attention"
11 />
12
13 <EditText
14 android:id="@+id/phonenumber_text"
15 android:layout_width="fill_parent"
16 android:layout_height="wrap_content"
17 android:hint="@string/phone_num_attention"
18 />
19
20 <Button
21 android:id="@+id/submit_button"
22 android:layout_width="wrap_content"
23 android:layout_height="wrap_content"
24 android:layout_gravity="right"
25 android:text="@string/phone_num_submit"
26 />
27
28 </LinearLayout>
很簡單的三個組件,文本,編輯框和按鈕。好了,在創建一個Activity吧,我把它命名為HomeActivity,注意在Mainifest文件中配置:
1 package com.cnblog.activity;
2
3 import java.util.regex.Matcher;
4 import java.util.regex.Pattern;
5
6 import android.app.Activity;
7 import android.content.Intent;
8 import android.net.Uri;
9 import android.os.Bundle;
10 import android.os.Handler;
11 import android.view.Gravity;
12 import android.view.View;
13 import android.view.View.OnClickListener;
14 import android.view.Window;
15 import android.widget.Button;
16 import android.widget.EditText;
17 import android.widget.Toast;
18
19 public class HomeActivity extends Activity {
20 private EditText phoneNumEditText;
21 private Button phoneNumSubmitbButton;
22 private Handler mHandler;
23
24 @Override
25 protected void onCreate(Bundle savedInstanceState) {
26 requestWindowFeature(Window.FEATURE_NO_TITLE);
27 setContentView(R.layout.home_activity);
28 super.onCreate(savedInstanceState);
29 mHandler = new Handler();
30 phoneNumEditText = (EditText) findViewById(R.id.phonenumber_text);
31 phoneNumSubmitbButton = (Button) findViewById(R.id.submit_button);
32 phoneNumSubmitbButton.setOnClickListener(new OnClickListener() {
33 @Override
34 public void onClick(View v) {
35 String number = phoneNumEditText.getText().toString();
36 if (number == null || number.equals("")) {
37 postTip("請輸入電話號碼!");
38 } else if (!checkPhone(number)) {
39 postTip("請輸入正確的電話號碼!");
40 } else {
41 Intent intent = new Intent(Intent.ACTION_DIAL, Uri
42 .parse("tel:" + number));
43 startActivity(intent);
44 finish();
45 }
46 }
47 });
48 }
49
50 private void postTip(final String message) {
51 mHandler.post(new Runnable() {
52 @Override
53 public void run() {
54 Toast toast = Toast.makeText(HomeActivity.this, message,
55 Toast.LENGTH_LONG);
56 toast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 50);
57 toast.show();
58 }
59 });
60 }
61
62 public boolean checkPhone(String phone) {
63 Pattern pattern = Pattern
64 .compile("^[1]([3][0-9]{1}|59|58|88|89)[0-9]{8}$");
65 Matcher matcher = pattern.matcher(phone);
66 if (matcher.matches()) {
67 return true;
68 }
69 return false;
70 }
71 }
上面的代碼主要是對用戶的輸入進行判斷,這里限制必須是11位的電話號碼,大家可以根據自己的想法更改。注意哦,這里需要添加調用撥號程序的權限
<uses-permission android:name="android.permission.CALL_PRIVILEGED" />,我這邊使用的是CALL_PRIVILEGED,你也可以使用CALL_PHONE,它會直接撥號,我覺得不太友好,而上一個是調用撥號鍵盤。下面是Mainifest文件
1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.cnblog.activity"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk android:minSdkVersion="7" />
8
9 <application
10 android:icon="@drawable/ic_launcher"
11 android:label="@string/app_name" >
12 <activity
13 android:label="@string/app_name"
14 android:name=".CnBlogActivity"
15 android:screenOrientation="portrait"
16 android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
17 <intent-filter >
18 <action android:name="android.intent.action.MAIN" />
19
20 <category android:name="android.intent.category.LAUNCHER" />
21 </intent-filter>
22 </activity>
23 <activity
24 android:name="HomeActivity"
25 android:screenOrientation="portrait" />
26 </application>
27
28 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
29 <uses-permission android:name="android.permission.CALL_PRIVILEGED" />
30
31 </manifest>
這個標簽android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"標明使用全屏模式。
好了,下面來測試你的程序吧,如果有真機可以直接發布到機子上,如果沒有就只有創建模擬器了,由於模擬器創建起來太慢,我就偷個懶不再敘述如何在模擬器中發布了,大家可以參考這個文章:http://book.douban.com/reading/12919079/。好了,咱們下次再續。