【发布时间】:2017-06-22 05:00:59
【问题描述】:
我已在manifest.xml文件中授予权限
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
并定义一个接收者:
<receiver android:name=".OutgoingCallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
接收方文件为:
public class OutgoingCallReceiver extends BroadcastReceiver {
public static String TAG = "AUTODIAL";
static long start_time, end_time, call_duration;
String number;
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "intent get data : "+intent.getData(), Toast.LENGTH_LONG).show();
String action = intent.getAction();
String state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);
Toast.makeText(context, "Phone State : "+state, Toast.LENGTH_LONG).show();
if(state==null) {
number=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(context, "Out going Phone number : "+number, Toast.LENGTH_LONG).show();
}
if (action.equalsIgnoreCase("android.intent.action.PHONE_STATE")) {
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
start_time = System.currentTimeMillis();
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
end_time = System.currentTimeMillis();
}
call_duration = end_time - start_time;
}
}
在这里,它给出了所有通话时长,而不仅仅是我从我的应用程序拨打的通话时长。
从我的应用拨号后是否可以返回?
【问题讨论】:
-
这可能会有所帮助.. stackoverflow.com/questions/19493360/…
-
这里使用了 getContentResolver,但是当我通过 Intent 从应用程序拨号时 callIntent = new Intent(Intent.ACTION_CALL); callIntent.setData(Uri.parse("tel:"+phone_number)); callIntent.putExtra("flag",1); context.startActivity(callIntent);所以我需要在 onReceive() 里面计算
标签: android phone-call android-phone-call