黑馬程序員_android筆記1

---------------------- android培訓java培訓、期待與您交流! ----------------------



         第一天

1、安裝:

所需開發環境:

JDK 5 或 JDK 6 (僅有JRE不夠Eclipse 3.5 (galileo)

下載用於在Eclipse 開發android應用的ADT 插件 

下載地址:http://dl.google.com/android/ADT-0.9.7.zip

安裝 Eclipse 插件 (ADT)

啓動 Eclipse,選擇 Help > Install New Software,在出現的對話框裏,點擊Add按鈕,在對話框的name一欄輸入“ADT”, 然後點擊Archive...,瀏覽和選擇已經下載的ADT插件壓縮文件。 

點擊 OK.。返回可用軟件的視圖,你會看到這個插件,然後選擇Developer Tools (會選中下面的“Android Developer Tools”和 “Android Editors),點擊 Next,最後重啓 Eclipse

下載安裝Android SDK

Android SDK包含了開發Android應用所依賴的jar文件、運行環境及相關工具。  

下載地址: http://dl.google.com/android/android-sdk_r06-windows.zip

下載完SDK後,把.zip文件解壓到你電腦上合適位置。啓動 Eclipse,選擇window->preferences,在打開的視圖左邊點擊android,在右邊的SDK Location中選擇Android SDK所在位置。

然後建立模擬器:window中得Android sdk and AVD Manager中新建

2、卸掉:

刪除Eclipse 安裝目錄下features文件夾下的

com.android.ide.eclipse.adt_0.9.5.v200911191123-20404.jar

com.android.ide.eclipse.ddms_0.9.5.v200911191123-20404.jar

還有plugins文件夾下的

com.android.ide.eclipse.adt_0.9.5.v200911191123-20404.jarcom.android.ide.eclipse.ddms_0.9.5.v200911191123-20404.jar

然後重啓Eclipse

3、打開Android模擬器時,出現無信號,拔打電話或發短信時,提示“尚未註冊網絡”錯誤信息的解決方案如下。

  場景一:你的電腦沒有連接上互聯網,同時也沒有在局域網。

  解決辦法:右鍵點擊網上鄰居,選擇"屬性",在網絡連接窗口中右鍵點擊"本地連接",選擇"屬性",設置TCP/IP屬性如下:

     IP地址:192.168.1.100

     子網掩碼:255.255.255.0

     默認網關:192.168.1.100

     首選DNS服務器:192.168.1.100

  場景二:你的電腦沒有連接上互聯網,但在局域網。

  解決辦法:右鍵點擊網上鄰居,選擇"屬性",在網絡連接窗口中右鍵點擊"本地連接",選擇"屬性",設置TCP/IP屬性如下:

     IP地址:設置成你所在局域網的IP,如:192.168.1.100 

     子網掩碼:設置成你所在局域網的掩碼,如:255.255.255.0

     默認網關:設置成你所在局域網的網關,一般網關的IP格式爲:*.*.*.1,如:192.168.1.1

     首選DNS服務器:設置成你所在局域網的路由器IP,一般路由器的IP格式爲:*.*.*.1,如:192.168.1.1

  最後一種解決方案是:讓你的電腦連接上互聯網。

                                     第一個android應用hello

Hello.java應用資源清單解釋:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

     

 package="liuxu.myfirstandroid"

    

  android:versionCode="1"

 

     android:versionName="1.0">

 

   <application android:icon="@drawable/icon" 

android:label="@string/app_name">

      

  <activity android:name=".Hello"

        

          android:label="@string/app_name">

       

     <intent-filter>

     

           <action android:name="android.intent.action.MAIN" />

      

          <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

    </activity>

    </application>

    <uses-sdk android:minSdkVersion="8" />

</manifest>

當用戶單擊應用圖標時,系統會將點擊封裝成一個IntendIntend包含動作名稱和類別,將此意圖發給意圖處理器,然後意圖處理器 會按照Intend尋找相應的組件顯示。

意圖會找到Hello.java類,對這個類進行實例化。

實例化完成後調用onCreate方法,這個方法只會執行一次。然後執行setContentView方法用於顯示一個界面。

package liuxu.myfirstandroid;

import android.app.Activity;

import android.os.Bundle;

public class Hello extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

    }

}

main.xml佈局文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<TextView  

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="@string/hello"

    />

</LinearLayout>

                           電話撥號器

效果圖:    

             

界面顯示文字引用設置:

NewFile.xml(位於values目錄,相當於strings.xml的作用)

<?xml version="1.0" encoding="UTF-8"?>

<resources>

    <string name="input">請輸入電話號碼:</string>

    <string name="dialog">撥打</string>

</resources>

Activity佈局設置:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<TextView  

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="@string/input"

    />

 <EditText   

    android:layout_width="fill_parent" 

    android:layout_height="100px" 

    android:id="@+id/content"

   />

 <Button    

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text="@string/dialog"

    android:id="@+id/button"

    />

</LinearLayout>

電話撥號組件清單描述(這個組件有很多activity,每個activity又有幾個意圖過濾器,這裏只列出撥打電話的activity的意圖過濾器)

 …………

<activity android:name="OutgoingCallBroadcaster"

                android:permission="android.permission.CALL_PHONE"

                android:theme="@android:style/Theme.NoDisplay"

                android:configChanges="orientation|keyboardHidden">

            <!-- CALL action intent filters, for the various ways

                 of initiating an outgoing call. -->

            <intent-filter>  <!--撥打電話是需要匹配的意圖過濾器-->

                <action android:name="android.intent.action.CALL" />

                <category android:name="android.intent.category.DEFAULT" />

                <data android:scheme="tel" />

            </intent-filter>

…………

</activity>

…………

PhoneActivity.class:

public class PhoneActivity extends Activity {

EditText phoneText;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        phoneText=(EditText) findViewById (R.id.phone);

        Button button=(Button)findViewById(R.id.button);

        button.setOnClickListener(new ButtonListener());

    }

    private class ButtonListener implements OnClickListener{

          @Override

        public void onClick(View v) {

        

         String phoneNo = phoneText.getText().toString();

         Intent intent=new Intent("android.intent.action.CALL" ,Uri.parse("tel:"+phoneText.getText()));

         startActivity(intent);

        }

    }    

}

從上可知打電話權限的使用:在Manifest文件中申明權限:

<uses-permission android:name="android.permission.CALL_PHONE" />

在程序中:Button but=(Button) findViewById(R.id.button);

          but.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

    EditText et=(EditText) findViewById(R.id.phoneText);

 Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"

                     +et.getText().toString()));

    Phone.this.startActivity(intent);

}

});


短信發送器

java代碼事件處理:

button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

     EditText phoneNoText=(EditText) findViewById(R.id.phoneNoText);

     EditText contentText=(EditText) findViewById(R.id.contentText);

     String phoneNo=phoneNoText.getText().toString();

     String content=contentText.getText().toString();

     SmsManager manager1=SmsManager.getDefault();

     List<String> cons=manager1.divideMessage(content);

 for(String con:cons){

manager1.sendTextMessage(phoneNo, null , con, nullnull); 

 }

}

});

功能清單文件權限聲明:

<uses-permission android:name="android.permission.SEND_SMS" />




---------------------- android培訓java培訓、期待與您交流! ----------------------

詳細請查看:http://edu.csdn.net/heima

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章