有没有办法获得联系短信计数和联系电话时间,Android是否仍然存储它们?
我认为有一些内容可以存储短信息,但是我不想阅读所有表格,知道为特定用户发送了多少短信,你知道其他方式吗?
答案 0 :(得分:0)
可能对您有所帮助。
void ReadContacts(String sort) {
String[] fields;
final Uri uri = ContactsContract.Contacts.CONTENT_URI;
final String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_URI
};
//boolean mShowInvisible = false;
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
String[] selectionArgs = null;
final String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
m_curContacts = cr.query(uri, projection, selection, selectionArgs, sortOrder);
String image = ContactsContract.Contacts.PHOTO_URI;
if(image!=null){
fields = new String[] {ContactsContract.Data.DISPLAY_NAME, ContactsContract.Contacts.PHOTO_URI};
}else{
fields = new String[] {ContactsContract.Data.DISPLAY_NAME, user_photo};
}
myadapter= new MySimpleCursorAdapter(this, R.layout.list_search, m_curContacts, fields, new int []{R.id.textView1,R.id.imageView1});
myadapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
String selection;
Log.d("hii", "runQuery constraint:"+constraint);
if(constraint.equals("")){
selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'" +
" AND "+ ContactsContract.Contacts.DISPLAY_NAME + " LIKE '%"+"&%#"+"%'";
}else{
selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'" +
" AND "+ ContactsContract.Contacts.DISPLAY_NAME + " LIKE '%"+constraint+"%'";
}
String[] selectionArgs = null;//new String[]{"'1'"};//, };
Cursor filter = cr.query(uri, projection, selection, selectionArgs, sortOrder);
return filter;
}
});
/*if you want to see all contact and then search*/
// listview.setAdapter(myadapter);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
long _id=0;
Cursor c = (Cursor) listview.getItemAtPosition(position);
String DisplayName = c.getString(c.getColumnIndexOrThrow( ContactsContract.Contacts.DISPLAY_NAME));
String id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
c.close();
// get the phone number
Cursor pCur = getApplicationContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
number = pCur.getString(pCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
pCur.close();
// get email and type
Cursor emailCur = getApplicationContext().getContentResolver().query( ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (emailCur.moveToNext()) {
email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
}
emailCur.close();
//getting image from contact
_id = Long.parseLong(id);
String _image=retrieveContactPhoto(_id);
retriveCallLog(number);
number= number.replaceAll( "[^\\d]", "");
int _call=in_call+out_call;
int _sms=in_sms+out_sms;
System.out.println(DisplayName+number+_call+_sms+email);
Intent search = new Intent(getApplicationContext(),Search_Add.class);
search.putExtra("_id", id);
search.putExtra("name", DisplayName);
search.putExtra("number", number.toString());
search.putExtra("image", _image);
search.putExtra("call", Integer.toString(_call));
search.putExtra("sms", Integer.toString(_sms));
search.putExtra("email", email);
startActivity(search);
overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
Toast.makeText(getApplicationContext(), "Selected Person:"+DisplayName,Toast.LENGTH_SHORT).show();
}
public String retrieveContactPhoto(long _id) {
Bitmap photo = null;
ImageView imageView = (ImageView) findViewById(R.id.Image_user);
try {
inputStream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(),
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(_id)));
if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG,100, baos);
byte [] b=baos.toByteArray();
user_photo=Base64.encodeToString(b, Base64.DEFAULT);
inputStream.close();
}else {
System.out.println("input stream is null");
BitmapDrawable bmd1 = (BitmapDrawable) getResources().getDrawable(R.drawable.user_pic);
Bitmap bm1 = bmd1.getBitmap();
ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
bm1.compress(Bitmap.CompressFormat.JPEG, 100, stream1);
byte[] bitmapdata1 = stream1.toByteArray();
user_photo = Base64.encodeToString(bitmapdata1, 0);
}
} catch (IOException e) {
e.printStackTrace();
}
return user_photo;
}
public void retriveCallLog(String number){
Database myDatabase=new Database(CustomGallary.this);
number= number.replaceAll( "[^\\d]", "");
Cursor cur=myDatabase.getFirstRecordValue(number);
System.out.println("retrive call log()"+cur.getCount());
if(cur.getCount()>0)
if(cur!=null)
{
cur.moveToFirst();
in_call=cur.getInt(cur.getColumnIndex(Database.RECEIVED_CALL));
out_call=cur.getInt(cur.getColumnIndex(Database.DIALED_CALL));
in_sms=cur.getInt(cur.getColumnIndex(Database.RECEIVED_SMS));
out_sms=cur.getInt(cur.getColumnIndex(Database.SENT_SMS));
int totalcall=in_call+out_call;
int totalsms=in_sms+out_sms;
System.out.println("incomincall:"+in_call+"outgoing:"+out_call);
cur.close();
}else{
Toast.makeText(getApplicationContext(), "NO Call and Sms Found", Toast.LENGTH_SHORT).show();
}
myDatabase.close();
}
});
// cur.close();
}