Reputation: 21
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.
Upvotes: 2
Views: 1904
Reputation: 14755
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
Upvotes: 2