【发布时间】:2011-09-12 08:22:21
【问题描述】:
我编写了 PhoneStateListener,它通过广播接收器监听电话事件。我在TelephonyManager.CALL_STATE_IDLE 收到以下错误
06-10 13:50:48.360: 详细/错误(4686):URI: 内容://com.android.contacts/phone_lookup/, 呼叫用户:com.emergency.alert, 调用包:com.emergency.alert
代码如下
public class MyPhoneStateListener extends PhoneStateListener {
public Context context;
//private static MediaPlayer mMediaPlayer;
private Uri alert;
private static Uri prev_ringtone;
public static final String PREFS_NAME = "ealertprefs";
public static int phone_state;
public EmergencyAdapter dbHelper;
private String fetchName;
@Override
public void onCallStateChanged(int state, String incomingNumber) {
// TODO Auto-generated method stub
switch(state){
case TelephonyManager.CALL_STATE_IDLE:
try {
if(call_from_elist(incomingNumber)) {
retrieve_phone();
}
}
catch(Exception e) {
Log.v("ERROR", e.getMessage());
}
Log.v("CALL", "IDLE");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//retrieve_phone();
break;
case TelephonyManager.CALL_STATE_RINGING:
if(call_from_elist(incomingNumber)) {
set_uri();
wake_up_phone();
send_notification();
}
break;
}
}
private void retrieve_phone() {
AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
am.setRingerMode(phone_state);
RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, prev_ringtone);
}
private boolean call_from_elist(String number) {
String[] projection = new String[] {
PhoneLookup._ID,
PhoneLookup.DISPLAY_NAME };
// encode the phone number and build the filter URI
Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
// query time
Cursor c = context.getContentResolver().query(contactUri, projection, null, null, null);
// if the query returns 1 or more results
// return the first result
Log.v("ELIST", "LOG");
if (c.moveToFirst()) {
Log.v("ELIST", "LOG2");
String _id = c.getString(c
.getColumnIndex(ContactsContract.Contacts._ID));
dbHelper = new EmergencyAdapter(context);
dbHelper.open();
Log.v("CALLER", _id);
Cursor cursor = dbHelper.fetchEntry_call(Long.parseLong(_id));
Log.v("CALLER", ""+cursor.getCount());
if(cursor.getCount() > 0) {
fetchName = cursor.getString(1);
Log.v("CALLER", fetchName);
return true;
}
else {
return false;
}
}
return false;
}
private void wake_up_phone() {
AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
phone_state = am.getRingerMode();
Log.v("WAKEUP", Integer.toString(phone_state));
RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, alert);
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
private void send_notification(){
NotificationManager notifier = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.icon;
Notification notification = new Notification(icon,"Simple Notification",System.currentTimeMillis());
Intent toLaunch = new Intent(context, main.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, toLaunch, 0);
notification.setLatestEventInfo(context, "Emergency Alert", "Emergency call received from "+fetchName, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notifier.notify(0x007, notification);
}
private void set_uri() {
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
String ringtone = settings.getString("call_uri", "");
if(ringtone.equals("")) {
this.alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
else {
this.alert = Uri.parse(ringtone);
}
prev_ringtone = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE);
}
}
此错误仅发生在 TelephonyManager.CALL_STATE_IDLE 情况下。请提供任何解决此问题的方法。
堆栈跟踪是
06-10 15:32:39.332: 错误/AndroidRuntime(5559):致命 例外:主要 06-10 15:32:39.332: 错误/Android 运行时(5559): java.lang.IllegalArgumentException: 网址: 内容://com.android.contacts/phone_lookup/, 呼叫用户:com.emergency.alert, 调用包:com.emergency.alert 06-10 15:32:39.332: 错误/AndroidRuntime(5559):在 android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:144) 06-10 15:32:39.332: 错误/AndroidRuntime(5559):在 android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114) 06-10 15:32:39.332: 错误/AndroidRuntime(5559):在 android.content.ContentProviderProxy.bulkQueryInternal(ContentProviderNative.java:372) 06-10 15:32:39.332: 错误/AndroidRuntime(5559):在 android.content.ContentProviderProxy.query(ContentProviderNative.java:408) 06-10 15:32:39.332: 错误/AndroidRuntime(5559):在 android.content.ContentResolver.query(ContentResolver.java:264) 06-10 15:32:39.332: 错误/AndroidRuntime(5559):在 com.emergency.alert.MyPhoneStateListener.call_from_elist(MyPhoneStateListener.java:72) 06-10 15:32:39.332: 错误/AndroidRuntime(5559):在 com.emergency.alert.MyPhoneStateListener.onCallStateChanged(MyPhoneStateListener.java:36) 06-10 15:32:39.332: 错误/AndroidRuntime(5559):在 android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:391) 06-10 15:32:39.332: 错误/AndroidRuntime(5559):在 android.os.Handler.dispatchMessage(Handler.java:99) 06-10 15:32:39.332: 错误/AndroidRuntime(5559):在 android.os.Looper.loop(Looper.java:143) 06-10 15:32:39.332: 错误/AndroidRuntime(5559):在 android.app.ActivityThread.main(ActivityThread.java:4196) 06-10 15:32:39.332: 错误/AndroidRuntime(5559):在 java.lang.reflect.Method.invokeNative(Native 方法) 06-10 15:32:39.332: 错误/AndroidRuntime(5559):在 java.lang.reflect.Method.invoke(Method.java:507) 06-10 15:32:39.332: 错误/AndroidRuntime(5559):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 06-10 15:32:39.332: 错误/AndroidRuntime(5559):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 06-10 15:32:39.332: 错误/AndroidRuntime(5559):在 dalvik.system.NativeStart.main(Native 方法)
【问题讨论】:
-
尝试将实际异常添加到 Log.v() 调用中,因为它会将异常的完整详细信息转储到 LogCat: Log.v( "TAG", "Where it was caught", e);然后提供发生故障时被转储的堆栈跟踪的 LogCat。
-
我已经发布了堆栈跟踪,请看一下,如果你发现了什么,请告诉我
标签: android