BroadcastReceiver class that detects activity going onPause - android

I'm sorry I know when we mix BroadcastReceivers with activity life-cycle we cause the brain to have lots of errors and malfunction. I need help my brain stopped and my question is simple.
Is there a way to have BroadcastReceiver class that detect an activity going onPause() method ? if yes then how would that class be?

the only thing i can think of it on your activity send a costume broadcast intent that one of your receivers.
e.g:
action:
public static final String CUSTOM_INTENT = "example.com.intent.action.ActivityGoingOnPause";
activity onPause:
protected void onPause() {
Intent i = new Intent();
i.setAction(CUSTOM_INTENT);
context.sendBroadcast(i);
}
manifest:
<receiver android:name=".YourReceiver" android:enabled="true">
<intent-filter>
<action android:name="example.com.intent.action.ActivityGoingOnPause"></action>
</intent-filter>
</receiver>
reciver:
public class YourReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(YourActivity.CUSTOM_INTENT)) {
//do your thing
}
}
}
Hope this helps

Set a global variable like isActivityPaused in onPause and unset it in onResume of the activity and then check that variable and decide whether to send broadcast or not.

Related

Set InnerClass broadcastreceiver to android action

This is what I am using right now:
in the onCreate method:
registerReceiver(bootup, new IntentFilter("android.intent.action.BOOT_COMPLETED"));
the bootup receiver:
BroadcastReceiver bootup = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("test", "received");
}
};
These are both in the MainActivity class of my app. What I want is for the bootup receiver to be called when the bootup of the phone is complete. I need it in the MainActivity class because I need to access a few things from it.
How would I set this? My current solution does not work.
EDIT: The posted solution seems as though it will work for my purpose, and this is what has been suggested by other threads. However, when I put a log statement in the receiver that it ties to, nothing appears in the console. Is this because the application is not running? I have also tried sending a notification with notificationmanager
EDIT 2: I took advice and switched to a broadcastreceiver in a another class for detecting the reboot. It works by simply changing the name attribute on the manifest file. This being the reason why I cant use an inner class receiver doesn't really make any sense compared to what I have seen on other questions. Can someone explain why I can't point the receiver to the inner class one and why I have to use a separate class?
Create a Receiver:
public class BootupReceiver extends BroadcastReceiver {
private final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";
#Override
public void onReceive(Context context, Intent intent) {
if (ACTION_BOOT.equals(intent.getAction()))
Toast.makeText(context, R.string.bootup_receiver, Toast.LENGTH_SHORT).show();
}
}
And receiver and uses-permission in AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="com.example.restarttest.BootupReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

Broadcast not triggering

I have been teaching myself android programming and I've run up against a problem with broadcasts. Essentially this code is just me testing to see if I can get broadcasts to work. When I run it my broadcast is not called and I'm not sure why.
Here is the relevant bit of manifest
<reciever
android:name="application.logic.StartEventReciever"
android:label="#string/title_activity_start_event" >
<intent-filter>
<action android:name="Set Start Alarm" />
</intent-filter>
</reciever>
Here is my BroadcastReciever
public class StartEventReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.v("#StartEvent", "BOO");
}
}
And here is the bit where I make the intent. It's a part of a larger static class I've been using.
public static void setStartAlarm(Context context){
Intent intent = new Intent("Set Start Alarm");
context.sendBroadcast(intent);
}
Your <reciever tag is spelled wrong. It is <receiver> </receiver>

Broadcast Receiver with inner class not registered

I've searched and searched, and tried everything I can think of 5 times. I can not get my broadcast receiver to register.
Currently, in the manifest, in the application element, I have:
<receiver android:name=".MainActivity$browserReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="com.mycompany.snapbench.browsertest" />
</intent-filter>
</receiver>
In my MainActivity class:
public static BrowserReceiver browserReceiver;
MainActivity->onCreate:
browserReceiver = new BrowserReceiver();
And finally, at the end of the MainActivity class:
public class BrowserReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("Receiver", "I'm pretty sick of this.");
}
}
When I press a button in the app, I run this, as a test:
if (isIntentAvailable(this, "com.mycompany.snapbench.browsertest")) {
Log.e("Intent", "It is there.");
} else {
Log.e("Intent", "FAIL");
}
And, of course, I always get "FAIL" in LogCat. What am I doing wrong?
Just tested two approaches and both worked fine. For both I implemented a a class SmsReceiver in an activity called ContactManager:
public static class SmsReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
#Override
public void onReceive(final Context context, final Intent intent) {
if (intent != null && SMS_RECEIVED.equals(intent.getAction())) {
Log.d("TAG", "SMS received in inner class");
}
}
}
first I registered the receiver in the onCreate() method:
IntentFilter filter = new IntentFilter(SmsReceiver.SMS_RECEIVED);
BroadcastReceiver receiver = new SmsReceiver();
registerReceiver(receiver, filter);
Actually it would be better do register it in onResume() and unregister it in onPause().
secondly I removed my registration code and added the receiver to the manifest:
Both solutions worked pretty well and printed the text SMS received in inner class. As far as I see you didn't declare your inner class static. That's might be the error. So simply change it to:
public static class BrowserReceiver extends BroadcastReceiver {
...
}
Hope this helps ... Cheers!
call registerReceiver in onCreate of your MainActivity
registerReceiver(browserReceiver, new IntentFilter("com.mycompany.snapbench.browsertest"));
remember to unregisterReceiver(browserReceiver) in onDestroy.

How can i get recevier instance(which is registered in AndroidManifest.xml) in activity

I've register a receiver in AndroidManifest.xml like this
<receiver android:name="com.sunrise.taximate.message.MessageRecevier">
<intent-filter>
<action android:name="xxx.xxxx.xxx.xxx" />
</intent-filter>
</receiver>
and now I want to get the receiver's instance in one of my activities(Like MainActivity),but I don't know how to. anyone can help me?
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
// Your code here to do what ever you want
}
}
Receivers are meant to act on events generated by the system and sometimes the users. There are special cases where you might want to get an instance of those yourself but this is uncommon. The whole point of having receivers is to react to system events and take some action. Unless you know what you are doing, I would recommend against creating receiver instances yourself in an activity.
If you really want to, you can do it like this
private BroadcastReceiver myReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
// do stuff
}
}
Also see this thread for related info:
BroadcastReceiver as inner class

Android BroadcastReceiver within Activity

I'm just trying this little sample project, all it does:
Activity one has a Button that sends a Broadcast. Activity two displays a toast when received.
Below is the code, the Broadcast is never received. What do I do wrong?
Sending the Broadcast
public class SendBroadcast extends Activity {
public static String BROADCAST_ACTION = "com.unitedcoders.android.broadcasttest.SHOWTOAST";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void sendBroadcast(View v){
Intent broadcast = new Intent();
broadcast.setAction(BROADCAST_ACTION);
sendBroadcast(broadcast);
}
}
Receiving it
public class ToastDisplay extends Activity {
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(), "received", Toast.LENGTH_SHORT);
}
};
#Override
protected void onResume() {
IntentFilter filter = new IntentFilter();
filter.addAction(SendBroadcast.BROADCAST_ACTION);
registerReceiver(receiver, filter);
super.onResume();
}
#Override
protected void onPause() {
unregisterReceiver(receiver);
super.onPause();
}
}
Manifest
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".SendBroadcast" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ToastDisplay">
<intent-filter>
<action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST"></action>
</intent-filter>
</activity>
</application>
What do I do wrong?
The source code of ToastDisplay is OK (mine is similar and works), but it will only receive something, if it is currently in foreground (you register receiver in onResume). But it can not receive anything if a different activity (in this case SendBroadcast activity) is shown.
Instead you probably want to startActivity ToastDisplay from the first activity?
BroadcastReceiver and Activity make sense in a different use case. In my application I need to receive notifications from a background GPS tracking service and show them in the activity (if the activity is in the foreground).
There is no need to register the receiver in the manifest. It would be even harmful in my use case - my receiver manipulates the UI of the activity and the UI would not be available during onReceive if the activity is not currently shown. Instead I register and unregister the receiver for activity in onResume and onPause as described in
BroadcastReceiver documentation:
You can either dynamically register an instance of this class with Context.registerReceiver() or statically publish an implementation through the tag in your AndroidManifest.xml.
Toast.makeText(getApplicationContext(), "received", Toast.LENGTH_SHORT);
makes the toast, but doesnt show it.
You have to do Toast.makeText(getApplicationContext(), "received", Toast.LENGTH_SHORT).show();
Extends the ToastDisplay class with BroadcastReceiver and register the receiver in the manifest file,and dont register your broadcast receiver in onResume() .
<application
....
<receiver android:name=".ToastDisplay">
<intent-filter>
<action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST"/>
</intent-filter>
</receiver>
</application>
if you want to register in activity then register in the onCreate() method e.g:
onCreate(){
sentSmsBroadcastCome = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "SMS SENT!!", Toast.LENGTH_SHORT).show();
}
};
IntentFilter filterSend = new IntentFilter();
filterSend.addAction("m.sent");
registerReceiver(sentSmsBroadcastCome, filterSend);
}
You need to define the receiver as a class in the manifest and it will receive the intent:
<application
....
<receiver android:name=".ToastReceiver">
<intent-filter>
<action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST"/>
</intent-filter>
</receiver>
</application>
And you don't need to create the class manually inside ToastDisplay.
In the code you provided, you must be inside ToastDisplay activity to actually receive the Intent.
I think your problem is that you send the broadcast before the other activity start ! so the other activity will not receive anything .
The best practice to test your code is to sendbroadcast from thread or from a service so the activity is opened and its registered the receiver and the background process sends a message.
start the ToastDisplay activity from the sender activity ( I didn't test that but it may work probably )
You forget to write .show() at the end, which is used to show the toast message.
Toast.makeText(getApplicationContext(), "received", Toast.LENGTH_SHORT).show();
It is a common mistake that programmer does, but i am sure after this you won't repeat the mistake again... :D
Your also have to register the receiver in onCreate(), like this:
IntentFilter filter = new IntentFilter();
filter.addAction("csinald.meg");
registerReceiver(receiver, filter);

Categories

Resources