When i call my service in a broadcast receiver if i put at the last of onReceive method; the service got triggered correctly but if i put the call and i do some other things after the service never got triggered.
Let me explain, if i do this the service got triggered
public class receiver extends BroadcastReceiver {
#Override
public void onReceive( Context context, Intent intent ) {
// OK
Intent i = new Intent( context, MyService.class );
context.startService( i );
}
}
But if i do this, the service never got triggered and i need to do some other things after the service...
public class receiver extends BroadcastReceiver {
#Override
public void onReceive( Context context, Intent intent ) {
Intent i = new Intent( context, MyService.class );
context.startService( i );
someMethod();
}
}
Maybe i need to add some instruction before i call the service ? .- Sorry about my english
But if i do this, the service never got triggered and i need to do
some other things after the service...
it may help you:
public class receiver extends BroadcastReceiver {
#Override
public void onReceive( Context context, Intent intent ) {
final Context ctx = context;
Thread thread = new Thread(new Runnable(){
#Override
public void run(){
Intent i = new Intent( context, MyService.class );
ctx.startService( i );
}
});
thread.start();
someMethod();
}
}
Related
On Android 7, I know that I have to register the CONNECTIVITY_ACTIONreceiver programmatically, and not in the manifest
the receiver goes off in the moment I register it. How do I prevent that?
in My application class I do this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
IntentFilter filter = new IntentFilter();
filter.addAction(android.net.ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(new NetworkUpdateReceiver(), filter);
}
and then the onReceive method is called right away:
public class NetworkUpdateReceiver extends BroadcastReceiver {
private Context context;
#Override
public void onReceive(Context context, Intent intent) {
this.context = context;
Log.e("NetworkUpdateReceiver", "onReceive");
After that it works normally - I get a method invocation every time the network is turned on or off
BroadcastReceiver.java has method isInitialStickyBroadcast(), so to ignore Connectivity change when you register receiver you can write such code:
public class NetworkUpdateReceiver extends BroadcastReceiver {
private Context context;
#Override
public void onReceive(Context context, Intent intent) {
this.context = context;
if (isInitialStickyBroadcast()) {
Log.i("NetworkUpdateReceiver", "onReceive ignored, sticky");
return;
}
Log.i("NetworkUpdateReceiver", "Actual onReceive");
I have a function in activity I want to run this function with broadcastreceiver. How can I make this?
public class Myclass extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
}
}
This is my broadcastreceiver class I want to run function which is in my activty please tell me with some code how to do this.
If the method you want to execute needs your activity instance, then you can register the broadcast receiver inside your activity, so it can access your activity's state and functions.
In your Activity "onCreate" method:
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("Your Intent action here");
intentFilter.addAction("Another action you want to receive");
final BroadcastReceiver myReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
theFunctionYouWantToExecute();
}
};
registerReceiver(myReceiver, intentFilter);
And in your "onDestroy" method:
unregisterReceiver(myReceiver);
Keep in mind that in this case your broadcast receiver has full access to your activity state, BUT it's lifecycle will be conditioned to the activity lifecycle.
Another option you have is to declare your activity method as static, so you can execute it in any part of your application.
You can declare an interface in Myclass and implement it in your MainActivity
public class Myclass extends BroadcastReceiver{
public interface MyClassInterface {
void onMyClassReceive();
}
private MyClassInterface mListener;
public Myclass(MyClassInterface mMyClassInterface) {
mListener = mMyClassInterface;
}
#Override
public void onReceive(Context context, Intent intent) {
mListener.onMyClassReceive();
}
}
Then in your MainActivity:
public class MainActivity implements Myclass.MyClassInterface {
private mMyClass Myclass = new Myclass(this);
#Override
public void onMyClassReceive() {
// Do stuff when Myclass.onMyClassReceive() is called,
// which will be called when Myclass.onReceive() is called.
}
}
You are almost there. Just create your method in the Activity and using Activity's instance call that method. Remember that your method inside your Activity should be not private.
public class Myclass extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
new YourActivity().yourFunction();
}
}
If you want to create a static method inside your Activity then
public class Myclass extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
YourActivity.yourFunction();
}
}
To trigger the Broadcast, you have to pass an Intent. If you want to trigger it from any Activity then
Intent intent = new Intent();
this.sendBroadcast(intent);
If you want to trigger the Broadcast from a Fragment then
Intent intent = new Intent();
getActivity().sendBroadcast(intent);
I know it's quote naif, but you could call a static method in your activity.
In your activity you declare the method like this:
public static <return_type> yourMethod(<input_objs>){
....
Your code
....
}
In the receiver you can use this function just calling:
YourActivityClass.yourMethod(<input_objs>);
I hope it helped.
I IntentService that I would like to send message to the main Activity it is nested in. I am using a broadcast receiver to broadcast the message I got from the IntentService as such:
public static class ResponseReceiver extends BroadcastReceiver {
public static final String ACTION_RESP = "com.mypackage.intent.action.MESSAGE_PROCESSED";
#Override
public void onReceive(Context context, Intent intent) {
String text;
text = intent.getStringExtra(RegistrationIntentService.PARAM_OUT);
regid = text;
}
}
I have registered the receiver in the Oncreate method of the main Activity. How can I send the "text" in this case? It is weird that regid in this case is null while "text" has the string data I wanted.
you can user result receiver with intent service to get the result into activity or fragment, follow the following links,
http://sohailaziz05.blogspot.in/2012/05/intentservice-providing-data-back-to.html
In your service you do
Intent intent = new Intent();
intent.setAction(yourActionToMatchBroadcastReceiverIntentFilter);
intent.putExtra(RegistrationIntentService.PARAM_OUT, yourText);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
In activity register your BroadcastReceiver in onResume() and unregister it in onPause(). Whenever your activity is active the receiver will receive intents from your IntentService.
EDIT
public static class ResponseReceiver extends BroadcastReceiver {
MapActivity activity;
public ResponseReceiver(MapActivity activity) {
this.activity = activity;
}
#Override
public void onReceive(Context context, Intent intent) {
activity.regid = intent.getStringExtra(RegistrationIntentService.PARAM_OUT);
// do whatever you need here
}
}
When registering, this was what worked for me
IntentFilter filter = new IntentFilter(ResponseReceiver.ACTION_RESP);
filter.addCategory(Intent.CATEGORY_DEFAULT);
receiver = new ResponseReceiver();
LocalBroadcastManager.getInstance(this).registerReceiver(receiver, filter);
as opposed to
registerReceiver(receiver, filter);
#Override
public void onReceive(Context context, Intent intent) {
final String text = intent.getStringExtra(RegistrationIntentService.PARAM_OUT);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
//access the string text and send it to backend
}
});
}
I hope this will help someone. Sending the string like suggested in the comments didn't work for me. I was getting nullpointerexception at that specific line where I assigned ma.regid = text;
I have application that use audio device of the phone, I want my activity to be finish when any call come to my device
This is the code
public class PhoneStateReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context mContext, Intent intent) {
if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_RINGING)) {
// Send local broadcast to set pin code in the dialog
Intent broadcast = new Intent();
broadcast.setAction(ThurayaConstants.INCOMING_CALL);
mContext.sendBroadcast(broadcast);
}
}
}
}
I have added this class to listen to global receiver in the manifest file to detect when any call come to my device.
and in my fragment I register the following receiver
private BroadcastReceiver mIncomingCallReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
getActivity().finish();
}
};
The problem that my activity is not closed , can any one help here ???
I have a question, how to finish activity in brodcastreceiver onCallEnded (SIP) . There is a brodcastreceiver like this:
public class IncomingCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
SipAudioCall incomingCall = null;
try {
SipAudioCall.Listener listener = new SipAudioCall.Listener() {
...
#Override
public void onCallEnded(SipAudioCall call) {
// IncomingCallActivity.finish();
}
};
Main mainActivity = (Main) context;
incomingCall = mainActivity.manager.takeAudioCall(intent, listener);
mainActivity.call = incomingCall;
Intent i = new Intent(context, IncomingCallActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Then start new activity to answer or reject call, but how to close it when call is finished? Cant use IncomingCallActivity.finish() in onCallEnded.
First why can you add the listener in IncomingCallActivity itself. I dont think that will make any difference.
Second for your solution,
start your activity with a FLAG_ACTIVITY_SINGLE_TOP so that you receive the new Intent in onNewIntent(Intent intent) of the activity.
Then onCallEnded start the activity once again.
Intent i = new Intent(context, IncomingCallActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.putExtra("Close", true); // an extra which says to finish the activity.
context.startActivity(i);
Now in onNewIntent(Intent intent)
if(intent.getBooleanExtra("Close", false))
finish();
You can do it like this:
#Override
public void onCallEnded(SipAudioCall call) {
// IncomingCallActivity.finish();
//Here set any value like an int
intToClose = 1;
}
then in your activity check for the intToClose, if its 1 then just call fnish();
UPDATE: This code is from my mind, so it can have some errors. Add this to your current activity, or something like this:
scheduler = Executors.newSingleThreadScheduledExecutor ( );
scheduler.scheduleAtFixedRate ( new Runnable ( ) {
public void run ( )
{
runOnUiThread ( new Runnable ( ) {
public void run ( ) {
closeInt = IncomingCallReceiver.intToClose;
if ( closeInt == 1 ) {
scheduler.shutdown ( );
finish ( );
}
}
} );
}
}, 750, 750, TimeUnit.MILLISECONDS );
I used this code my problem solved.
Try this your problem will be solve. there is no endcall in broadcast so finish() on Idle state.
Happy coding!!
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
((Activity)context).finish();
}