【1】第一個安卓程式

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   os   ar   java   sp   

第一個程式從 Hello World 開始?? 


也許C語言學習是從這開始的,但是書上不是這麼開始的,也許起點就決定了高度??


一、建立安卓項目


(1)選擇安卓項目



(2)隨便建個名字的項目,預設下一步



二、運行環境

(1)建立虛擬設備,在電腦上運行這個虛擬安卓模擬器。


(2)在自己的手機上運行,手機用資料線串連電腦,調為開發人員模式,然後啟動並執行時候就會檢測到手機,然後選擇用手機運行。


我選擇把自己的小米3作為了開發的測試手機,因為電腦的安卓模擬器調試應該會很卡。


三、運行出來其實就是個hello world..




四、開始學習播放器功能


能不能唱個來自網上的小曲??


可以,只需要修改MainActivity.java檔案就可以了



(1)匯入兩個標頭檔,分別表示安卓媒體和網路的標頭檔

import android.media.*;import android.net.*;

(2)定義一個媒體播放的變數

private MediaPlayer mp;

(3)寫入播放的函數 playMusicFromWeb(),這個函數的調用是在onCreate函數裡面。

<span style="white-space:pre"></span>public void playMusicFromWeb(){try {Uri file = Uri.parse("http://img3.epanshi.com/2967/upload/1.mp3");mp = MediaPlayer.create(this, file);mp.start();} catch (Exception e) {Log.e(Debug_tag, "Player failed", e);}}

這個連結是《客官不可以》音樂,不要驚呆哦:http://img3.epanshi.com/2967/upload/1.mp3


確定你寫的程式正確?還需要添加調試資訊。

Log.i() 記錄資訊類的訊息

Log.d() 記錄debug的訊息

Log.w() 記錄warning的訊息

Log.e() 記錄error的訊息

而這裡的調試定義了一個叫Debug_tag的變數,調試的

private static final String Debug_tag="testLog";

名字叫做testLog,會在調試的時候顯示這個變數的所有資訊。



(4)為了安全,在onStop()函數裡面定義停止以及釋放播放器資源的方法

如果發現沒有onStop()函數,一定是姿勢不對,在.java代碼裡面的類名上右擊滑鼠,選擇Source 再選擇 "Override/Implement Methods” 功能表項目,然後勾選onStop()方法。

protected void onStop() {// TODO Auto-generated method stubif(mp!=null){mp.stop();mp.release();}super.onStop();}


播放不出來的錯誤:

缺少了賦予程式連網許可權,開啟AndroidMainfest.xml檔案添加以下代碼就行。

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



五、擷取位置代碼

就不多提了,還是要注意賦予擷取位置許可權

import android.location.*;

擷取位置

<span style="white-space:pre"></span>public void getLocation(){try{LocationManager locMgr=(LocationManager)getSystemService(LOCATION_SERVICE);Location recentLoc=locMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);Log.i(Debug_tag, "loc:"+recentLoc.toString());}catch(Exception e){Log.e(Debug_tag,"get location failed!",e);}}







六、至此,簡單的第一個APP就出來了,好像有點簡陋。

附:完整.java代碼


package com.example.test;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.media.*;import android.net.*;import android.location.*;public class MainActivity extends ActionBarActivity {private MediaPlayer mp;private static final String Debug_tag="testLog";//播放網上的音樂public void playMusicFromWeb(){try {Uri file = Uri.parse("http://img3.epanshi.com/2967/upload/1.mp3");mp = MediaPlayer.create(this, file);mp.start();} catch (Exception e) {Log.e(Debug_tag, "Player failed", e);}}//擷取位置public void getLocation(){try{LocationManager locMgr=(LocationManager)getSystemService(LOCATION_SERVICE);Location recentLoc=locMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);Log.i(Debug_tag, "loc:"+recentLoc.toString());}catch(Exception e){Log.e(Debug_tag,"get location failed!",e);}}@Overrideprotected void onStop() {// TODO Auto-generated method stubif(mp!=null){mp.stop();mp.release();}super.onStop();}    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                //開始記錄bug資訊        Log.i(Debug_tag, "MydeugLog");        //播放音樂        playMusicFromWeb();        //擷取位置        getLocation();                    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}



【1】第一個安卓程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.