阅读短信列表

时间:2019-02-19 12:22:29

标签: android json readfile writefile

这是我的Android代码,用于从手机中读取所有短信,并将每个短信放入JSONObject中。之后,我将JSONObject放在JSON文件中。当我运行代码时,我只会读一条短信。您如何帮助我阅读所有短信?

public void RecupSms() {

        JSONObject objSms = new JSONObject();

        try {
            FileOutputStream out = this.openFileOutput(nomDufichierSms, MODE_PRIVATE);


            Map<String, String> sms = new HashMap<String, String>();

            //ContentResolver contentResolver1 = getContentResolver();
             cursor3 = getContentResolver().query(SMS_URI_OUTBOX, null, null, null, null);
             totalSms  = cursor3.getColumnCount();


            if (cursor3.moveToFirst()) {

               // for(int i =0; i < totalSms; i++)
               do {
                    address = cursor3.getString(cursor3.getColumnIndex("address"));
                    body = cursor3.getString(cursor3.getColumnIndexOrThrow("body"));
                    //building.append("numero").append(address).append("message").append(body);

                    objSms.put("numero", address);
                    objSms.put("message", body);
                    //objSms.put("details sms", sms);


                } while (cursor3 != null && cursor3.moveToNext());


                infosSms = objSms.toString();
                out.write(infosSms.getBytes());

            } else{
                Toast.makeText(this, "0 sms", Toast.LENGTH_SHORT).show();
            }



            cursor3.close();

            out.close();


            Toast.makeText(this, "File saved!", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(this, "Error:" + e.getMessage(), Toast.LENGTH_SHORT).show();
        }


        try {
            // Open stream to read file.
            FileInputStream in = this.openFileInput(nomDufichierSms);

            BufferedReader br= new BufferedReader(new InputStreamReader(in));

            StringBuilder sb= new StringBuilder();
            String s= null;
            while((s= br.readLine())!= null)  {
                sb.append(s).append("\n");
            }
            detaile.setText(sb.toString());

        } catch (Exception e) {
            Toast.makeText(this,"Error:"+ e.getMessage(),Toast.LENGTH_SHORT).show();
        }
    }

这段代码的结果只给我一个短信,我不知道为什么

0 个答案:

没有答案