2

I broadcast an Intent : com.ss.CUSTOM_INTENT

/*-----Class BroadcastReceiverApp-------*/<br/>
public class BroadcastReceiverApp extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.broadcast_activity);
    }

/*    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }*/

    // broadcast a custom intent.
    public void broadcastIntent(View view){
        Intent intent = new Intent();
        intent.setAction("com.ss.CUSTOM_INTENT");
        sendBroadcast(intent);
    }
}
/*------------------------*/

But when I receive that broadcast intent, I want it to be shown in notification drawer. The Receiver class can extend only BroadcastReceiver class, hence it is now allowing to extend Activity class there by I cannot use:

Context context = getApplicationContext();

which is to be further used in Notification.Builder(context)....

This is my the Broadcast Receiver class:

public class MyReceiver extends BroadcastReceiver , Activity{

    private static final int MY_NOTIFICATION_ID=1;
    NotificationManager notificationManager;
    Notification myNotification;
    private final String myBlog = "http://android-er.blogspot.com/";
    Context cont = getApplicationContext();

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();

                Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog));
        PendingIntent pendingIntent = PendingIntent.getActivity(
                MyReceiver.this,
                0,
                myIntent,
                Intent.FLAG_ACTIVITY_NEW_TASK);

        myNotification = new Notification.Builder(context)
                .setContentTitle("Exercise of Notification!")
                .setContentText("http://android-er.blogspot.com/")
                .setTicker("Notification!")
                .setWhen(System.currentTimeMillis())
                .setContentIntent(pendingIntent)
                .setDefaults(Notification.DEFAULT_SOUND)
                .setAutoCancel(true)
                .setSmallIcon(R.drawable.ic_launcher)
                .build();

        notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(MY_NOTIFICATION_ID, myNotification);

        Log.i(getClass().getSimpleName(),"Sucessfully Changed Time");
    }
}

Can anyone help in this ? I want to later change the broadcast intent to "android.intent.action.DATE_CHANGED/TIME_CHANGED" which is even not working.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • You cannot extend two base classes at the same time. – EpicPandaForce May 22 '16 at 16:59
  • :! I know that, we need interfaces for that, but how can I broadcast something finally into a Notification finally, thats my exact question, since receiver class is not allowing me to create a Context object. – Suraj Sawant May 23 '16 at 09:35
  • Two different class working with each other. Where do you need the context ? – AxelH May 23 '16 at 09:42
  • @SurajSawant are you extending Activity just to have access to the Context ?? – AxelH May 23 '16 at 09:44
  • Please ignore that, It doesn't work like that I know , I have just shown them here to convey that I want to use them together. I've found one such example http://www.androidbegin.com/tutorial/android-broadcast-receiver-notification-tutorial/ – Suraj Sawant May 23 '16 at 10:13
  • you can `implement` from `BroadcastReceiver` – Devendra Singh May 23 '16 at 10:15

1 Answers1

2

But when I receive that broadcast intent, I want it to be shown in notification drawer.

If your question is "how to update the gui from a BroadcastReceiver" you have to put the BroadcastReceiver class inside the Activity that you want to update.

The nested BroadcastReceiver class can access the elements of the outer Activity.

For an example see Android BroadcastReceiver within Activity

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
k3b
  • 14,517
  • 7
  • 53
  • 85