嗨,我在这个论坛上搜索了很多正确的结果,但找不到。通话结束后,我需要上次拨出电话的详细信息。为此,我正在使用 BroadcasteReceiver 这是我的接收器的代码
public class CallStateBroadcaster extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE)).listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);
}}
这是 PhoneStateListener 的代码
public class CustomPhoneStateListener extends PhoneStateListener{
private Context context;
public CustomPhoneStateListener(Context paramContext)
{
this.context = paramContext;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
// TODO Auto-generated method stub
super.onCallStateChanged(state, incomingNumber);
((TelephonyManager)this.context.getSystemService("phone")).listen(this, PhoneStateListener.LISTEN_NONE);
if(TelephonyManager.CALL_STATE_IDLE == state)
{
try {
Thread.sleep(1500L);
Intent intent = new Intent(this.context,LastCallInfoActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.context.startActivity(intent);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是我的活动的代码,用于获取通话记录的通话详细信息
public class LastCallInfoActivity extends Activity{
String addtolist;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Cursor callDetailCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null,null,null,android.provider.CallLog.Calls.DATE + " DESC limit 1");
int phoneNumber= callDetailCursor.getColumnIndex(CallLog.Calls.NUMBER);
int callType=callDetailCursor.getColumnIndex(CallLog.Calls.TYPE);
int callDate=callDetailCursor.getColumnIndex(CallLog.Calls.DATE);
int callDuration=callDetailCursor.getColumnIndex(CallLog.Calls.DURATION);
Log.i(">>CAllDetails", "getsCallLogs" );
if(callDetailCursor.getCount()>0)
{
while(callDetailCursor.moveToNext())
{
String phoneNumberString=callDetailCursor.getString(phoneNumber);
String contactName= getContactName(this, phoneNumberString);
String callTypeString =callDetailCursor.getString(callType);
String callDateString=callDetailCursor.getString(callDate);
String callDurationString=callDetailCursor.getString(callDuration);
Date callDayTime=new Date(Long.valueOf(callDateString));
int callCode = Integer.parseInt(callTypeString);
int calldur=Integer.parseInt(callDurationString);
if (callCode==2 && calldur>=1)
{
Double callCost=Double.parseDouble(callDurationString);
String callCostString= String.valueOf( callCost);
Log.i(">>CAllDetails", "getsLocation" );
addtolist= "Name :"+contactName+"\n"+
"Phone Number: "+phoneNumberString+"\n"+"Call Duration :"+
callDurationString+" Seconds\n"+"Call Date: "+callDayTime+"\n"+
callCostString;
}
}
}callDetailCursor.close();
Toast.makeText(this, addtolist, Toast.LENGTH_LONG).show();
}
public String getContactName(Context context, String phoneNumber) {
ContentResolver cr = context.getContentResolver();
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cursor = cr.query(uri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null);
if (cursor == null) {
return null;
}
String contactName = null;
if(cursor.moveToFirst()) {
contactName = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
if(cursor != null && !cursor.isClosed()) {
cursor.close();
}
return contactName;
}
}
甚至在我的电话断开并开始我的活动之前,我的这个程序就显示了敬酒。请帮助我并更正我的代码,以便在通话结束后执行它谢谢