开发者

What is wrong with the code

开发者 https://www.devze.com 2023-03-12 11:51 出处:网络
I have written PhoneStateListener which listens for telephony events through broadcast receiver. I am receiving following error on TelephonyManager.CALL_STATE_IDLE

I have written PhoneStateListener which listens for telephony events through broadcast receiver. I am receiving following error on TelephonyManager.CALL_STATE_IDLE

06-10 13:50:48.360: VERBOSE/ERROR(4686): URI: content://com.android.contacts/phone_lookup/, calling user: com.emergency.alert, calling package:com.emergency.alert

Code is Listed below

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);
    }
}

This error only occurs on TelephonyManager.CALL_STATE_IDLE case. Please provide any solution to this problem.

Stack Trace is

06-10 15:32:39.332: ERROR/AndroidRuntime(5559): FATAL EXCEPTION: main 06-10 15:32:39.332: ERROR/AndroidRuntime(5559): java.lang.IllegalArgumentException: URI: content://com.android.contacts/phone_lookup/, calling user: com.emergency.alert, calling package:com.emergency.alert 06-10 15:32:39.332: ERROR/AndroidRuntime(5559): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:144) 06-10 15:32:39.332: ERROR/AndroidRuntime(5559): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114) 06-10 15:32:39.332: ERROR/AndroidRuntime(5559): at android.content.ContentProviderProxy.bulkQueryInternal(ContentProviderNative.java:372) 06-10 15:32:39.332: ERROR/AndroidRuntime(5559): at android.content.ContentProviderProxy.query(ContentProviderNative.java:408) 06-10 15:32:39.332: ERROR/AndroidRuntime(5559): at android.content.ContentResolver.query(ContentResolver.java:264) 06-10 15:32:39.332: ERROR/AndroidRuntime(5559): at com.emergency.alert.MyPhoneStateListener.call_from_elist(MyPhoneStateListener.java:72) 06-10 15:32:39.332: ERROR/AndroidRuntime(5559): at com.emergency.alert.MyPhoneStateListener.onCallStateChanged(MyPhoneStateListener.java:36) 06-1开发者_StackOverflow社区0 15:32:39.332: ERROR/AndroidRuntime(5559): at 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: ERROR/AndroidRuntime(5559): at android.os.Looper.loop(Looper.java:143) 06-10 15:32:39.332: ERROR/AndroidRuntime(5559): at android.app.ActivityThread.main(ActivityThread.java:4196) 06-10 15:32:39.332: ERROR/AndroidRuntime(5559): at java.lang.reflect.Method.invokeNative(Native Method) 06-10 15:32:39.332: ERROR/AndroidRuntime(5559): at java.lang.reflect.Method.invoke(Method.java:507) 06-10 15:32:39.332: ERROR/AndroidRuntime(5559): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 06-10 15:32:39.332: ERROR/AndroidRuntime(5559): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 06-10 15:32:39.332: ERROR/AndroidRuntime(5559): at dalvik.system.NativeStart.main(Native Method)


I think that your problem is that when you're receiving CALL_STATE_IDLE, the incomingNumber variable is not set because there isn't an active or ringing call, and therefore no phone number associated with the current phone state. You're then doing a contacts lookup which is failing because you are not including a valid phone number in the query.

So you're trying to lookup a contact when you don't actually have a phone number to search on.


Ah I've seen this error before,

your URI is malformed as you are setting it to the default ringtone

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

However the default ringtone has not been set so your getting back a blank string / null or some other error.

check this question and answer for clarity: How to play ringtone/alarm sound in Android

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号