我想获取最近拨打的电话的详细信息,即最后一个电话号码,日期,时间,持续时间等。我已经编写了代码,但第一次我的应用程序崩溃,下次显示第二次拨打电话的详细信息。
private class PhoneCallListener extends PhoneStateListener {
private boolean isPhoneCalling = false;
String LOG_TAG = "Test";
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (TelephonyManager.CALL_STATE_RINGING == state) {
Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
Log.i(LOG_TAG, "OFFHOOK");
isPhoneCalling = true;
Log.i("Test", "OFFHOOK" + isPhoneCalling );
}
if (TelephonyManager.CALL_STATE_IDLE == state) {
// run when class initial and phone call ended, need detect flag from CALL_STATE_OFFHOOK
Log.i(LOG_TAG, "IDLE");
Log.i("Test", "IDLE" + isPhoneCalling);
if (isPhoneCalling) {
Log.i(LOG_TAG, "restart app");
isPhoneCalling = false;
Cursor c = ContactMainActivity.this.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, CallLog.Calls.DATE + " DESC");
LayoutInflater layoutInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.toast_layout, null);
final String name;
final String number;
String duration, date;
if (c != null) {
if (c.moveToPosition(0)) {
name= c.getString(c.getColumnIndex(Calls.CACHED_NAME));
number= c.getString(c.getColumnIndex(Calls.NUMBER));
duration= c.getString(c.getColumnIndex(Calls.DURATION));
Date date1= new Date(Long.valueOf(c.getString(c.getColumnIndex(Calls.DATE))));
String Temp=date1.toString();
}
}
我在一个对话框中显示所有信息,以便在该对话框中显示。它显示错误但只是第一次
logcat的:
07-05 14:45:34.601: E/AndroidRuntime(11094): FATAL EXCEPTION: main
07-05 14:45:34.601: E/AndroidRuntime(11094): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@405330b0 is not valid; is your activity running?
07-05 14:45:34.601: E/AndroidRuntime(11094): at android.view.ViewRoot.setView(ViewRoot.java:532)
07-05 14:45:34.601: E/AndroidRuntime(11094): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
07-05 14:45:34.601: E/AndroidRuntime(11094): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
07-05 14:45:34.601: E/AndroidRuntime(11094): at android.view.Window$LocalWindowManager.addView(Window.java:424)
07-05 14:45:34.601: E/AndroidRuntime(11094): at android.app.Dialog.show(Dialog.java:241)
07-05 14:45:34.601: E/AndroidRuntime(11094): at com.example.crmbolt.ContactMainActivity$PhoneCallListener.onCallStateChanged(ContactMainActivity.java:1167)
07-05 14:45:34.601: E/AndroidRuntime(11094): at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:319)
07-05 14:45:34.601: E/AndroidRuntime(11094): at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 14:45:34.601: E/AndroidRuntime(11094): at android.os.Looper.loop(Looper.java:130)
07-05 14:45:34.601: E/AndroidRuntime(11094): at android.app.ActivityThread.main(ActivityThread.java:3687)
07-05 14:45:34.601: E/AndroidRuntime(11094): at java.lang.reflect.Method.invokeNative(Native Method)
07-05 14:45:34.601: E/AndroidRuntime(11094): at java.lang.reflect.Method.invoke(Method.java:507)
07-05 14:45:34.601: E/AndroidRuntime(11094): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
07-05 14:45:34.601: E/AndroidRuntime(11094): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
07-05 14:45:34.601: E/AndroidRuntime(11094): at dalvik.system.NativeStart.main(Native Method)