我正在创建一个可以访问手机状态的应用程序,并且可以根据某些限制结束拨出电话。我能够使用:
实现这一目标if (!intent.getAction().equals("android.intent.action.PHONE_STATE"))
return;
else
{
String time = finalprefs.getString("timedelay","0");
System.out.println("TIME - "+time);
number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
WhitelistDAO whiteListDao = new WhitelistDAO(context);
List<Whitelist> whiteList = whiteListDao.getAllWhitelist();
if(number != null) {
number = lastten(number);
Log.e("CALLED NUMBER", "NUM - " + number);
Log.e("BLOCKED NUMBER", "BLLACK - " + new Whitelist(number).toString());
// Check, whether this is a member of "Black listed" phone numbers stored in the database
if (whiteList.contains(new Whitelist(number))) {
Log.e("Call","Allowed");
Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(),"This call is not allowed by your administration! Contact Karthik",Toast.LENGTH_SHORT).show();
disconnectPhoneItelephony(context);
return;
}
}
但是现在我想在连接10分钟后断开拨出电话!我搜索了这个并且我没有得到任何正确的解决方案!它的大部分内容都说明它无法在通话中获得通话时间!那么有没有解决方案?
请帮助!
答案 0 :(得分:0)
尝试此代码我还添加了通话记录的权限..
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_PERMISSIONS_REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (!checkPermissions()) {
requestPermissions();
} else {
getCallDetails();
}
}
private void getCallDetails() {
StringBuffer sb = new StringBuffer();
Uri contacts = CallLog.Calls.CONTENT_URI;
Cursor managedCursor = this.getContentResolver().query(contacts, null, null, null, null);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
sb.append("Call Details :");
while (managedCursor.moveToNext()) {
HashMap rowDataCall = new HashMap<String, String>();
String phNumber = managedCursor.getString(number);
String callType = managedCursor.getString(type);
String callDate = managedCursor.getString(date);
String callDayTime = new Date(Long.valueOf(callDate)).toString();
// long timestamp = convertDateToTimestamp(callDayTime);
String callDuration = managedCursor.getString(duration);
String dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode) {
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
}
sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + callDuration);
sb.append("\n----------------------------------");
}
managedCursor.close();
System.out.println(sb);
}
/**
* this method request to permission asked.
*/
private void requestPermissions() {
boolean shouldProvideRationale =
ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_CALL_LOG);
if (shouldProvideRationale) {
} else {
Log.i("Error", "Requesting permission");
// previously and checked "Never ask again".
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_CALL_LOG},
REQUEST_PERMISSIONS_REQUEST_CODE);
}
}
/**
* this method check permission and return current state of permission need.
*/
private boolean checkPermissions() {
int permissionState = ActivityCompat.checkSelfPermission(this,
Manifest.permission.READ_CALL_LOG);
return permissionState == PackageManager.PERMISSION_GRANTED;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSIONS_REQUEST_CODE) {
if (grantResults.length <= 0) {
// If user interaction was interrupted, the permission request is cancelled and you
// receive empty arrays.
Log.i("Error", "User interaction was cancelled.");
} else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission was granted. Kick off the process of building and connecting
// GoogleApiClient.
getCallDetails();
} else {
}
}
}
}
我希望你在android清单文件中添加以下权限..
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
答案 1 :(得分:0)
似乎您想要拨打电话并在10分钟后结束。
所以我建议启动一个计时器来计算10分钟的会话时间,然后使用处理程序,检查同一个呼叫是否还在继续:如果是,那么&gt;结束它
Java中的:
//call this method when call is starting
void startTimer() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {//this method will execute after 10 minutes
//check if call is still on going
//if so - write the logic to end call here
}
}, 1000 * 60 * 10); //10 min in milli-seconds
}
在Kotlin:
//call this method when call is starting
private fun startTimer() {
Handler().postDelayed({ //this method will execute after 10 minutes
//check if call is still on going
//if so - write the logic to end call here
}, 1000 * 60 * 10) //10 min in milli-seconds
}