代码有什么问题

时间:2011-06-10 09:00:52

标签: android

我编写了PhoneStateListener,它通过广播接收器监听电话事件。我在TelephonyManager.CALL_STATE_IDLE

上收到以下错误消息
  

06-10 13:50:48.360:   VERBOSE / ERROR(4686):URI:   内容://com.android.contacts/phone_lookup/,   调用user: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案例中。请提供此问题的任何解决方案。

Stack Trace是

  

06-10 15:32:39.332:   错误/ AndroidRuntime(5559):致命   例外:主06-10 15:32:39.332:   ERROR / AndroidRuntime(5559):   java.lang.IllegalArgumentException异常:   URI:   内容://com.android.contacts/phone_lookup/,   调用user:com.emergency.alert,   调用包:com.emergency.alert   06-10 15:32:39.332:   错误/ AndroidRuntime(5559):at   android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:144)   06-10 15:32:39.332:   错误/ AndroidRuntime(5559):at   android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)   06-10 15:32:39.332:   错误/ AndroidRuntime(5559):at   android.content.ContentProviderProxy.bulkQueryInternal(ContentProviderNative.java:372)   06-10 15:32:39.332:   错误/ AndroidRuntime(5559):at   android.content.ContentProviderProxy.query(ContentProviderNative.java:408)   06-10 15:32:39.332:   错误/ AndroidRuntime(5559):at   android.content.ContentResolver.query(ContentResolver.java:264)   06-10 15:32:39.332:   错误/ AndroidRuntime(5559):at   com.emergency.alert.MyPhoneStateListener.call_from_elist(MyPhoneStateListener.java:72)   06-10 15:32:39.332:   错误/ AndroidRuntime(5559):at   com.emergency.alert.MyPhoneStateListener.onCallStateChanged(MyPhoneStateListener.java:36)   06-10 15:32:39.332:   错误/ AndroidRuntime(5559):at   android.telephony.PhoneStateListener $ 2.handleMessage(PhoneStateListener.java:391)   06-10 15:32:39.332:   错误/ AndroidRuntime(5559):at   android.os.Handler.dispatchMessage(Handler.java:99)   06-10 15:32:39.332:   错误/ AndroidRuntime(5559):at   android.os.Looper.loop(Looper.java:143)   06-10 15:32:39.332:   错误/ AndroidRuntime(5559):at   android.app.ActivityThread.main(ActivityThread.java:4196)   06-10 15:32:39.332:   错误/ AndroidRuntime(5559):at   java.lang.reflect.Method.invokeNative(母语   方法)06-10 15:32:39.332:   错误/ AndroidRuntime(5559):at   java.lang.reflect.Method.invoke(Method.java:507)   06-10 15:32:39.332:   错误/ AndroidRuntime(5559):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)   06-10 15:32:39.332:   错误/ AndroidRuntime(5559):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)   06-10 15:32:39.332:   错误/ AndroidRuntime(5559):at   dalvik.system.NativeStart.main(母语   方法)

2 个答案:

答案 0 :(得分:4)

我认为您的问题是,当您收到CALL_STATE_IDLE时,未设置incomingNumber变量,因为没有活动或振铃呼叫,因此没有与当前电话状态关联的电话号码。然后,您正在进行失败的联系人查找,因为您未在查询中包含有效的电话号码。

因此,当您实际上没有要搜索的电话号码时,您正试图查找联系人。

答案 1 :(得分:0)

啊我以前见过这个错误,

您的URI格式错误,因为您将其设置为默认铃声

 prev_ringtone = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE);

但是,默认铃声尚未设置,因此您将返回空字符串/ null或其他错误。

检查此问题并回答清楚:How to play ringtone/alarm sound in Android