Android研究
Java代码
1、Android从网上下载文件
Java代码
URL url=null;
try {
url = new URL("http://192.168.1.100/Test/111.mp3");
HttpURLConnection urlConn = (HttpURLConnection) url
.openConnection();
BufferedInputStream bis = new BufferedInputStream(urlConn
.getInputStream());
FileOutputStream fos = new FileOutputStream(
Environment.getExternalStorageDirectory().getName()+
"\\music\\6.mp3");
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] buf = new byte[3 * 1024];
int result = bis.read(buf);
while (result != -1) {
bos.write(buf, 0, result);
result = bis.read(buf);
}
bos.flush();
bis.close();
fos.close();
bos.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Xml代码
<uses-permission android:name="android.permission.INTERNET"/><!--访问网络-->
Xml代码
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><!--访问sd卡-->
2、android实现多线程
Java代码
package cd.edu.app;
import android.app.Activity;
import android.os.Bundle;
import android.os.HandlerThread;
import android.os.Message;
import cn.edu.subclass.MyHandler;
public class AndroidThreadActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
HandlerThread handThread=new HandlerThread("app");
handThread.start();
MyHandler myHand=new MyHandler(handThread.getLooper());
Message msg=myHand.obtainMessage();
msg.sendToTarget();
System.out.println(Thread.currentThread().getName());
}
}
Java代码
package cn.edu.subclass;
import android.os.*;
public class MyHandler extends Handler{
public MyHandler(Looper loop){
super(loop);
}
public void handleMessage(Message message){
System.out.println(Thread.currentThread().getName());
}
}
3、Android中的广播机制
Android中之所以叫广播机制,其实就类似于我们平常现实生活中的广播,比如当android系统收到一个一条短信时,这时候就会向其他注册了广播并对此广播事件感兴趣的程序发出消息,当别的应用程序收到改广播时,就会对该事件进行一定的处理。
方法一,通过配置AndroidManifest.xml文件,新增
Java代码
<receiver android:name="cn.edu.subclass.MyBroadCastReceiver">
<intent-filter >
<!--action中的标签代表感兴趣的广播,此时我们对接收短信感兴趣-->
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
此时,如果系统收到短信,就会将广播信息传给该程序,该程序此时在那里处理这个广播呢,这时候我们就需要继承一个类,并重写其中的方法,如下
Java代码
package cn.edu.subclass;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyBroadCastReceiver extends BroadcastReceiver{
public MyBroadCastReceiver(){
System.out.println("constructed!");
}
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
System.out.println("OK");
}
}
当系统收到短信信后就会输出OK,也就是执行上面的onReceive方法
方法二:通过代码注册广播
Java代码
package cn.edu.design;
import android.app.Activity;
import android.content.IntentFilter;
import android.os.Bundle;
import cn.edu.subclass.MyBroadCastReceiver;
public class AndroidBroadCastActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
&nb
补充:移动开发 , Android ,