Reputation: 1420
as far as I can tell this should work. Except it doesn't :-/
the idea is to extend all the Activity classes I use to include a BroadcastReceiver that pops up Toast Messages when I send them. The end game for this strategy is that async worker jobs can "discover for themselves" which is the "top most" form (if available) and then post messages to the uses via toast despite being part of a async event when I DO NOT CHOOSE(*) to pass the context as an argument.
The problem: when I call doDemo() the fire event happenes (I get the
log.d
from doDemo()) but the receive event never fires.
(*)this is essential, it's the whole purpose
public class ToastActivity extends Activity{
private BroadcastReceiver ToastBroadCastReceiver = new BroadcastReceiver() {
@Override
public void onReceive( Context context, Intent intent ) {
Toast.makeText(getApplicationContext(), intent.getAction() + " package: "+ intent.getPackage(), Toast.LENGTH_SHORT).show();
Log.d("ToastBroadCastReceiver", "Broadcastreceiver: " + intent.getAction() + " package: "+ intent.getPackage() + " @" + System.currentTimeMillis());
}
};
public void onResume() {
super.onResume();
registerReceiver(ToastBroadCastReceiver, new IntentFilter("com.my.domain.BROADCAST"));
}
public void onPause() {
super.onPause();
unregisterReceiver(ToastBroadCastReceiver);
}
public void doDemo(){
Log.d("ToastBroadCastReceiver", "doDemo Broadcast started" );
Intent broadCastIntent = new Intent();
broadCastIntent.setAction( "com.my.domain.BROADCAST");
broadCastIntent.setPackage( "well hello test!" );
sendBroadcast(broadCastIntent);
Log.d("ToastBroadCastReceiver", "doDemo Broadcast sent" );
}
}
EDIT
After a long conversation with Ali this is what we came up with
public class ToastActivity extends Activity{
private BroadcastReceiver ToastBroadCastReceiver = new BroadcastReceiver() {
@Override
public void onReceive( Context context, Intent intent ) {
if (intent.getAction().equals("TOASTMESSAGE")) {
String message = "";
Bundle extras = intent.getExtras();
if (extras != null) {
message = extras.getString("message");
}
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
Log.d("ToastBroadCastReceiver", "Broadcastreceiver: " + message + "|" + intent.getAction());
}
}
};
public void ToastRegister(){
registerReceiver(ToastBroadCastReceiver, new IntentFilter("TOASTMESSAGE"));
}
public void ToastDeRegister(){
try {
unregisterReceiver(ToastBroadCastReceiver);
}
catch (Exception e) {
Log.e("ToastBroadCastReceiver", e.getMessage());
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ToastRegister();
}
public void onResume() {
super.onResume();
ToastRegister();
}
public void onPause() {
super.onPause();
ToastDeRegister();
}
public void onDestroy() {
super.onDestroy();
ToastDeRegister();
}
public void doDemo(){
Log.d("ToastBroadCastReceiver", "doDemo Broadcast started" );
Intent broadCastIntent = new Intent();
broadCastIntent.setAction("TOASTMESSAGE");
broadCastIntent.putExtra("message","hello test message!");
sendBroadcast(broadCastIntent);
Log.d("ToastBroadCastReceiver", "doDemo Broadcast sent" );
}
}
Upvotes: 1
Views: 655
Reputation: 9217
This line is the issue broadCastIntent.setPackage( "well hello test!" );
edit your code like this
broadCastIntent.setPackage( "com.my.domain.BROADCAST");
broadCastIntent.setAction( "well hello test!" );
also replace your this line
registerReceiver(ToastBroadCastReceiver, new IntentFilter("com.my.domain.BROADCAST"));
with this
registerReceiver(ToastBroadCastReceiver, new IntentFilter("well hello test!"));
in your onReceive do like this
if (intent.getAction().equals("well hello test!")) {
Update_chat_counter();
Upvotes: 2