【Android】入門級串連網路樣本: 網頁瀏覽和播放網路MP3

來源:互聯網
上載者:User
<pre name="code" class="html">前提:使用可以連網的模擬器或者手機調試</pre><p>一,更改xml檔案<pre name="code" class="html"><?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="Hello World, MainActivity" /> <WebView android:id="@+id/myWebView1" android:layout_height="330px" android:layout_width="300px" android:layout_x="7px" android:layout_y="90px" android:focusable="false" /></LinearLayout></pre><p>二,更改AndroidManifest.xml檔案<?xml version="1.0" encoding="utf-8"?><br /><manifest xmlns:android="http://schemas.android.com/apk/res/android"<br /> package="tianshuai.webView"<br /> android:versionCode="1"<br /> android:versionName="1.0"><br /> <application android:label="@string/app_name" ><br /> <activity android:name="MainActivity"<br /> android:label="@string/app_name"><br /> <intent-filter><br /> <action android:name="android.intent.action.MAIN" /><br /> <category android:name="android.intent.category.LAUNCHER" /><br /> </intent-filter><br /> </activity><br /> </application><br /><uses-permission android:name="android.permission.INTERNET"/> --允許程式連網<br /></manifest> 三,更改java檔案


import android.app.Activity;<br />import android.media.MediaPlayer;<br />import android.os.Bundle;<br />import android.webkit.WebSettings;<br />import android.webkit.WebView;<br />import java.io.IOException;<br />import java.util.logging.Level;<br />import java.util.logging.Logger;</p><p>public class MainActivity extends Activity<br />{<br /> private WebView mWebView;<br /> private MediaPlayer mp;<br /> @Override<br /> public void onCreate(Bundle savedInstanceState)<br /> {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> mWebView = (WebView) findViewById(R.id.myWebView1);<br /> WebSettings webSettings = mWebView.getSettings();<br /> webSettings.setJavaScriptEnabled(true);<br /> mWebView.loadUrl("http://www.baidu.com"); </p><p> new Thread()<br /> {<br /> @Override<br /> public void run()<br /> {<br /> mp = new MediaPlayer();<br /> try {<br /> mp.setDataSource("http://fm111.img.xiaonei.com/tribe/20070613/10/52/A314269027058MUS.mp3 ");<br /> mp.prepare();<br /> } catch (IOException ex) {<br /> Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);<br /> } catch (IllegalArgumentException ex) {<br /> Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);<br /> } catch (IllegalStateException ex) {<br /> Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);<br /> }</p><p> mp.start();<br /> }<br /> }.start();</p><p> }<br />}



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.