`
liufei.fir
  • 浏览: 676324 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Android

阅读更多
1、Android从网上下载文件 

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

<uses-permission android:name="android.permission.INTERNET"/><!--访问网络-->

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><!--访问sd卡-->

2、android实现多线程
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());
   
    }
    
  
}
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文件,新增


<receiver android:name="cn.edu.subclass.MyBroadCastReceiver">
		<intent-filter >
                         <!--action中的标签代表感兴趣的广播,此时我们对接收短信感兴趣-->
			<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
		</intent-filter>
	</receiver>
此时,如果系统收到短信,就会将广播信息传给该程序,该程序此时在那里处理这个广播呢,这时候我们就需要继承一个类,并重写其中的方法,如下

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方法



方法二:通过代码注册广播

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);
        
        MyBroadCastReceiver my=new MyBroadCastReceiver();
        IntentFilter filter=new IntentFilter();
        filter.addAction("android.provider.Telephony.SMS_RECEIVED");
        this.registerReceiver(my, filter);

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

}
方法一和方法二的区别是:方法一在程序关闭的时候,也会收到广播,执行其中的onReceive,方法二只会在程序开启的时候才会接收广播,执行onRecive方法,如果方法二种要移除该广播,可以调用unregisterReciver方法.

注意两种方法都要在AndroidManifest.xml中添加上如下配置信息:

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

3、Android中的ListView使用方法
package cn.edu.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleAdapter;

public class Mp3PlayerActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
      List<Map<String,String>> list=new ArrayList<Map<String,String>>();
      Map<String,String> map1=new HashMap<String,String>();
      map1.put("name", "Hello");
      map1.put("size", "1000");
      list.add(map1);
      
      SimpleAdapter simpleAdapter=new SimpleAdapter(this,list,R.layout.mylistview,new String []{"name","size"},new int [] {R.id.name,R.id.size});
      setListAdapter(simpleAdapter);
      
    }
}
<?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"
    />
  
   
    <ListView android:id="@id/android:list"  
            android:drawSelectorOnTop="false"  
            android:layout_width="fill_parent"   
            android:layout_height="wrap_content"   
            android:scrollbars="vertical"/>  
  
   
</LinearLayout>
<?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:id="@+id/name"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    <TextView  
    android:id="@+id/size"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />

   
</LinearLayout>
分享到:
评论

相关推荐

    Android组件设计思想

    Android应用开发的哲学是把一切都看作是组件。把应用程序组件化的好处是降低模块间的耦合性,同时提高模块的复用性。Android的组件设计思想与传统的组件设计思想最大的区别在于,前者不依赖于进程。也就是说,进程...

    新版Android开发教程.rar

    ----------------------------------- Android 编程基础 1 封面----------------------------------- Android 编程基础 2 开放手机联盟 --Open --Open --Open --Open Handset Handset Handset Handset Alliance ...

    Android框架揭秘.pdf

    《Android框架揭秘》通过对Android系统源代码的分析,主要介绍Android框架的初始化过程及主要组件的工作原理。作者直接分析和整理了Android框架的主要源代码,并详细讲解了理解框架工作原理所需的各种基础知识和构成...

    android应用开发范例精解

    第2篇为应用开发篇,通过实例介绍了Android UI布局、Android人机界面、手机硬件设备的使用、Android本地存储系统、Android中的数据库、多线程设计、Android传感器、Android游戏开发基础、Android与Internet,以及...

    Android BLE蓝牙例子(包括android版Lightblue)实例源码

    源码里面有Bluetooth4_3/BLEDemo/Android_Lightblue.apk三个.前两个是BLE的demo。BLEDemo这个功能较Bluetooth4_3多一些,有兴趣的可以都看下。Android_Lightblue.apk是Android版的lightblue,在进行ble开发的时候用...

    Android BLE开发之Android手机与BLE终端通信 程序文件

    Android 4.3才开始支持BLE API,所以请各位客官把本文代码运行在蓝牙4.0和Android 4.3及其以上的系统,另外本文所用的BLE终端是一个蓝牙4.0的串口蓝牙模块。 PS:我的i9100刷了4.4系统后,竟然也能跟BLE蓝牙模块通信...

    Android 8.0.0 (API 26) SDK Platform

    使用: export ANDROID_HOME="/opt/android-...# mv /opt/android-sdk/platforms/android-8.0.0 /opt/android-sdk/platforms/android-26 官网下载地址:https://dl.google.com/android/repository/platform-26_r01.zip

    Android 7.1.1 (API 25) SDK Platform 下载

    # mv /opt/android-sdk/platforms/android-25/android-7.1.1/* /opt/android-sdk/platforms/android-25/ # rm -rf /opt/android-sdk/platforms/android-25/android-7.1.1 官网下载地址:...

    Android support.v7包

    Android support.v7包

    android系统原理及开发要点详解

     第1章“Android系统概述”,概述Android系统方面的内容,包括智能手机平台的发展历程、Android系统的特点、Android的3种开发工作,以及Android的2种不同的开发方式。  第2章“Android系统开发综述”,介绍Android...

    android 经典代码例子

    android 经典代码例子,若干个。。。。

    c#开发Android应用实战

    《C#开发Android应用实战——使用Mono for Android和.NET/C# 全面透彻地讲解Android应用编程知识,分析如何结合使用C#和Mono来编写在Android设备系列上运行的应用程序。在这本由多位专家联袂撰写的必备精品书籍的...

    Android游戏源码大合集(主要是AndEngine和Libgdx的)

    最近在网络上看到有网友抱怨Android游戏源码找不到,所以小弟收集了一些AndEngine和Libgdx的游戏源码,以Eclipse工程的形式配置好环境,再陆续发出(某引擎避嫌,不在此列)。 虽说主要是由Libgdx与AndEngine开发的...

    Android平台开发之旅(第2版).pdf

    《Android平台开发之旅(第2版)》涵盖Android 3/4的新特性,立足实际的开发案例,介绍了Android平台开发的基础概念、实用技术和应用模式。主要内容包括应用程序框架、高级界面、数据库应用、网络通信与Web开发、无线...

    Android客户端调用webService上传图片到服务器

    调用服务器的webservice接口,实现从Android上传图片到服务器,然后从服务器下载图片到Android客户端 从Android端用io流读取到要上传的图片,用Base64编码成字节流的字符串,通过调用webservice把该字符串作为参数...

    Android应用程序输入事件处理机制

    在Android应用程序中,有一类特殊的消息,是专门负责与用户进行交互的,它们就是触摸屏和键盘等输入事件。触摸屏和键盘事件是统一由系统输入管理器InputManager进行分发的。也就是说,InputManager负责从硬件接收...

    《Google Android SDK开发范例大全(第3版)》.pdf

     全书共分11章,主要以范例集的方式来讲述android的知识点,详细介绍了开发android的人机交互界面、android常用的开发控件、android手机收发短信等通信服务、开发android手机的自动服务功能和娱乐多媒体功能以及...

    Android开发之旅 完整版pdf

    • Android 开发之旅:android架构 • Android 开发之旅:应用程序基础及组件 • Android 开发之旅:应用程序基础及组件(续) • Android 开发之旅:活动与任务 • Android 开发之旅:进程与线程 • Android ...

Global site tag (gtag.js) - Google Analytics