短信表和联系人表中的数字格式

时间:2011-07-02 13:31:51

标签: android android-contentprovider

以下代码在活动开始时在Toast中给我一个0的计数。我不明白,因为它应该给我1个coz实际上在相应的联系人的收件箱中有一个短信,我想这是两个tablesi.ems表和联系人表之间的电话号码格式的问题。如何同步电话号码以便我能够比较它们。我试过phoneNumberUtils.format但无济于事。任何帮助: 代码:

package com.messageHider;

import java.util.ArrayList;
import java.util.Date;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.telephony.PhoneNumberUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class contactsmses extends Activity implements OnItemClickListener,OnClickListener{
    TextView textViewSource;
    ListView listViewContactSMS;
    Button buttonHide;
    SimpleCursorAdapter adapter;
    Handler handler=new Handler();
    Uri smsUri=Uri.parse("content://sms/inbox");
    Uri contentUri=ContactsContract.Contacts.CONTENT_URI;
    Uri phoneUri=ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    Cursor cursor=null,cursor2=null,cursor3=null,cursor_sms=null;
    ArrayList<String>names= new ArrayList<String>();
    ArrayAdapter<String> array_adapter;
    String sender;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.contactsmses);
        listViewContactSMS=(ListView)findViewById(R.id.listViewContactSMS);
        textViewSource=(TextView)findViewById(R.id.textViewsmsSource);
        buttonHide=(Button)findViewById(R.id.buttonHide);
        buttonHide.setOnClickListener(this);
        listViewContactSMS.setOnItemClickListener(this);
        super.onCreate(savedInstanceState);
    }
    @Override
    protected void onStart() {
        Intent intent=getIntent();
        sender=intent.getStringExtra("sender");
        textViewSource.setText("Messages from "+sender+":");
        SharedPreferences prefs=getSharedPreferences(sms.PREFERENCE_FILE,0);
        String sms_sender=prefs.getString("smssender","No such sender");
        cursor2=getContentResolver().query(contentUri, null, Contacts.DISPLAY_NAME+"=?",new String[]{sms_sender},null);
        cursor2.moveToFirst();
        String contact_id=cursor2.getString(cursor2.getColumnIndex(Contacts._ID));
        cursor3=getContentResolver().query(phoneUri, null, Phone.CONTACT_ID+"=?", new String[]{contact_id}, null);
        cursor3.moveToFirst();
        String number=cursor3.getString(cursor3.getColumnIndex(Phone.NUMBER));
        cursor_sms=getContentResolver().query(smsUri, null, "address=?", new String[]{number}, null);
        cursor_sms.moveToFirst();
        Toast.makeText(getApplicationContext(), String.valueOf(cursor_sms.getCount()), Toast.LENGTH_LONG).show();
        /*String[]from={"body"};
        int[]to={android.R.id.text1};
        adapter=new SimpleCursorAdapter(getApplicationContext(),android.R.layout.simple_list_item_multiple_choice, cursor_SMS, from, to);
        listViewContactSMS.setAdapter(adapter);
        listViewContactSMS.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);*/
        super.onStart();
    }

0 个答案:

没有答案