"1" The first Android app

Source: Internet
Author: User

The first program starts with Hello world??


Maybe C language learning starts from here, but it is not the beginning of the book, perhaps the starting point determines the height?


First, the new Android project


(1) Select Android Project



(2) Create a project with a name, default next



Second, the operating environment

(1) Create a virtual device and run the virtual Android emulator on your computer.


(2) running on their own mobile phone with a data cable connected to the computer, the developer mode, and then run the time will detect the phone, and then choose to run with a mobile phone.


I chose to put my own millet 3 as a test phone, because the computer's Android simulator debugging should be very card.


Third, running out is actually a Hello world.




Iv. Start learning the player function


Can you sing a ditty from the Internet?


Yes, you just need to modify the Mainactivity.java file.



(1) Import two header files, respectively, to represent the Android media and network header files

Import Android.media.*;import android.net.*;

(2) Define a variable for media playback

Private MediaPlayer MP;

(3) the function Playmusicfromweb (), which is written to play, is called in the OnCreate function.

<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);}}

This link is "Sir can't" music, do not be stunned OH: Http://img3.epanshi.com/2967/upload/1.mp3


Are you sure you wrote the correct procedure? You also need to add debugging information.

LOG.I () messages that record information classes

LOG.D () record debug messages

LOG.W () record the warning message

LOG.E () record the error message

And here the debug defines a variable called Debug_tag, debugging the

private static final String debug_tag= "Testlog";

The name is Testlog, which displays all the information about the variable when it is debugged.



(4) In order to be safe, define the method of stopping and releasing the player resources in the OnStop () function.

If you find that there is no onstop () function, it must be the wrong posture, right-click on the class name inside the. Java code, select Source and select the Override/implement Methods menu item, and then tick the OnStop () method.

protected void OnStop () {//TODO auto-generated method stubif (mp!=null) {mp.stop (); Mp.release ();} Super.onstop ();}


Error not playing out:

Without granting the program networking privileges, open the Androidmainfest.xml file by adding the following code.

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



V. Get the Location Code

Don't mention it, or pay attention to giving access to location permissions

Import android.location.*;

Get 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);}}







Six, so far, the simple first app came out, seems a bit shabby.

Attached: complete. Java code


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.*;p ublic class Mainactivity extends Actionbaractivity {private MediaPlayer mp;private static Final String debug_tag= "Testlog";//Play online music 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);}} Get location 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);        Start recording bug information log.i (Debug_tag, "Mydeuglog");        Play Music Playmusicfromweb ();                    Get position getLocation (); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Actio        n 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 would//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" The first Android app

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.