Android - other app stealing my intent receiver? - java

I have an app which uses the ACTION_MEDIA_BUTTON intent with a BroadcastReceiver to control a music player. The user pushes a button on external hardware and it controls the in-app music player. The user can also HOLD DOWN the button and change the volume.
I recently downloaded another app which uses the headset button, and it takes over the media button intent from my app! So when this other app is open and I press the button, the other app will start running, but my app will think that the button is still pressed down so it will cycle the volume.
To summarize,
my app is open, supposed to be sole listener of media button intents
other app gets opened, it also wants to be sole listener of media button intents
button gets pressed with both apps open, control goes to other app
my app thinks the button is being held down, as it lost control as the button was pressed in down mode (I think). It then launches functions I don't want launched because it thinks the user has held down the button.
Is there any way I could make sure that while my app is open it's the sole receiver of this media button intent? Could I at least check to see if another app has taken over, so I can prevent unexpected behaviour?
Thank you for any help, I've never had apps not play nicely before!

You can alter your BroadcastReceiver's priority (make it something large, like 10000): it should then get the Intent first, and then you can pass it on to the other app.

I have a similiar issue. I believe, outside of the 'arms race' over the priorites mentioned, the only real solution is to close the other application. If you are releasing this application to other users, you could possibly give them a message telling them to close other media player apps and services.
In your manifest you can set the intent priority to the max value of an integer which is: 2147483647.
You should not however set your IntentFilter priority over 1000 as it tells you in the API docs. You can set the IntentFilter priority like so:
myIntentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
I assume you should subtract one from the SYSTEM_HIGH_PRIORITY constant as the docs say the value must be less than SYSTEM_HIGH_PRIORITY.
Quote from docs about IntentFilter.setPriority(int):
Applications must use a value that is larger than SYSTEM_LOW_PRIORITY and smaller than SYSTEM_HIGH_PRIORITY.

Related

What happens when a user holds the headset button

I'm trying to find out what happens when a user pushes the media button of headset. I know the phone recognizes holding the button, but when I test it myself and hold the button broadcast will send a ACTION_MEDIA_BUTTON. When I push the button down and release it, I get two broadcasts (one for ACTION_DOWN and one for ACTION_UP). But when I hold it doesn't send anything.
This is how most buttons work for anything. Almost no hardware will tell you when something is being pushed down, just when a change is made.
If you want to know how long a button is held down for, you want to start recording the time using something like System.currentTimeMillis when you receive an ACTION_DOWN message and when you receive an ACTION_UP you can record the end time and then calculate the difference.

Android Activity Lifecycle With/Without Lock-screen

I'm trying to find out what the difference is in the activity lifecycle when a lock-screen is enabled versus disabled.
Scenario A (no lock-screen)
App is running
power button is hit to turn display off
power button is hit to turn display on
App is immediately resumed
Scenario B (lock screen enabled, swipe to unlock)
App is running
power button is hit to turn display off
power button is hit to turn display on
Swipe screen to unlock
App is resumed (but we are getting a bug in the display, which is where we are trying to find the difference)
I have printed out the activity lifecycle for both scenarios and they show up identical for both scenarios.
What might be the difference in the lifecycle that would cause this different behavior when using lock-screen versus no lock-screen?
Take a look at this documentation. It could be that the lock screen would imply that the app process is killed and therefore data is lost (which is necessary to present the page and therefore the app crashes). I would suggest to debug the activity states to find the answer you're looking for.
You can test it with writing Logs into onResume(), onCreate(), onPause(), onDestroy() etc.
Also pressing Power button and and opening LockScreen changes the devices screen orientation into "Portrait" mode(Phones and tablets with locked rotation only). It may cause different reactions you are mentioned about.

Android - Bluetooth Buttons... Media Button Intents or Bluetooth Headset API?

edit
It think all this stuff is now well out of date, there have been many changes to this as android has developed. I'll post an answer when I have worked it out (if noone else has).
I really just want a background app to get first crack at bluetooth messages.
end of edit
I have an android app that needs input from the user - I want to use bluetooth headset buttons (actually bluetooth watch/band buttons - but technically the same!).
I have been looking at media button examples, there are many here and elsewhere which all basically say:-
1) Put receiver/intent in manifest
2) Register receiver in main activity
3) Do processing in the receiver class
This isn't working for me (even cutting and pasting sample code), with no errors, but no events being triggered.
However I also found this:
http://developer.android.com/reference/android/bluetooth/BluetoothHeadset.html
Which is a dedicated bluetooth headset api... has it superceded the media button approach? or is it just higher in the food chain? Should this be used instead of the Media Button Event stuff - or are they just different perspectives?
If the media button stuff is the way, I can post my non-working code, but don't want to waste anyones time if its an out of date approach!
Thanks!
For examples of using bluetooth buttons - this is an excellent example...
https://code.google.com/p/media-button-router/
It detects a hit on the headset 'play' button then pops a dialog asking which app to direct the 'play' to - it plays the choices via the headset so one can be selected without viewing the screen.

How do i start activity when screen goes off?

I have an application which is doing its work only when devices screen goes off. I have set up broadcast receiver (Intent.ACTION_SCREEN_OFF) and it works fine. I got record in my LOGCAT always when devices screen goes off. So far so good.
My onReceive code from ACTION_SCREEN_OFF uses some code calculating stuff, and all executes fine (When screen goes off). So far so good.
At the end of my onReceive code i'm starting new activity, but the onCreate of targeting activity is NOT always executing. (For example on my HTC Desire 2.3.7 works fine. On HTC Wildfire S 2.3.5, Xperia Arc S 2.3.4 it does not execute, and on Samsung Galaxy ACE 2.2.1 , it depends. Sometimes is executing sometimes isn't). My LOGCAT shows that onReceive executed till the end but Activity was not started. I'm using following code to starting this activity:
Intent startH_Activity = new Intent(getApplicationContext(), HandleActivity.class);
startH_Activity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startH_Activity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(startH_Activity);
The important things to note:
I said that on some phones mentioned above it doesn't work. Well its not working immediately, like it should (when SCREEN_OFF fires ). Always activity starts like.. 10-15 minutes after screen went off (which is not acceptable)
When screen goes off and activity does not start , if I press POWER button on device it immediatelyfires off my target activity and app is working like normal. (again this is not acceptable. It should fire automatically).
When ANY device is connected to the PC it works like it should. Activity starts immediatelyafter screen goes off.
After reading a lot of Stack Overflow I realized that this is because MAYBE device goes to sleep. That's why it works like normal if i press POWER button, or Wakes up automatically after 10-15min because it synchronizes or something. So I used WAKELOCK.
//OnCreate Service
powMan = (PowerManager) getSystemService(POWER_SERVICE);
wake = powMan.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
//OnReceive (screen goes off)
wake.acquire();
But still without a success. (Am i doing wrong?) Even wake lock is acquired my activity won't show up on these devices.
Basically what I'm asking. How do i open usual activity when screen goes off? Or at least how to turn screen ON if activity won't start (remember, pressing on power button shows up my activity
I would really need some help now, because I'm getting really frustrated about this. Thank you for help.
You should be starting Service instead of Activity when screen goes off.
I would register yet another broadcast receiver and start the Activity when screen goes on (Intent.ACTION_SCREEN_ON).
Two issues:
1) How do you pass data from one receiver to another? What comes to my mind is either starting a Service or saving the information to a file (or shared preferences).
2) What happens if you have two events that must start an Activity? (E.g. "something started" and "something finished".) You must resolve this, either showing both events or just the latest one.
Note that the user can turn the screen on and off without unlocking the screen (Intent.ACTION_USER_PRESENT), and that if the phone is configured not to lock the screen, Intent.ACTION_USER_PRESENT does not happen.

Android Auto Message Sending

i want to develop an android application to locate a mobile no.Obviously it cannot be done without user approval.
Basically i need the GPS coordinates of the other user.So my idea is to, after selecting a contact from the list,when i click on locate Button a automatic message/notifaction is sent to the other user asking for his/her permission to Allow/Deny.Once the user clicks on the allow button an automatic msg will be sent to the locator giving the longitudes,latitudes of the mobile which i can locate using GPS
If I understood you well, on the receiving end, a Service will be monitoring for Events (on Bluetooth or Wifi) and should popup a dialog requiring user to accept or deny a transaction.
You can quite easily make an Activity that polls for Event (or better, make a bounded background Service Message your Activity when a new Event has been detected) and display a popup AlertDialog. If you design it this way you will need your Activity to be active and running (i.e. displayed).
If you need your application to get user attention even when your application is not displayed, you will not be able to do so: you can not (nor should) directly start an Activity from within a Service. You will have to use Notifications and/or a Toast message to unintrusively notify your user that something requires his/her attention. Notifications appear in the status bar and can be used to launch your Accept/Deny Activity.

Categories