title: Unable to start activity from onReceive method of broadCastReceiver
body: <p>I am working on an alarm application.
  In this, I am using a broadcast receiver and alarm manager. When the alarm goes off the onReceive method is called and then from the onReceive method, I am calling startActivity to start an intent </p>
  
  <p>BUT the problem that I am facing is that in some devices activity is not getting started when the app is in the background or killed.</p>
  
  <p>In onReceive method, I have three things: </p>
  
  <ol>
  <li>Toast </li>
  <li>RingtoneManager</li>
  <li>Intent</li>
  </ol>
  
  <pre><code>  @Override
      public void onReceive(Context context, Intent intent) {
  
          Toast.makeText(context, "In broadcast receiver", Toast.LENGTH_LONG).show();
  
          Bundle args = intent.getBundleExtra(Constants.BROADCAST_RECEIVER_BUNDLE);
          Alarms alarms = (Alarms) args.getSerializable(Constants.ALARM_OBJECT);
          int alarmRequestCode = args.getInt(Constants.ALARM_REQUEST_CODE);
  
  
          Intent OpenAppIntent = new Intent();
  
  //      I also tried with adding classes in intent object directly eg new Intent(context, MyActivity.class) but no luck
  
          OpenAppIntent.setClass(context,com.example.MyActivity.class);
          OpenAppIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
          OpenAppIntent.putExtra(Constants.BROADCAST_RECEIVER_BUNDLE , args);
          context.startActivity(OpenAppIntent);
  
          Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
          if (alarmUri == null) {
              alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
          }
          Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
          ringtone.play();
  
      }
  
  
  </code></pre>
  
  <p>Now we have two cases</p>
  
  <ol>
  <li>When we have all three items in onReceive method ( toast, ringtone manager and intent ) - Then toast and ringtone is getting played and displayed respectively and activity is not getting started </li>
  <li>When we remove the ringtone manager from onReceive method ( toast and intent ) - Then Toast is also not getting displayed ( activity is not started as well )</li>
  </ol>
  
  <p>Devices in which this problem is noticed are Gionee F9, Motorola g5s, and Redmi k20 pro. Also, sometimes the same thing happens with Nokia 7 Plus</p>
  
  <p>Please tell me what's wrong with the code or what is the best way to start an activity when the broadcast is received.</p>
  
  <p><strong>Test 1</strong></p>
  
  <p>Instead of starting activity I tried displaying a fullscreen notification as shown <a href="https://developer.android.com/training/notify-user/time-sensitive" rel="nofollow noreferrer">here</a> and as suggested in a comment but unable to do so because of Error <em>Non-static method startForeground(int, notification) cannot be referenced from a static context</em></p>
  
  <pre><code>@Override
      public void onReceive(Context context, Intent intent) {
  
  
          Intent notificationIntent = new Intent(context, MyActivity.class);
          Bundle bundle = new Bundle();
          bundle.putSerializable(Constants.ALARM_OBJECT , alarms);
          notificationIntent.putExtra(Constants.BROADCAST_RECEIVER_BUNDLE , bundle);
          PendingIntent pendingIntent = PendingIntent.getActivity(context,
                  0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  
          Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID)
                  .setContentTitle(Constants.APP_NAME)
                  .setContentText("Stop the alarm")
                  .setSmallIcon(R.drawable.logo_inner)
                  .setPriority(NotificationCompat.PRIORITY_HIGH)
                  .setFullScreenIntent(pendingIntent, true)
                  .build();
  
          Service.startForeground(1, notification); // Non-static method startForeground(int, notification) cannot be referenced from a static context 
      }
  
  </code></pre>
  
  <p><strong>Test 2</strong></p>
  
  <p>I Also tried starting foreground service directly from the onReceive method but the same problem is occurring as with the starting activity. I also tried debugging by placing breakpoints on the onCreate method of service but onCreate never get called these devices ( Gionee F9, Motorola g5s, and Redmi k20 pro )</p>
  
  <pre><code>    @Override
      public void onReceive(Context context, Intent intent) {
          Intent alarmServiceIntent = new Intent(context, AlarmSoundService.class);
          Bundle alarmServiceBundle = new Bundle();
          alarmServiceBundle.putSerializable(Constants.ALARM_OBJECT , alarms );
          alarmServiceIntent.putExtra(Constants.ALARM_BUNDLE , alarmServiceBundle);
  //        startService(alarmServiceIntent);
          ContextCompat.startForegroundService(context, alarmServiceIntent);
      }
  
  </code></pre>
  
  <p>Any suggestions will be appreciated.</p>
  
  <p>Thank you</p>