见下图,我想创建一个像这样的对话,我可以做设计部分,但我需要为对话格式的消息创建一个数据库
messaging.java
< private static String LIST_SEPARATOR =“!@#$%^& * 1234 __ / - |:.. :: | / __ * 4321& ^%$#@!”;
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);
}
}
}
}