Android初学模块代码


HttpClient进行网络通信----------------------------------
1:get方式
String httpUrl = "http://59.64.158.106:8080/test/xxx.jsp"; 
        //HttpGet对象  
        HttpGet httpGet = new HttpGet(httpUrl); 
        try{ 
            //取得HttpClient对象  
            HttpClient httpClient = new DefaultHttpClient(); 
            //请求HttpClient,取得HttpResponse  
            HttpResponse httpResponse = httpClient.execute(httpGet); 
            //请求成功  
            if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ 
                //取得返回的字符串  
                String strResult = EntityUtils.toString(httpResponse.getEntity()); 
                textView.setText(strResult); 
            }else{ 
                textView.setText("请求错误"); 
            } 

2:post方式
String httpUrl = "http://59.64.158.106:8080/test/httpGet.jsp"; 
        //httpPost连接对象  
        HttpPost httpPost = new HttpPost(httpUrl); 
        //使用NameValuePair来保存要传递的post阐述  
        List<NameValuePair> params = new ArrayList<NameValuePair>(); 
        //添加要传递的参数  
        params.add(new BasicNameValuePair("par", "HttpClient_android_post")); 
        try{ 
            //设置字符集  
            HttpEntity httpEntity = new UrlEncodedFormEntity(params, "gb2312"); 
            httpPost.setEntity(httpEntity); 
            //取得默认的HttpClient  
            HttpClient httpClient = new DefaultHttpClient(); 
            //取得HttpResponse  
            HttpResponse httpResponse = httpClient.execute(httpPost); 
            //HttpStatus.SC_OK)表示连接成功  
            if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ 
                //取得返回的字符串  
                String result = EntityUtils.toString(httpResponse.getEntity()); 
                textView.setText(result); 
            }else{ 
                textView.setText("请求错误"); 
            } 

 

 


android——activity生命周期------------------------

public class Activity extends ApplicationContext {     
   protected void onCreate(Bundle savedInstanceState);    
   protected void onStart();          
   protected void onRestart();       
   protected void onResume();       
   protected void onPause();        
   protected void onStop();      
   protected void onDestroy();  } 
 
注:1-- 我们打开应用时先后执行了onCreate()->onStart()->onResume三个方法
    2-- 当我们按BACK键时,我们这个应用程序将结束,这时候我们将先后调用onPause()->onStop()->onDestory()三个方法
    3-- 当我们打开应用程序时,需要切换任务,这时候我们会选择按HOME键,然后去其他应用程序,而当我们按HOME的时候,Activity先后执行了onPause()->onStop()这两个方法,可在onPause()里面做一些状态的记录,以便下次打开的时候保留上次的操作值
        而当我们再次启动ActivityDemo应用程序时,则先后分别执行了onRestart()->onStart()->onResume()三个方法
      、

 

 

 

页面停留几秒钟,然后自动跳转-------------------------

  Timer timer = new Timer();//timer中有一个线程,这个线程不断执行task
  TimerTask task = new TimerTask() { //timertask实现runnable接口,TimerTask类就代表一个在指定时间内执行的task
   @Override
   public void run() {
    Intent intent = new Intent(Hello.this, MainActivity.class);
    startActivity(intent);
    Hello.this.finish();
   }
  };
   timer.schedule(task, 1000 * 3);//设置这个task在延迟三秒之后自动执行

 

 

 

发送短信---------------------------
String body=”this is mms demo”;
           Intent mmsintent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(”smsto”, number, null));
           mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body);
           mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, true);
           mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, true);
            startActivity(mmsintent);
发送彩信
StringBuilder sb = new StringBuilder();
            sb.append(”file://”);
            sb.append(fd.getAbsoluteFile());
            Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(”mmsto”, number, null));
            // Below extra datas are all optional.
            intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_SUBJECT, subject);
            intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body);
            intent.putExtra(Messaging.KEY_ACTION_SENDTO_CONTENT_URI, sb.toString());
            intent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, composeMode);
            intent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, exitOnSent);
            startActivity(intent)

 

 

清空手机上Cookie--------------------------
CookieSyncManager.createInstance(getApplicationContext());
        CookieManager.getInstance().removeAllCookie();

 

 

 

按钮单击的图片暗淡变化 1:<ImageButton    。。。。      android:src="@drawable/file_np"  />

2:在drawable中定义一个file_np.xml文件,如下:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android" >    

<item android:drawable="@drawable/file_icon"  android:state_pressed="true"></item>

    <item android:drawable="@drawable/file_p" /> </selector>

 

 

SimpleAdapter SimpleAdapter adapter = new SimpleAdapter( this, getData(),         R.layout.monitor_record_listview, new String[] {LIST_TIME,   LIST_LOGSIZE, LIST_RESULT}, new int[] {R.id.time, R.id.logsize,  R.id.upresult}); listview.setAdapter(adapter);

注:getdata--List<Map<String,Object>>    

R.layout.xxx--当前listview中的每一行的布局,每一行可以有多个控件,向外提供id    

String[]--Map集合中的String值     int[]--R.layout.xxx中的对应的id

ArrayAdapter adapterData = new String[] {"第一行string","第二行string",..... };   

      ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(xxx.this,

android.R.layout.simple_list_item_1(默认每行的布局),adapterData);             

listView.setAdapter(arrayAdapter);

 

 

 

posted @ 2013-07-31 17:22  Blue-Dream  阅读(225)  评论(0)    收藏  举报