how to find out broadcast receiver is called? - java

I'm trying to use usb serial port but all example I've seen use broadcast receiver. in their code they give a pending intent to UsbSeialManager.requestPermission(pi). now i want to know if there is a way to see if onReceive() is called or not. and to wait for it.
I need a method that return correct usb(onReceive() is called)
P.S. I'm making a class for each connected usb.

Within the help of Log.d("key","value"); you can print the log to identify that onReceive() is called or not

Related

How to get the Activity instance of an Android app running the foreground?

How do I get the actual Activity instance of the top current activity of a running Andoind application?
Reason: I receive a OnMessageReceived Data payload from Firebase while my application is running in the foreground, and I need to finish() it.
Note: there are tons of other info to get the ComponentName using getRunningTasks() or getAppTasks(), but all these does not seem to provide any way to get the actual Activity instance.
The better way would be to use a Local Broad Cast to inform your activity to finish itself.
You just need to register a Broad Cast Receiver inside your activity and then send the broadcast in your FCM Messaging Service. Check this page on how to do this.
Note: Make sure to unregister the receiver when you're done with the activity.
You can also use EventBus for the Same

Google Cloud Messaging: BroadcastReceiver or Service?

What is the generally preferred method of using GCM in its current state?
The documentation only talks about using it with a BroadcastReceiver and only mentions Services in one sentence without further explanation.
In my application, I need to be able to react to an unknown number of successive GCM messages and queue them so I can process them one by one. This processing needs to be done in order the messages are received in.
This cannot be done with a BroadcastReceiver, as for every broadcast received, a new instance of my receiver class is created (this was the method I tried first as per the getting started guide). Can it be done with a service or, more precisely, is the instance of my service kept between messages received?
If so, when and how does this service need to be started, added to the manifest, etc.?
I do not need to interact with my main application/UI. The service can do its business on its own.
GCM message comes as broadcast so you must use BroadcastReceiver. If you need to queue them for any reason. just make your BroadcastReceiver hand the message to IntentService or anything else you find suitable for your task.

Run AsyncTask every X second to update GPS position

I have a class that extends AsyncTask, which fetches the gps cordinates from the device.
I would like to keep the data updated, so my initial though was to call the class from a timer or a handler. Is this a smart way to implement it, or am i better off listening to the onLocationChanged and do my updates in there?
Hope you get the idea, otherwise ill elaborate.
Thanks!
An alarmManager will be a good solution here.
These allow you to schedule your application to be run at some point in the future.
When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running.
So when alarm gets triggered, call your execute() method of Async task.
For more info see this: http://developer.android.com/reference/android/app/AlarmManager.html
I also want to implement the same in my app in near future. If you get the solution, don't forget to update the post about how you implemented it.
Thank you.

How to run activity, if app in background?

I writing the network application and i have a problem with starting activity if the application in background. I want to start activity when some data comes to network. In my Actity A a have a receiver and when it receive some answer from server, it must run the Activity B. But if the App in background, Activity B not starting and metod onCreate() doesn`t execute. It execute only when the user go back to App. But its not really what i want, becouse in Activity B i need to start timer and i neen to enable GPS and some other work. Besides that, Activity B receive some data too, and if B not existing - this receiver will never receive anything.
I tryed IntentServise, but its not working - result the same as without him.
Any ideas? Thanks for any information :-)
Maybe your receiver is not getting intents when your app is background, for example if you're unregistering it in onDestroy method...
try declaring your broadcast receiver in manifest. In broadcast receiver class use starActivity to call act B. This works for me.
If you still have some problems maybe you should provide some source code(for example function where you're working with intents) to clarify your question.
You can't force activity to come back from background. You can use status bar to show notification for a user. Think also about it that this a mobile device, someone can in the middle of conversation or use GPS as navigation in car.

Android : Phone call detect when connected?

I can use this code to make outgoing call:
Intent dial = new Intent(Intent.ACTION_CALL);
dial.setData(Uri.parse("tel:5556") );
context.startActivity(dial);
But how to detect whether the call is picked up or the call is refused?
I tried PhoneStateListener, but it is not working.
Unfortunately, Android gives no mean to know when an outgoing call has been answered.
PhoneStateListener works fine but the 3 states notified by onCallStateChanged are not enough. An additional state like CALL_STATE_CONNECTED would be welcome.
There is an open issue requesting this feature but it didn't get much attention so far:
https://code.google.com/p/android/issues/detail?id=14266
Some people (like me) falls back using logcat and tries to infer if an outgoing call has been answered but this is far from an ideal solution.
I am also searching for an answer to the same problem, apparently no straight forward method is present to do this. But, I think we can combine a Call Log content observer with PhoneStateListener to get the call duration.
We could set a flag in the shared prefs when an outgoing call is started, if anything changes in call log and our shared prefs flag is true we could get the call duration from the call log to see if the call was ever connected :)
You can Check these Duplicate questions:
How can my android app detect a dropped call?
Detecting outgoing call and call hangup event in android

Categories

Resources