第三方聊天应用

时间:2018-06-27 11:29:08

标签: java android

我创建了一个Android聊天应用程序,该应用程序使用getContentResolver()。applyBatch方法加载了聊天记录。当我将应用程序设置为默认应用程序时,它会在发送新消息时同时加载聊天记录,但是当我拒绝将应用程序设置为默认应用程序时,我们必须返回上一个屏幕,而当再次返回时,它将加载最后一条消息。

ArrayList<ThreadModel> list;

public ThreadService() {
    super("ThreadService");
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected void onHandleIntent(Intent intent) {
    Log.d("testing" , "ThreadService: onHandleIntent()");

    String threadId = intent.getStringExtra("ThreadId");
    list = new ArrayList<>();
    if (!TextUtils.isEmpty(threadId))
        bulkInsert(list, threadId);
}

public void bulkInsert(ArrayList<ThreadModel> arrayList, String threadId) {
    Log.d("testing" , "TreadService: bulk insert");

    putSmsIntoDb(threadId);
    putMmsIntoDb(threadId);
    ArrayList<ContentProviderOperation> operations = new ArrayList<>();

    if (operations.size() > 0)
        operations.clear();

    if (arrayList != null && !arrayList.isEmpty()) {
        for (int i = 0; i < arrayList.size(); i++) {
            operations.add(ContentProviderOperation.newInsert(ItemsContract.Thread.CONTENT_URI)
                    .withValue(ItemsContract.Thread.SMS_ID, arrayList.get(i).getSms_id())
                    .withValue(ItemsContract.Thread.ADDRESS, arrayList.get(i).getAddress())
                    .withValue(ItemsContract.Thread.SMS_TEXT_BODY, arrayList.get(i).getSms_text_body())
                    .withValue(ItemsContract.Thread.SMS_IS_READ, arrayList.get(i).getSms_read())
                    .withValue(ItemsContract.Thread.DATE, arrayList.get(i).getDate())
                    .withValue(ItemsContract.Thread.SMS_TYPE, arrayList.get(i).getSms_type())
                    .withValue(ItemsContract.Thread.THREAD_ID, arrayList.get(i).getThread_id())
                    .withValue(ItemsContract.Thread.MESSAGE_TYPE, arrayList.get(i).getMessage_type())
                    .withValue(ItemsContract.Thread.MMS_IMAGE_URI, arrayList.get(i).getMms_image_uri())
                    .withValue(ItemsContract.Thread.MMS_AUDIO_URI, arrayList.get(i).getMms_audio_uri())
                    .withValue(ItemsContract.Thread.MMS_ID, arrayList.get(i).getMms_id())
                    .withValue(ItemsContract.Thread.MMS_TEXT_BODY, arrayList.get(i).getMms_text_body())
                    .withValue(ItemsContract.Thread.MMS_IS_READ, arrayList.get(i).getMms_read())
                    .withValue(ItemsContract.Thread.MMS_TYPE, arrayList.get(i).getMms_type())
                    .build()
            );
        }
        try {
            Log.d("testing" , "TreadService: applyBatch");

           this.getContentResolver().applyBatch(ItemsContract.AUTHORITY, operations);


            } catch (Exception e) {
            Log.d("testing" , "TreadService: exception " + e.toString());

            e.printStackTrace();
        }
    }
}


public void putSmsIntoDb(String threadId) {
    System.out.println("==============================ScanSMS()==============================");
    //Initialize Box
    Log.d("testing" , "TreadService: putSmsIntoDb");

    Uri uri = Uri.parse("content://sms");
    String[] proj = {"*"};
    ContentResolver cr = getContentResolver();
    String where = "thread_id =" + threadId;
    Cursor c = cr.query(uri, null, where, null, "date asc");

    if (c != null && c.moveToFirst()) {
        do {
            String[] col = c.getColumnNames();
            String str = "";
            for (int i = 0; i < col.length; i++) {
                str = str + col[i] + ": " + c.getString(i) + ", ";
            }
            //System.out.println(str);

            System.out.println("--------------------SMS------------------");

            int smsId = Integer.valueOf(c.getString(c.getColumnIndex("_id")));
            String address = c.getString(c.getColumnIndex("address"));
            String smsTextBody = c.getString(c.getColumnIndex("body"));
            int read = Integer.valueOf(c.getString(c.getColumnIndex("read")));
            long date = c.getLong(c.getColumnIndex("date"));
            int type = Integer.valueOf(c.getString(c.getColumnIndex("type")));
            String thread_Id = c.getString(c.getColumnIndex("thread_id"));
            String msgType = "SMS";
            ThreadModel threadModel = new ThreadModel(smsId, address, smsTextBody, read, date
                    , type, thread_Id, msgType, null, null, -1, null, 1, -1);
            list.add(threadModel);
        } while (c.moveToNext());
    }
    if (c != null) {
        c.close();
    }
}

0 个答案:

没有答案