How do i show an AlertDialog from receiver class. My receiver class receives by time of my Alarm using AlarmManager. And, my alertdialog can show if my application is not opened also.
How can i achieve this? Thanks.
There is not possible to create AlertDialog from Broadcast Receiver.
But there is one way to accomplish this task.
Create a activity and set the theme as a dialog.
OnReceive() method of your Broadcast Receiver start the activity which you had create in 1st step
You have to set the flag Intent.FLAG_ACTIVITY_NEW_TASK to start the activity from the broadcast receiver.
So you code will looks like below
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, yourDialogActivity.class);
i .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
Yes, you can do like this. Just start one activity class from your receiver using Intent and in that activity class just use the code from this Blog
Change that class with whatever you needs for AlertDialog. This will display the when your application closed also. Hope this helps you.
Related
I currently have a bound Service with my Main activity.
I was wondering if it was possible to have a Thread Running inside this bound Service that can pass integers to my Main Activity. This Service needs to auto update my Main Activity's Text View with any new Random number Integer without clicking a button.
Should i look into Handlers?? Or Message/ Bundles??
Any help would be appreciated ! Thank you !
You can define one Receiver your MainActivity and you can use send Broadcast to that receiver to update your UI.. That is a simple way to do it
In your service Just define one intent and put values into it like follwing
Intent i = new Intent();
i.setAction("RECEIVERACTION");
i.putExtra("data", "mydata");
sendBroadcast(i);
and in your activity
public static class Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String data = intent.getStringExtra("data");
textView.setText(data);
}
}
Hope it helps!!!
Please go through api guides. Specifically
https://developer.android.com/guide/components/bound-services.html
https://developer.android.com/guide/components/services.html
This should give you a pretty good idea to move forward and also code samples
I am using an alarmmanger with broadcast receiver and when a specified time occurs, i need to update the MainActivity with extra parameter but its behaving randomly.
here is code in the broadcast receiver
public void onReceive(Context context, Intent intent){
//calculate time
//check time
if(isTime){
Log.d("tag", "sleep");
//set value in shared preference
editor = (context.getSharedPreferences("uniqueId", 0)).edit();
editor.putBoolean("sleepmode",false);
editor.apply();
Intent gotoSmileyScreen = new Intent(context, MainActivty.class);
gotoSmileyScreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK );
context.startActivity(gotoSmileyScreen);
}
}
In logcat, i am able to see "sleep" , so i know the method is being called at the right time
Here is code in MainActivity
//inside on create
//get value from shared preference and check
if(!isSleepMode){
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}else{
//do nothing
}
The user will most likely be already in that activity but not necessarily.
The activity is being called for some devices and the screen turns off as expected but for some others, the activity is never called from the intent and screen wont turn off.
From the documentation, Intent.FLAG_ACTIVITY_CLEAR_TASK, should clear the activity before calling it again or am i missing something?
Is there a better way to update the activity from the receiver without calling it again?
Edit: My app is a launcher, does it in any way effect calling the intent?
I found a workaround using Intent.FLAG_ACTIVITY_SINGLE_TOP and overriding onNewIntent in the MainActivity. If the activity is already in the top, it calls onNewIntent otherwise creates a new instance of the activity. So the new code is like this
gotoSmileyScreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP );
and it works. Hope it helps someone.
Change your intent as below and try
Intent gotoSmileyScreen = new Intent();
gotoSmileyScreen.setClassName("com...<Your broadcast receiver name>", "com.....MainActivty.class");
gotoSmileyScreen.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(gotoSmileyScreen);
For Broadcast receiver try like above.
Another way to update the activity is by using Dynamic Broadcast receiver
To acheive this inside the onReceive Method use sendBroadcast to send the data to activity ad inside the activity
register the broadcast Receiver to receive the data
Register your dynamic broadvast Receiver inside onCreate() and unregister inside onDestroy()
I define BroadcastReceiver in an application manifest and the receiver receives events in onReceive as expected. However I do not create the receiver class so I can't pass any information about my activity which the receiver is supposed to control. Context parameter gives me only application context and no any activity reference. So what is right way to make communication between application activities and broadcast receiver?
The answer is a little twisted but I found it here as well
Inform Activity from a BroadcastReceiver ONLY if it is in the foreground
It seems working. Please close the question then.
You can communicate broadcast to activity from this:-
Intent intent=new Intent(context,YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
I'm having a problem with sending a broadcast from a Service to an activity.
This is what I have in my Service class:
Intent intent = new Intent();
intent.setAction(BROADCAST_ACTION);
sendBroadcast(intent);
I have many Activities and in one of my activities I have this:
class MyBroadcast extends BroadcastReceiver {
#Override
public void onReceive(Context ctxt, Intent i) {
System.out.println("received");
}
};
The problem I have is that my broadcast receiver doesn't receive anything!!
Help!
EDIT:
If I have many activities how can send a broadcast message to all of them. In other words can I apply the same broadcast receiver to all the activities !?
Like others said, you need to register the activity first to receive those broadcasts (see Flo's answer)
For your other quesition (re: EDIT). If you are taking the same action, you should create an overall Activity, and have your other activities extend that activity..
Then in this super class, implement the broadcast receiver registers on onResume and un register onStop..
You have to register the broadcast receiver before it can receive anything.
Have a look at this question.
I want to know whether an app can be a BroadcastReceiver and sender? Please expain with an example.
Application can't be a BroadcastReceiver. BroadcastReceiver is an application component. But answer to your question is yes: you can send broadcasts from one component and receive it in another.
For ex. in activity:
Intent intent = new Intent(...);
sendBroadcast(intent);
In receiver:
#Override
public void onReceive(Context context, Intent intent) {
// here is your intent
}
Yes, it can. An example can be found here.
If by app you mean activity, so yes you can but you will have to embed your BroadcastReceiver in your activity and register/unregister it yourself. That way, you just need to add your activity as Activity in the Manifest and you activity will be able to receive a broadcast and send broadcast as well.
I m not too sure how it behaves in term of life cycle though. You will need to look it up if it s what you want.