我已經編寫了通過廣播接收器監聽電話事件的PhoneStateListener。我接收關於TelephonyManager.CALL_STATE_IDLE
代碼有什麼問題
06-10 13以下錯誤:50:48.360: VERBOSE/ERROR(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.CA LL_STATE_IDLE大小寫。請爲此問題提供任何解決方案。
堆棧跟蹤是
06-10 15:32:39.332: ERROR/AndroidRuntime(5559):致命 例外:主06-10 15:32:39.332: ERROR/AndroidRuntime(5559 ): java.lang.IllegalArgumentException異常: URI: 內容://com.android.contacts/phone_lookup/, 主叫用戶:com.emergency.alert, 調用包:com.emergency.alert 06-10 15 :32:39.332: ERROR/AndroidRuntime(5559):在 andr oid.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:144) 06-10 15:32:39.332: ERROR/AndroidRuntime(5559):在 android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114) 06 - 15:32:39.332: 錯誤/ AndroidRuntime(5559):在 在 android.content.ContentProviderProxy.query(ContentProviderNative.java:408) 06-10 15:32:39.332: ERROR/AndroidRuntime(5559):在 android.content.ContentResolver.query(ContentResolver.java:264) 06-10 15:32:39.332: ERROR/AndroidRuntime(5559):在 com.emergency.alert.MyPhoneStateListener.call_from_elist(MyPhoneStateListener.java:72) 06-10 15:32:39.332: ERROR/AndroidRuntime (5559):在 com.emergency.alert.MyPhoneStateListener.onCallStateChanged(MyPhoneStateListener.java:36) 06-10 15:32:39.332: ERROR/AndroidRuntime(5559):在 android.telephony.PhoneStateListener $ 2.handleMessage (PhoneStateListener.java:391) 06-10 15:32:39.332: ERROR/AndroidRuntime(5559):at 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: ERROR/AndroidRuntime(5559):在 android.app.ActivityThread.main(ActivityThread.java:4196) 06-10 15:32:39.332: 錯誤/ AndroidRuntime(5559):在 java.lang.reflect.Method.invokeNative(Native Method)06-10 15:32:39.332: 錯誤/ AndroidRuntime(5559):在 java.lang.reflect.Method.invoke(Method.java:507) 06-10 15:32:39。332: ERROR/AndroidRuntime(5559):在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839) 06-10 15:32:39.332: ERROR/AndroidRuntime(5559):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 06-10 15:32:39.332: ERROR/AndroidRuntime(5559):在 dalvik.system.NativeStart.main(母語 法)
嘗試將實際的異常添加到Log.v()調用中,因爲它會轉儲引發LogCat的異常的完整詳細信息:Log.v(「TAG」,「它被捕獲的位置」,e);然後提供發生故障時轉儲的堆棧跟蹤的LogCat。 – 2011-06-10 09:09:26
我已經發布堆棧跟蹤,請看看它,並讓我知道,如果你找出一些東西 – 2011-06-10 10:38:59
你的URI格式不正確 – Blundell 2011-06-10 10:39:57