开发者

number formats in sms table and contacts table

开发者 https://www.devze.com 2023-03-17 07:41 出处:网络
The following code gives me a count of 0 in the Toast when the activity starts. I dont understand since it should give me 1 coz there is actually a an sms in the inbox from the corresponding contact,

The following code gives me a count of 0 in the Toast when the activity starts. I dont understand since it should give me 1 coz there is actually a an sms in the inbox from the corresponding contact, I guess it is a matter of phone number format between the two tablesi.e sms table and contacts table. How do i sync the phone numbers so that I can be able to compare them. i have tried phoneNumberUtils.format but to no avail. Any help: Code:

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);
     开发者_如何学Go   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

精彩评论

暂无评论...
验证码 换一张
取 消