Example usage for android.telephony TelephonyManager EXTRA_INCOMING_NUMBER

List of usage examples for android.telephony TelephonyManager EXTRA_INCOMING_NUMBER

Introduction

In this page you can find the example usage for android.telephony TelephonyManager EXTRA_INCOMING_NUMBER.

Prototype

String EXTRA_INCOMING_NUMBER

To view the source code for android.telephony TelephonyManager EXTRA_INCOMING_NUMBER.

Click Source Link

Document

Extra key used with the #ACTION_PHONE_STATE_CHANGED broadcast for a String containing the incoming or outgoing phone number.

Usage

From source file:com.jsw.callcastreceiver.Call_BroadCastReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();//  w w  w . j a  v  a2 s  .  c  om
    if (bundle != null) {
        String state = bundle.getString(TelephonyManager.EXTRA_STATE);
        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            String number = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);

            Intent newIntent = new Intent(context, Telefon_Activity.class);
            newIntent.putExtra("number", number);
            PendingIntent pending = PendingIntent.getActivity(context, CALLNOTIFICATION, newIntent,
                    PendingIntent.FLAG_ONE_SHOT);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
            builder.setContentTitle("Telefon Activity");
            builder.setSmallIcon(R.mipmap.ic_launcher);
            builder.setContentText("Incoming call from: " + number);
            builder.setDefaults(Notification.DEFAULT_VIBRATE);
            builder.setDefaults(Notification.DEFAULT_LIGHTS);
            //Aadir el objeto PendingItem a la notificicacion
            builder.setContentIntent(pending);
            //Aadir la notificacion al Manager
            NotificationManager notify = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            notify.notify(CALLNOTIFICATION, builder.build());
        }
    }
}

From source file:com.yangtsaosoftware.pebblemessenger.receivers.CallStateHandler.java

@Override
public void onReceive(Context context, Intent intent) {

    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
        String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Constants.log(LOG_TAG, "A new call is coming:" + incomingNumber);
        Intent inner_intent = new Intent(MessageProcessingService.class.getName());
        inner_intent.putExtra(Constants.BROADCAST_COMMAND, Constants.BROADCAST_CALL_INCOMING);
        if (incomingNumber != null && !incomingNumber.isEmpty()) {
            inner_intent.putExtra(Constants.BROADCAST_PHONE_NUM, incomingNumber);
            inner_intent.putExtra(Constants.BROADCAST_NAME, Constants.queryNameByNum(context, incomingNumber));
        } else {// www.jav a 2  s.c  o  m
            inner_intent.putExtra(Constants.BROADCAST_PHONE_NUM, "0");
            inner_intent.putExtra(Constants.BROADCAST_NAME,
                    context.getString(R.string.notificationservice_privateNumber));
        }
        LocalBroadcastManager.getInstance(context).sendBroadcast(inner_intent);
    } else if (TelephonyManager.EXTRA_STATE_IDLE.equals(state)) {
        Constants.log(LOG_TAG, "Call is idle");
        Intent inner_intent = new Intent(MessageProcessingService.class.getName());
        inner_intent.putExtra(Constants.BROADCAST_COMMAND, Constants.BROADCAST_CALL_IDLE);
        LocalBroadcastManager.getInstance(context).sendBroadcast(inner_intent);
        inner_intent = new Intent(PebbleCenter.class.getName());
        inner_intent.putExtra(Constants.BROADCAST_COMMAND, Constants.BROADCAST_CALL_IDLE);
        LocalBroadcastManager.getInstance(context).sendBroadcast(inner_intent);
    } else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state)) {
        Constants.log(LOG_TAG, "Call is off hook");
        Intent inner_intent = new Intent(PebbleCenter.class.getName());
        inner_intent.putExtra(Constants.BROADCAST_COMMAND, Constants.BROADCAST_CALL_HOOK);
        LocalBroadcastManager.getInstance(context).sendBroadcast(inner_intent);
        inner_intent = new Intent(MessageProcessingService.class.getName());
        inner_intent.putExtra(Constants.BROADCAST_COMMAND, Constants.BROADCAST_CALL_HOOK);
        LocalBroadcastManager.getInstance(context).sendBroadcast(inner_intent);
    }
}

From source file:at.bitfire.nophonespam.CallReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(intent.getAction()) && intent
            .getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
        String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Log.i(TAG, "Received call: " + incomingNumber);

        Settings settings = new Settings(context);
        if (TextUtils.isEmpty(incomingNumber)) {
            // private number (no caller ID)
            if (settings.blockHiddenNumbers())
                rejectCall(context, null);

        } else {/*from   ww w. ja v  a 2 s  .  c o  m*/
            DbHelper dbHelper = new DbHelper(context);
            try {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                Cursor c = db.query(Number._TABLE, null, "? LIKE " + Number.NUMBER,
                        new String[] { incomingNumber }, null, null, null);
                if (c.moveToNext()) {
                    ContentValues values = new ContentValues();
                    DatabaseUtils.cursorRowToContentValues(c, values);
                    Number number = Number.fromValues(values);

                    rejectCall(context, number);

                    values.clear();
                    values.put(Number.LAST_CALL, System.currentTimeMillis());
                    values.put(Number.TIMES_CALLED, number.timesCalled + 1);
                    db.update(Number._TABLE, values, Number.NUMBER + "=?", new String[] { number.number });

                    BlacklistObserver.notifyUpdated();
                }
                c.close();
            } finally {
                dbHelper.close();
            }
        }
    }
}

From source file:com.szanata.cordova.phonestatechangelistener.PhoneStateChangeListener.java

/**
 * creates a new BroadcastReceiver to listen whether the Telephony State changes
 *///from   w  ww .  j  av  a 2 s .c om
public void startPhoneListener(final CallbackContext callbackContext) {

    if (this.receiver == null) {
        this.receiver = new BroadcastReceiver() {

            @Override
            public void onReceive(final Context context, final Intent intent) {

                if (intent != null && intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
                    String state = intent.hasExtra(TelephonyManager.EXTRA_STATE)
                            ? intent.getStringExtra(TelephonyManager.EXTRA_STATE)
                            : NONE;
                    String number = "";

                    if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                        number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                    }

                    final JSONObject data = new JSONObject();
                    try {
                        data.put("state", state);
                        data.put("number", number);
                        callbackContext.success(data);
                    } catch (final JSONException e) {
                        callbackContext.error(e.getMessage());
                    }

                }
            }
        };

        this.context.registerReceiver(this.receiver,
                new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED));
    }
}

From source file:com.szanata.cordova.plugins.PhoneStateChangeListener.java

/**
 * creates a new BroadcastReceiver to listen whether the Telephony State changes
 *//*  ww w.ja  va2  s .co m*/
public void startPhoneListener() {

    if (this.receiver == null) {
        this.receiver = new BroadcastReceiver() {

            @Override
            public void onReceive(final Context context, final Intent intent) {

                if (intent != null && intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
                    String state = intent.hasExtra(TelephonyManager.EXTRA_STATE)
                            ? intent.getStringExtra(TelephonyManager.EXTRA_STATE)
                            : NONE;
                    String number = "";

                    if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                        number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                    }
                    if (callbackContext != null) {
                        final JSONObject data = new JSONObject();
                        try {
                            data.put("state", state);
                            data.put("number", number);
                        } catch (final JSONException e) {
                        }
                        ;
                        callbackContext.success(data);
                    }
                }
            }
        };

        this.context.registerReceiver(this.receiver,
                new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED));
    }
}

From source file:org.durka.hallmonitor.CoreReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED) || intent.getAction().equals(QUICKBOOT_POWERON)
            || intent.getAction().equals(HTC_QUICKBOOT_POWERON)) {
        Log.d(LOG_TAG + ".boot", "Boot called.");
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        if (prefs.getBoolean("pref_enabled", false)) {
            Intent mIntent = new Intent(context, CoreService.class);
            context.startService(mIntent);
        }//from   www  .  j  av a2s.c  o m
    }

    if (CoreStateManager.getInitialized()) {
        localContext = CoreStateManager.getContext();
        mStateManager = ((CoreApp) localContext).getStateManager();
    } else {
        return;
    }

    if (!mStateManager.getPreference().getBoolean("pref_enabled", false)) {
        return;
    }

    mStateManager.acquireCPUGlobal();

    if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {

        Log.d(LOG_TAG + ".screen", "Screen on event received.");

        if (mStateManager.getCoverClosed()) {
            Log.d(LOG_TAG + ".screen", "Cover is closed, display Default Activity.");
            mStateManager.setBlackScreenTime(0);
            Intent mIntent = new Intent(localContext, CoreService.class);
            mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_LAUNCH_ACTIVITY);
            mStateManager.sendToCoreService(mIntent);
        } else {
            // Log.d(LOG_TAG + ".screen",
            // "Cover is open, free everything.");
            // mStateManager.freeDevice();
            Log.d(LOG_TAG + ".screen", "Cover is open, send to background.");
            Intent stbDAIntent = new Intent(CoreApp.DA_ACTION_SEND_TO_BACKGROUND);
            LocalBroadcastManager.getInstance(localContext).sendBroadcastSync(stbDAIntent);
        }

    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
        Log.d(LOG_TAG + ".screen", "Screen off event received.");
        // mStateManager.freeDevice();

    } else if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
        Intent batteryDAIntent = new Intent(CoreApp.DA_ACTION_BATTERY_REFRESH);
        LocalBroadcastManager.getInstance(localContext).sendBroadcastSync(batteryDAIntent);

        Intent mIntent = new Intent(localContext, CoreService.class);
        mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_WAKEUP_DEVICE);
        mStateManager.sendToCoreService(mIntent);

    } else if (intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)) {
        Intent batteryDAIntent = new Intent(CoreApp.DA_ACTION_BATTERY_REFRESH);
        LocalBroadcastManager.getInstance(localContext).sendBroadcastSync(batteryDAIntent);

        Intent mIntent = new Intent(localContext, CoreService.class);
        mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_WAKEUP_DEVICE);
        mStateManager.sendToCoreService(mIntent);

    } else if (intent.getAction().equals(ALARM_ALERT_ACTION)) {

        Log.d(LOG_TAG + ".alarm", "Alarm on event received.");

        // only take action if alarm controls are enabled
        if (mStateManager.getPreference().getBoolean("pref_alarm_controls", false)) {

            Log.d(LOG_TAG + ".alarm", "Alarm controls are enabled, taking action.");
            mStateManager.setAlarmFiring(true);

            Intent mIntent = new Intent(localContext, CoreService.class);
            mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_INCOMMING_ALARM);
            mStateManager.sendToCoreService(mIntent);
        } else {
            Log.d(LOG_TAG + ".alarm", "Alarm controls are not enabled.");
        }

    } else if (intent.getAction().equals(ALARM_DONE_ACTION)) {

        Log.d(LOG_TAG + ".alarm", "Alarm done event received.");

        // only take action if alarm controls are enabled
        if (mStateManager.getPreference().getBoolean("pref_alarm_controls", false)) {
            Log.d(mStateManager.getPreference() + ".alarm", "alarm is over, cleaning up");
            mStateManager.setAlarmFiring(false);

            if (mStateManager.getCoverClosed()) {
                Intent mIntent = new Intent(localContext, CoreService.class);
                mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_LAUNCH_ACTIVITY);
                mStateManager.sendToCoreService(mIntent);
            }
        }
    } else if (intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {

        if (mStateManager.getPreference().getBoolean("pref_phone_controls", false)) {
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            Log.d(LOG_TAG + ".phone", "phone state changed to " + state);
            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                Intent mIntent;
                mStateManager.setPhoneRinging(true);
                mStateManager.setCallFrom(intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER));
                Log.d(LOG_TAG, "call from " + mStateManager.getCallFrom());
                mIntent = new Intent(localContext, CoreService.class);
                mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_INCOMMING_CALL);
                mStateManager.sendToCoreService(mIntent);
            } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                Intent mIntent;
                mStateManager.setPhoneRinging(false);
                Log.d(LOG_TAG, "call is over, cleaning up");
                if (mStateManager.getCoverClosed()) {
                    mIntent = new Intent(localContext, CoreService.class);
                    mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_LAUNCH_ACTIVITY);
                    mStateManager.sendToCoreService(mIntent);
                }
            } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
            }
        } else {
            Log.d(LOG_TAG + ".phone", "phone controls are not enabled");
        }
    } else if (intent.getAction().equals(mStateManager.getActionCover())) {
        int state = intent.getIntExtra(EXTRA_LID_STATE, LID_ABSENT);
        Log.d(LOG_TAG + ".cover", "cover state changed to " + state);
        if (state == LID_CLOSED) {
            Log.d(LOG_TAG + ".cover", "Cover is close, enable Default Activity.");
            mStateManager.setCoverClosed(true);
            Intent mIntent = new Intent(localContext, CoreService.class);
            mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_LAUNCH_ACTIVITY);
            mStateManager.sendToCoreService(mIntent);
        } else if (state == LID_OPEN) {
            // Log.d(LOG_TAG + ".cover",
            // "Cover is open, stopping Default Activity.");
            mStateManager.setCoverClosed(false);
            // mStateManager.freeDevice();
            Log.d(LOG_TAG + ".screen", "Cover is open, send to background.");
            Intent stbDAIntent = new Intent(CoreApp.DA_ACTION_SEND_TO_BACKGROUND);
            LocalBroadcastManager.getInstance(localContext).sendBroadcastSync(stbDAIntent);
            Intent mIntent = new Intent(localContext, CoreService.class);
            mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_WAKEUP_DEVICE);
            mStateManager.sendToCoreService(mIntent);
        }

    } else if (intent.getAction().equals(TORCH_STATE_CHANGED)) {
        if (mStateManager.getPreference().getBoolean("pref_flash_controls", false)) {
            Log.d(LOG_TAG + ".torch", "torch state changed");
            Intent mIntent = new Intent(localContext, CoreService.class);
            mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_TORCH_STATE);
            if (intent.getIntExtra("state", 0) != 0) {
                mIntent.putExtra(CoreApp.CS_EXTRA_STATE, true);
            } else {
                mIntent.putExtra(CoreApp.CS_EXTRA_STATE, false);
            }
            mStateManager.sendToCoreService(mIntent);
        } else {
            Log.d(LOG_TAG + ".torch", "torch controls are not enabled.");
        }

    } else if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
        int state = intent.getExtras().getInt("state");
        Log.d(LOG_TAG + ".headset", "headset is " + (state == 0 ? "gone" : "here") + "!");
        Intent mIntent = new Intent(localContext, CoreService.class);
        mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_HEADSET_PLUG);
        mStateManager.sendToCoreService(mIntent);

    } else if (intent.getAction().equals("org.durka.hallmonitor.debug")) {
        Log.d(LOG_TAG + "", "received debug intent");
        // test intent to show/hide a notification
        boolean showhide = false;
        switch (intent.getIntExtra("notif", 0)) {
        case 1:
            showhide = true;
            break;
        case 2:
            showhide = false;
            break;
        }
        if (showhide) {
            Notification.Builder mBuilder = new Notification.Builder(localContext)
                    .setSmallIcon(R.drawable.ic_launcher).setContentTitle("Hall Monitor")
                    .setContentText("Debugging is fun!");

            NotificationManager mNotificationManager = (NotificationManager) localContext
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(42, mBuilder.build());
        } else {
            NotificationManager mNotificationManager = (NotificationManager) localContext
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.cancel(42);
        }
    }
    mStateManager.releaseCPUGlobal();
}