0

在这里,我正在使用 CallLogs.Call 并尝试使用从日志文件中获取通话历史记录contentResolver.query(),当我在模拟器上执行它时,它会显示记录,但在实际设备上运行时,Cursor 会给我零(0)条记录。我试图用实际设备调试它,但我不知道这里出了什么问题。帮助

    package com.example.billtrimmerfinal;

    import java.text.SimpleDateFormat;
    import java.util.Date;

    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.database.Cursor;
    import android.net.ParseException;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.Toast;


    public class callHistorySearch  extends Activity{

    private EditText txtStartDate;
    private EditText txtEndDate;
    private EditText txtNumber;
    private Button btnSearch;
    private RadioGroup radioLogType;
    private RadioButton radioSelected;
    private Date startDate;
    private Date endDate;
    private long Number;
    private String callType;
    private String callFrom;
    private long duration;
    private long DateOfCall;
    private StringBuilder data;
    int selectedId;
    private static String[] strFields = {
                    android.provider.CallLog.Calls.NUMBER, 
                    android.provider.CallLog.Calls.TYPE,
                    android.provider.CallLog.Calls.CACHED_NAME,
                    android.provider.CallLog.Calls.DURATION,
                    android.provider.CallLog.Calls.DATE,
                  };
    private Cursor cur=null;
    //private String[] strField={"NUMBER","TYPE","CACHED_NAME","DURATION,DATE"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.call_history_search1);
        btnSearch=(Button)findViewById(R.id.btnSearch);
        btnSearch.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                data=new StringBuilder("hello sonu");
                // TODO Auto-generated method stub
                radioLogType=(RadioGroup)findViewById(R.id.radioLogType);
                txtNumber=(EditText)findViewById(R.id.txtNumber);

                     selectedId=radioLogType.getCheckedRadioButtonId();//radioGroup
                radioSelected=(RadioButton)findViewById(selectedId);//radioButton
                int idx = radioLogType.indexOfChild(radioSelected);

                if(radioSelected==null)
                {
                    Toast.makeText(getApplicationContext(),"Please Select any of call types",5000).show();
                    return;
                }
                else if((txtNumber.getText().toString().trim().length()<0) ||(txtNumber.getText().equals("Number")))
                {
                    Toast.makeText(getApplicationContext(), "Please Select a Number", 5000).show();
                    return;
                }

                else
                {

                //Retrieving dates from the EditText
                txtStartDate=(EditText)findViewById(R.id.txtStartDate);
                txtEndDate=(EditText)findViewById(R.id.txtEndDate);
                SimpleDateFormat sdf=new SimpleDateFormat("dd/mm/yyyy");
                try
                {
                startDate=sdf.parse(txtStartDate.getText().toString());
                endDate=sdf.parse(txtEndDate.getText().toString());
                }

                catch (java.text.ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String strOrder = android.provider.CallLog.Calls.DATE + " DESC"; 
                String condition="TYPE=? AND NUMBER=? AND DATE >= ? "; 
                try{
                 cur = getContentResolver().query(
                        android.provider.CallLog.Calls.CONTENT_URI,
                        null,
                        condition,
                        new String[]{Integer.toString(idx+1),txtNumber.getText().toString(),Long.toString(startDate.getTime())},
                        strOrder
                        );
                int count=cur.getCount();
                Toast.makeText(getApplicationContext(),Long.toString(count),5000).show();
                while(cur.moveToNext())
                {   

                    Number=Long.parseLong(cur.getString(cur.getColumnIndex(strFields[0])));
                    Toast.makeText(getApplicationContext(),Long.toString(Number),5000).show();
                    callFrom=cur.getString(cur.getColumnIndex(strFields[2]));
                    callType=cur.getString(cur.getColumnIndex(strFields[1]));
                    duration=Long.parseLong(cur.getString(cur.getColumnIndex(strFields[3])));
                    DateOfCall=Long.parseLong(cur.getString(cur.getColumnIndex(strFields[4])));

                    int sec=(int)(duration);
                        int min=(int)(sec/60);
                         sec=sec%60;
                         int hour=min/60;
                         min=min%60;


                        data.append("\n"+"Number:-->"+Number+"\n");
                        data.append("Call From:-->"+ callFrom +"\n");
                        data.append("Call Type:-->Incoming"+"\n");
                        data.append("Call Duration:-->"+hour+" hr:"+min+" min:"+sec+" sec"+"\n");
                        data.append("Call Date:-->"+new Date(DateOfCall).toString()+"\n");
                        data.append("----------------------------------"+"\n\n");

                        }
                }
                catch(Exception ee)
                {
                    ee.printStackTrace();
                }

                //Toast.makeText(callHistorySearch.this, data.toString(),5000).show();

                Intent intent=new Intent(callHistorySearch.this,showCallRecord.class);
                intent.putExtra("data", data.toString());
                startActivity(intent);
                }





        }});
    }

}
4

0 回答 0