Android一般カラーメッセージリストdemoコアソースを参照

10969 ワード

Activityコンテンツ:
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * @author mpqi  2012 
 */
@SuppressWarnings("deprecation")
public class SmsPage extends Activity{
    private final String TAG="SmsPage"; 
    
    private final Uri CONTENT_URI = Uri.parse("content://mms/inbox"); //       
    
    private final Uri CONTENT_URI_PART = Uri.parse("content://mms/part"); //     
    
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.content1);
		
	    Cursor cursor = getContentResolver().query(CONTENT_URI, null, null,null, null);
	    
	    String name = "";
	    while (cursor.moveToNext()) {
	    	
	    	LinearLayout view = (LinearLayout) View.inflate(this, R.layout.smsitem_mms, null);
	    	
            TextView address = (TextView)view.findViewById(R.id.sms_address);
            TextView body = (TextView)view.findViewById(R.id.sms_body);
            TextView date = (TextView)view.findViewById(R.id.sms_date);
            TextView sub = (TextView)view.findViewById(R.id.sms_sub);
            ImageView image = (ImageView)view.findViewById(R.id.sms_image);

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date time=new Date(cursor.getLong(cursor.getColumnIndex("date"))*1000);//    
            int id = cursor.getInt(cursor.getColumnIndex("_id"));//  Id
            String subject = cursor.getString(cursor.getColumnIndex("sub"));//    
            Cursor cAdd =null;
            Cursor cPhones=null;
            Cursor cPeople=null;
            Cursor cPart=null;
            try {
                //    id addr           ,    msg_id  pdu  id;
                String selectionAdd = new String("msg_id=" + id );
                Uri uriAddr = Uri.parse("content://mms/" + id + "/addr");
                cAdd = getContentResolver().query(uriAddr, null, selectionAdd, null, null);
                
                //  addr        phones    PERSON_ID,  PERSON_ID  people   _id
                if(cAdd.moveToFirst()){//     2   ,           ,        
                    String number= cAdd.getString(cAdd.getColumnIndex("address"));
                    cPhones = getContentResolver().query(Contacts.Phones.CONTENT_URI, new String[]{Contacts.Phones.PERSON_ID},Contacts.Phones.NUMBER +"=?",new String[]{number}, null);
                    if(cPhones.getCount()>0){//  phones   PERSON_ID   people         
                        while (cPhones.moveToNext()) {
                            String pId = cPhones.getString(cPhones.getColumnIndex(Contacts.Phones.PERSON_ID));
                            Uri uriPeo = Uri.parse(Contacts.People.CONTENT_URI+"/"+pId);
                            cPeople = getContentResolver().query(uriPeo, null,null,null, null);
                            if(cPeople.getCount()>0){
                                String str="";
                                while (cPeople.moveToNext()) {
                                    if(str == ""){
                                        str = cPeople.getString(cPeople.getColumnIndex(Contacts.People.DISPLAY_NAME));
                                    }else{
                                        str += ","+cPeople.getString(cPeople.getColumnIndex(Contacts.People.DISPLAY_NAME));
                                    }
                                }
                                name=number+"/"+str;//        ,   ‘    /  ’     
                            }else{
                                name=number;//              
                            }
                        }
                    }else{
                        name=number;//              
                    }    
                }
                
                //    ID       
                String selectionPart = new String("mid="+id);//part   mid   pdu   _id
                cPart = getContentResolver().query(CONTENT_URI_PART, null,selectionPart,null, null);            
                String bodyStr="";
                String[] coloumns = null; 
                String[] values = null; 
                while(cPart.moveToNext()){ 
                    coloumns = cPart.getColumnNames(); 
                    if(values == null) 
                        values = new String[coloumns.length]; 
                    for(int i=0; i< cPart.getColumnCount(); i++){ 
                        values[i] = cPart.getString(i); 
                    } 
                    if(values[3].equals("image/jpeg") || values[3].equals("image/bmp") || values[3].equals("image/gif") || values[3].equals("image/jpg") || values[3].equals("image/png")){  //      
                        image.setImageBitmap(getMmsImage(values[0]));//          ,                  ImageView  Gallery,      
                        image.setVisibility(View.VISIBLE);
                    }else if(values[3].equals("text/plain")){
                        /**        
                        *        ,    2.1           ,   1.6 G2   
                        *      ,1.6  part       "text" ,   values[13],   
                        *   ,   1.6  (    G2,  ),               
                        *   _date     values[12]  ,     ,  2.1        
                        * ,        "text"     , "_date" null*/

                        if(values[12]!=null){//         ,  _date null,        "text"
                            bodyStr=getMmsText(values[0]);
                        }else{
                            bodyStr = values[13];                        	
                        }
                    }
                }
                if(!"".equals(subject) && subject != null){
                    try {
                        sub.setText(new String(subject.getBytes("iso-8859-1"),"UTF-8"));//           
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
                if(!"".equals(bodyStr)){
                    body.setText(bodyStr);
                }
                address.setText(name); 
                date.setText(format.format(time));
            } catch (RuntimeException e) {
                Log.e(TAG, e.getMessage());
            }finally{
                if(cAdd != null){
                    cAdd.close();
                }
                if(cPart != null){
                    cPart.close();
                }
                if(cPeople != null){
                    cPeople.close();
                }
                if(cPhones != null){
                    cPhones.close();
                }
            }
           
            linearLayout.addView(view);
	    	
	    }
	    
	    
    }
    
    
    private String getMmsText(String _id){ //      
        Uri partURI = Uri.parse("content://mms/part/" + _id ); 
        InputStream is = null; 
        StringBuilder sb = new StringBuilder();
        try { 
            is = getContentResolver().openInputStream(partURI); 
            if(is!=null){
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
                String temp = reader.readLine();
                while (temp != null) {
                    sb.append(temp);
                    temp=reader.readLine();//        InputStream  string   ,        ,           ,                  
                }
            }
        }catch (IOException e) { 
            e.printStackTrace();  
            Log.v(TAG, "      "+e.getMessage());
        }finally{ 
            if (is != null){ 
                try { 
                    is.close(); 
                }catch (IOException e){
                    Log.v(TAG, "      "+e.getMessage());
                }
            } 
        }
        return sb.toString();
    }
    
    private Bitmap getMmsImage(String _id){ //      
        Uri partURI = Uri.parse("content://mms/part/" + _id ); 
        //ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
        InputStream is = null; 
        Bitmap bitmap=null;
        try { 
            is = getContentResolver().openInputStream(partURI); 
            //byte[] buffer = new byte[256];  
            //int len = -1;
            //while ((len = is.read(buffer)) != -1) {
            //    baos.write(buffer, 0, len);
            //}
            //bitmap = BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.toByteArray().length);
            bitmap = BitmapFactory.decodeStream(is);
        }catch (IOException e) { 
            e.printStackTrace();  
            Log.v(TAG, "      "+e.getMessage());
        }finally{ 
            if (is != null){ 
                try { 
                    is.close(); 
                }catch (IOException e){
                    Log.v(TAG, "      "+e.getMessage());
                }
            } 
        }
        return bitmap;
    }
}


 
 
xmlレイアウトファイルは添付ファイルを参照してくださいね~^