如何正确显示收到的短信发件人和日期?

时间:2013-10-15 19:52:46

标签: android sms android-contentresolver

我有以下代码:

Uri uriSMS = Uri.parse("content://sms");
Cursor cur = getContentResolver().query(uriSMS, 
        new String[] {"_id", "thread_id", "address", "person", "date", "body", "type"},
        null, null, null);
startManagingCursor(cur);

String[] from = new String[]{"address", "body", "date"};
int[] to = new int[]{R.id.sms_from, R.id.sms_body, R.id.sms_date};
adapter = new SimpleCursorAdapter(this, R.layout.sms_row, cur, from, to);

setListAdapter(adapter);

目前我使用address列显示短信发件人,因此会显示电话号码。我想用人名替换电话号码。我期望person包含它,但对于大多数SMS来说它是空的。 然后,date字段包含long值,当我想以DD.MM.YYYY格式显示它时。我想我可以覆盖setViewValue()来修复它。但是还有其他(更好的)解决方案&

我如何解决这两个问题?

3 个答案:

答案 0 :(得分:1)

要解决日期问题,您可以执行此操作

    DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    formatter.format( Long.parseLong( dateString ) );  

并且对于发件人姓名,请尝试此

    Cursor cs= context.getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},condition,null,null);  
    cs.getString(cs.getColumnIndex(PhoneLookup.DISPLAY_NAME));

答案 1 :(得分:0)

我认为person对于联系人中不存在的数字是空的。

日期:

String strDateTime = (String) DateFormat.format("yyyy-MM-dd kk:mm", new Date(longValue));

答案 2 :(得分:-1)

尝试使用此方法以短信显示正确的日期和时间。在此,您需要传递从光标

收到的日期
!Seyed Ahmad Abtahi
!1396/04/14

PROGRAM ROKNI_PERSIAN_GULF

REAL::BATH(60,104)

INTEGER::X,nobu,nobv,nobu_start,nobu_end,nobv_start,nobv_end

CHARACTER*60::FILE1,FILE2

CHARACTER(8)::STR

! Give the path to the input and output file names 

FILE1 = "BATH4.TXT"

FILE2 = "BATHYOUT.DAT"

! Number of Open Boundaries of Horizontal vector V

nobv_start = 24

nobv_end = 103

nobv = nobv_end - nobv_start + 1

! Number of Open Boundaries of Vertical vector U

nobu_start = 1

nobu_end = 5

nobu = nobu_end - nobu_start + 1

! Reading raw data from input file and change the simple bathymetry to scientific numebers

OPEN (1, FILE = FILE1, FORM = 'FORMATTED', STATUS = 'OLD')

DO I = 1 , 60

        READ(1,*)(BATH(I,J),J=1,103)

        WRITE(*,*)(BATH(I,J),J=1,103)

ENDDO

CLOSE (1)

! Writing the calculated bathymetry to the output file

OPEN ( 2 , FILE = FILE2 , FORM = 'FORMATTED', STATUS = 'REPLACE', ACTION = 'WRITE' , RECL = 12*60*103 )

WRITE ( 2 , '(104(1P,E15.7E2))')(( BATH(I,J) , J = 1 , 104 ) , I = 1 , 60 )                                

! Writing NOBU coordinations

WRITE ( 2 , '(A5,I3)')'nobu:',nobu

DO X = nobu_start , nobu_end

    WRITE ( STR , '(I4)') X

    WRITE ( 2,'(2(A4))') ' 103', STR

END DO

! Writing NOBV coordinations

WRITE ( 2 , '(A5,I3)')'nobv:',nobv

DO X = nobv_start , nobv_end

    WRITE ( STR , '(I4)') X

    WRITE ( 2,'(2(A4))') STR , '   1'

END DO         

END PROGRAM