-1
public class MyReceiver extends BroadcastReceiver {
    private Context mcontext;
    TelephonyManager telephonyManager =
            (TelephonyManager) mcontext.getSystemService(Context.TELEPHONY_SERVICE);

public void onReceive(Context context, Intent intent) {
    mcontext = context;
    String action = intent.getAction();

        if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            // This code will execute when the phone has an incoming call

the above code i implemented for incoming call receiver..

vickychala
  • 11
  • 4

1 Answers1

0

The answer is simple: You cannot show an AlertDialog from within a BroadcastReceiver. You have to start an Activity (maybe a transparent one) to show an AlertDialog or simple show a Toast message.

EDIT

I´m not sure if it is possible to show an Activity while a phone call is received. If yes, it´s not a good idea because You are laying that over the phonecall view from Your device.

2nd EDIT

Here is how to implement an incoming call receiver:

first, create a broadcastReceiver for incoming calls:

    public class YourIncommingCallReceiver extends BroadcastReceiver {



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


             try{              
                 String phoneState = intent.getStringExtra(TelephonyManager.EXTRA_STATE);                     

            if(phoneState.equals(TelephonyManager.EXTRA_STATE_RINGING)){

          //example from Android API       
          IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
          Intent batteryStatus = context.registerReceiver(null, ifilter);

          int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
          boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                     status == BatteryManager.BATTERY_STATUS_FULL;

                 if(isCharging==true){
                       Toast.makeText(mContext,"PLEASE UNPLUG", Toast.LENGTH_LONG).show();
                    }
                  }                
              }

    catch(Exception e)
              {
                 //show error message
              }          
            } 
         }

Then, register receiver in Your manifest:

<receiver android:name=".YourIncommingCallReceiver" android:enabled="true">
            <intent-filter>
              <action android:name="android.intent.action.PHONE_STATE" />
                </intent-filter>
           </receiver>

and add permission in manifest:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

and delete this ACTION_POWER_CONNECTED reciever in Your code. This will only give a broadcast if device is connected or disconnected but not if a phone call comes in.

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
  • have You called the show() method too for Your toast? – Opiatefuchs Feb 16 '16 at 13:41
  • is that possible in broadcastreceiver to detect incoming and action_power_connected ,simultanaeously to notify the user – vickychala Feb 16 '16 at 13:42
  • usually, a broadcast receiver with multiple action works, but it´s not a good idea because I think it cannot be precise enough. instead You should check if device is plugged in Your phone call reciever programmatically. Use one of these answers: http://stackoverflow.com/questions/5283491/android-check-if-device-is-plugged-in – Opiatefuchs Feb 16 '16 at 13:48
  • and it´s related to how You have implemented Your broadcastReceiver inside Your manifest. In Your case, You will first get the action if the device will be plugged in and not on phone call. You should not do this in that way. only register an incoming call reciever and check programmatically inside this if device is in charge... – Opiatefuchs Feb 16 '16 at 13:50
  • if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) { – vickychala Feb 16 '16 at 13:59
  • TelephonyManager telephonyManager = (TelephonyManager) mcontext.getSystemService(Context.TELEPHONY_SERVICE); – vickychala Feb 16 '16 at 13:59
  • is that enough for incoming call receiver – vickychala Feb 16 '16 at 14:00
  • if You have registered the receiver in Your manifest.....please update Your post and also show Your manifest file. Doing this in comments is beyond the frame... – Opiatefuchs Feb 16 '16 at 14:01
  • i want to notify at the time of phone is ringing when we receive incoming call – vickychala Feb 16 '16 at 14:02
  • i have generated the apk and tested its not working...should i want to register with the telephony manager class – vickychala Feb 17 '16 at 12:44