用户在Android中结束通话后如何获得当前正在进行的通话时间?

时间:2017-07-31 10:57:51

标签: android

我从我的应用程序调用。在通话结束后,我试图获得通话时间。但我的代码返回呼叫持续时间两次。结束通话后拨打电话的最后通话时间和新通话时间。但是在用户结束新呼叫后我只需要一个呼叫持续时间。我怎样才能做到这一点。这是我的代码。提前致谢。

public class MainActivity extends AppCompatActivity {

private static final int CALL_PERMISSION= 99;
private static final int READ_LOG_PERMISSION=11;
Button btnGetCallDuration;
Context mContext;
private int mFlag = 0;
String phNumber = "";
boolean flag=false;
String callDuration = "";
public static int REQ_CODE=1;
private boolean callPermission=false;
private boolean readlogPermission=false;
public static final int MULTIPLE_PERMISSIONS = 10; // code you want.

String[] permissions = new String[] {
        Manifest.permission.READ_CALL_LOG,
        Manifest.permission.CALL_PHONE,
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mContext = MainActivity.this;

    btnGetCallDuration = (Button) findViewById(R.id.btnGetCallDuration);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
       checkPermissions();
    }

    btnGetCallDuration.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

                    Log.v("onclick", "called");
                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:121"));
                    startActivity(intent);
                    //flag = true;
        }
    });
}

private boolean checkPermissions() {
    int result;
    List<String> listPermissionsNeeded = new ArrayList<>();
    for (String p:permissions) {
        result = ContextCompat.checkSelfPermission(mContext,p);
        if (result != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(p);
        }
    }
    if (!listPermissionsNeeded.isEmpty()) {
        ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new
                String[listPermissionsNeeded.size()]), MULTIPLE_PERMISSIONS);
        return false;
    }

    return true;
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Log.v("request code",requestCode+"\t"+resultCode);

    if(requestCode==REQ_CODE)
    {
        Log.v("request code",requestCode+"");
       // getCallDruation();
    }

}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MULTIPLE_PERMISSIONS: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // permissions granted.
                callPermission=true;
            } else {
                // no permissions granted.
                callPermission=false;
            }
            return;
        }
    }
}


@Override
protected void onPause() {
    super.onPause();
    Log.v("Pause is called","pause");
    flag=true;
}

public boolean checkLocationPermission() {
    if (ContextCompat.checkSelfPermission(mContext,
            Manifest.permission.CALL_PHONE)
            != PackageManager.PERMISSION_GRANTED) {

        // Asking user if explanation is needed
        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                Manifest.permission.CALL_PHONE)) {

            // Show an explanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

            //Prompt the user once explanation has been shown
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.CALL_PHONE},
                    CALL_PERMISSION);


        } else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.CALL_PHONE},
                    CALL_PERMISSION);
        }
        return false;
    } else {
        return true;
    }
}

public boolean readCallLogPermission() {
    if (ContextCompat.checkSelfPermission(mContext,
            Manifest.permission.READ_CALL_LOG)
            != PackageManager.PERMISSION_GRANTED) {

        // Asking user if explanation is needed
        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                Manifest.permission.READ_CALL_LOG)) {

            // Show an explanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

            //Prompt the user once explanation has been shown
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.READ_CALL_LOG},
                    READ_LOG_PERMISSION);


        } else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.CALL_PHONE},
                    READ_LOG_PERMISSION);
        }
        return false;
    } else {
        callPermission=true;
        return callPermission;
    }
}


@Override
protected void onResume() {
    super.onResume();
    Log.v("flag","onrsume called");

    if(flag)
    {
        getCallDruation();
        flag=false;
    }

}

private void getCallDruation() {
    StringBuffer sb = new StringBuffer();
    Uri contacts = CallLog.Calls.CONTENT_URI;

    Log.v("get duration","called");
    Cursor cur = getContentResolver().query(contacts, null, null, null, android.provider.CallLog.Calls.DATE + " DESC");

    int number = cur.getColumnIndex(CallLog.Calls.NUMBER);
    int duration = cur.getColumnIndex(CallLog.Calls.DURATION);
    sb.append("Call Details : \n");
    while (cur.moveToNext()) {
        phNumber = cur.getString(number);
        callDuration = cur.getString(duration);
        sb.append("\nPhone Number:" + phNumber+"\t"+callDuration);
        break;
    }
    cur.close();
    String str = sb.toString();
    Log.v("restult",str);
    Toast.makeText(mContext, phNumber + "\t\t" + callDuration, Toast.LENGTH_SHORT).show();

}

 }

以上代码是从onResume()生命周期活动方法调用的。

0 个答案:

没有答案