Java 如何从android的inbox、sent、draft数据库中的消息创建对话
看到下面的图片,我想创建一个这样的对话,我可以做设计部分,但我需要创建一个数据库的消息是在对话格式 messaging.javaJava 如何从android的inbox、sent、draft数据库中的消息创建对话,java,android,sms,Java,Android,Sms,看到下面的图片,我想创建一个这样的对话,我可以做设计部分,但我需要创建一个数据库的消息是在对话格式 messaging.java 0){ while(cursor1.moveToNext()){ address=cursor1.getString(cursor1.getColumnIndex(columns[0])); 对于(i=0;i0){ while(cursor1.moveToNext()){ address=cursor1.getString(cursor1.getColumnIndex
public List phno=new ArrayList();
public List nam=new ArrayList();
public List message=new ArrayList();
公共列表时间=新建ArrayList();
public List type=new ArrayList();
public List lstMsg=new ArrayList();
public List lstime=new ArrayList();
public List lstType=new ArrayList();
public List tempMsg=new ArrayList();
public List testime=new ArrayList();
public List tempKind=new ArrayList();
@可空
@凌驾
公共IBinder onBind(意向){
返回null;
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
字符串地址;
String[]reqCols=新字符串[]{“\u id”,“thread\u id”,“address”,“person”,“date”,“body”,“type”};
Uri;
ContentResolver cr=getContentResolver();
字符串[]列=新字符串[]{“地址”、“人员”、“日期”、“正文”、“类型”};
游标游标1;
int i,c=0;
//获取电话号码
//收件箱
URI=URI.parse(“content://sms/inbox");
cursor1=cr.query(URI,reqCols,null,null,null);
if(cursor1.getCount()>0){
while(cursor1.moveToNext()){
address=cursor1.getString(cursor1.getColumnIndex(columns[0]));
对于(i=0;i0){
while(cursor1.moveToNext()){
address=cursor1.getString(cursor1.getColumnIndex(columns[0]));
对于(i=0;i0){
while(cursor1.moveToNext()){
address=cursor1.getString(cursor1.getColumnIndex(columns[0]));
对于(i=0;i0){
while(cursor1.moveToNext()){
address=cursor1.getString(cursor1.getColumnIndex(columns[0]));
if(地址等于(临时)){
testime.add(cursor1.getString(cursor1.getColumnIndex(columns[2]));//添加日期
tempMsg.add(cursor1.getString(cursor1.getColumnIndex(columns[3]));//添加消息
add(cursor1.getString(cursor1.getColumnIndex(columns[4]));
}
}
}
游标1.close();
URI=URI.parse(“content://sms/draft");
cursor1=cr.query(URI,reqCols,null,null,null);
if(cursor1.getCount()>0){
while(cursor1.moveToNext()){
address=cursor1.getString(cursor1.getColumnIndex(columns[0]));
if(地址等于(临时)){
testime.add(cursor1.getString(cursor1.getColumnIndex(columns[2]));//添加日期
tempMsg.add(cursor1.getString(cursor1.getColumnIndex(columns[3]));//添加消息
添加(cursor1.getString(cursor1.getColumnIndex(columns[4]));//添加类型
}
}
}
游标1.close();
排序();
添加(testime.get(testime.size()-1));
add(tempMsg.get(tempMsg.size()-1));
添加(tempKind.get(tempKind.size()-1));
添加(convertListToString(testime));
add(convertListToString(tempMsg));
type.add(convertListToString(tempKind));
}
最终排序();
返回开始时间;
}
@凌驾
公共空间{
super.ondestory();
}
//查找联系人姓名
公共静态字符串getContactName(上下文、字符串phoneNumber){
ContentResolver cr=context.getContentResolver();
Uri=Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT\u FILTER\u Uri,Uri.encode(phoneNumber));
Cursor Cursor=cr.query(uri,新字符串[]{ContactsContract.PhoneLookup.DISPLAY_NAME},null,null,null);
if(游标==null){
返回null;
}
字符串contactName=null;
if(cursor.moveToFirst()){
contactName=cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}
if(cursor!=null&!cursor.isClosed()){
public List<String> phno= new ArrayList<>();
public List<String> nam=new ArrayList<>();
public List<String> message=new ArrayList<>();
public List<String> time=new ArrayList<>();
public List<String> type=new ArrayList<>();
public List<String> lstMsg=new ArrayList<>();
public List<String> lstTime=new ArrayList<>();
public List<String> lstType=new ArrayList<>();
public List<String> tempMsg=new ArrayList<>();
public List<String> tempTime=new ArrayList<>();
public List<String> tempKind=new ArrayList<>();
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String address;
String[] reqCols = new String[]{"_id", "thread_id", "address", "person", "date", "body", "type"};
Uri URI;
ContentResolver cr = getContentResolver();
String[] columns = new String[]{"address", "person", "date", "body", "type"};
Cursor cursor1;
int i,c=0;
//gets phone numbers
//inbox
URI = Uri.parse("content://sms/inbox");
cursor1 = cr.query(URI, reqCols, null, null, null);
if (cursor1.getCount() > 0) {
while (cursor1.moveToNext()) {
address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
for (i = 0; i < phno.size(); i++){
if(address.equals(phno.get(i))) {
c++;
break;
}
}
if(c==0)
phno.add(address);
else
c=0;
}
}
cursor1.close();
//sentbox
URI = Uri.parse("content://sms/sent");
cursor1 = cr.query(URI,reqCols, null, null, null);
if (cursor1.getCount() > 0) {
while (cursor1.moveToNext()){
address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
for (i = 0; i < phno.size(); i++){
if(address.equals(phno.get(i))) {
c++;
break;
}
}
if(c==0)
phno.add(address);
else
c=0;
}
}
cursor1.close();
//Draft
URI = Uri.parse("content://sms/draft");
cursor1 = cr.query(URI,reqCols, null, null, null);
if (cursor1.getCount() > 0) {
while (cursor1.moveToNext()){
address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
for (i = 0; i < phno.size(); i++){
if(address.equals(phno.get(i))) {
c++;
break;
}
}
if(c==0)
phno.add(address);
else
c=0;
}
}
cursor1.close();
//gets contact name
for(i=0;i<phno.size();i++){
nam.add(getContactName(this,phno.get(i)));
}
for(String temp:phno){
URI = Uri.parse("content://sms/inbox");
cursor1 = cr.query(URI, reqCols, null, null, null);
if (cursor1.getCount() > 0) {
while (cursor1.moveToNext()) {
address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
if(address.equals(temp)) {
tempTime.add(cursor1.getString(cursor1.getColumnIndex(columns[2])));// adds date
tempMsg.add(cursor1.getString(cursor1.getColumnIndex(columns[3])));// adds message
tempKind.add(cursor1.getString(cursor1.getColumnIndex(columns[4])));
}
}
}
cursor1.close();
URI = Uri.parse("content://sms/sent");
cursor1 = cr.query(URI, reqCols, null, null, null);
if (cursor1.getCount() > 0) {
while (cursor1.moveToNext()) {
address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
if(address.equals(temp)) {
tempTime.add(cursor1.getString(cursor1.getColumnIndex(columns[2])));// adds date
tempMsg.add(cursor1.getString(cursor1.getColumnIndex(columns[3])));// adds message
tempKind.add(cursor1.getString(cursor1.getColumnIndex(columns[4])));
}
}
}
cursor1.close();
URI = Uri.parse("content://sms/draft");
cursor1 = cr.query(URI, reqCols, null, null, null);
if (cursor1.getCount() > 0) {
while (cursor1.moveToNext()) {
address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
if(address.equals(temp)) {
tempTime.add(cursor1.getString(cursor1.getColumnIndex(columns[2])));// adds date
tempMsg.add(cursor1.getString(cursor1.getColumnIndex(columns[3])));// adds message
tempKind.add(cursor1.getString(cursor1.getColumnIndex(columns[4])));// adds type
}
}
}
cursor1.close();
sort();
lstTime.add(tempTime.get(tempTime.size()-1));
lstMsg.add(tempMsg.get(tempMsg.size()-1));
lstType.add(tempKind.get(tempKind.size()-1));
time.add(convertListToString(tempTime));
message.add(convertListToString(tempMsg));
type.add(convertListToString(tempKind));
}
finalSort();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
//finds contact name
public static String getContactName(Context context, String phoneNumber) {
ContentResolver cr = context.getContentResolver();
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
if (cursor == null) {
return null;
}
String contactName = null;
if(cursor.moveToFirst()) {
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}
if(cursor != null && !cursor.isClosed()) {
cursor.close();
}
return contactName;
}
// Converts ArrayList to string
public static String convertListToString(List<String> stringList) {
StringBuffer stringBuffer = new StringBuffer();
for (String str : stringList) {
stringBuffer.append(str).append(LIST_SEPARATOR);
}
// Remove last separator
int lastIndex = stringBuffer.lastIndexOf(LIST_SEPARATOR);
stringBuffer.delete(lastIndex, lastIndex + LIST_SEPARATOR.length() + 1);
return stringBuffer.toString();
}
// Converts String to ArrayList
public static List<String> convertStringToList(String str) {
return Arrays.asList(str.split(LIST_SEPARATOR));
}
//Lists Sorting
public void sort(){
for(int j=0;j<tempTime.size();j++){
for(int i=j+1;i<tempTime.size();i++){
if((tempTime.get(i)).compareTo(tempTime.get(j))<0){
String t1 = tempTime.get(j);
String t2 = tempKind.get(j);
String t3 = tempMsg.get(j);
tempTime.set( j, tempTime.get(i));
tempTime.set(i,t1);
tempKind.set( j, tempKind.get(i));
tempKind.set(i,t2);
tempMsg.set( j, tempMsg.get(i));
tempMsg.set(i,t3);
}
}
}
}
//Final
//Lists Sorting
public void finalSort(){
for(int j=0;j<lstTime.size();j++){
for(int i=j+1;i<lstTime.size();i++){
if((lstTime.get(i)).compareTo(lstTime.get(j))<0){
String t1 = lstTime.get(j);
String t2 = lstMsg.get(j);
String t3 = lstType.get(j);
String t4 = time.get(j);
String t5 = message.get(j);
String t6 = type.get(j);
String t7 = phno.get(j);
String t8 = nam.get(j);
lstTime.set( j, lstTime.get(i));
lstTime.set(i,t1);
lstMsg.set( j, lstMsg.get(i));
lstMsg.set(i,t2);
lstType.set( j, lstType.get(i));
lstType.set(i,t3);
time.set( j, time.get(i));
time.set(i,t4);
message.set( j, message.get(i));
message.set(i,t5);
type.set( j, type.get(i));
type.set(i,t6);
phno.set( j, phno.get(i));
phno.set(i,t7);
nam.set( j, nam.get(i));
nam.set(i,t8);
}
}
}
}