I have a BroadcastReceiver and my goal is to detect a long press on the headset button. Tried different versions and none worked. So now I'm trying to check the actions received. If I click normally, I can see the actions 0 (KeyDown) and 1 (KeyUp). If I click and hold, nothing shows up, for either key down or up. What's wrong?
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
Log.e("action",""+event.getAction());
...
}
}
I can post more code if required, but I didn't think it was relevant
Running on an S4 with Android 4.3. Not sure if it matters, S-Voice is frozen, Google Now is disabled. And I've noticed that if my screen is off and I long click, I get a menu "Handle long press with Google Search / AutoVoice"
I think you can take control over long press with this bit of code in manifest. This will open up a dialog which lets you choose which app to use long press. It worked for me when phone is unlocked but it wont work when the phone is locked. It pops out dialog but it vanishes when you unlock the phone. Hope I can get some help with this.
Key is to open up "Activity" instead of implementing broadcast receiver.
<activity
android:name="packagename.activityname"
android:launchMode="singleInstance"
android:showOnLockScreen="true">
<intent-filter android:priority="2147483647">
<action android:name="android.speech.action.WEB_SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter android:priority="2147483647">
<action android:name="android.speech.action.VOICE_SEARCH_HANDS_FREE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="com.sec.action.SVOICE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
I have tested this on Kitkat. No idea what happens on previous versions.
Related
I need to catch longpress event of media button even when the device is locked or the app is in background. onKey listener does not work for apps in background. I have a working example with BroadcastReceiver, but it only works for short press. I am using AudioManager to register the receiver. What i can see in console when i press the button for a long time is this:
I/MediaSessionService(684): voice-based interactions: about to use ACTION_WEB_SEARCH
I/ActivityManager(684): START u0 {act=android.speech.action.WEB_SEARCH flg=0x10800000} from uid 1000 on display 0
So i have tried listening to ACTION_WEB_SEARCH by context.registerReceiver() method with no success. I also added receiver with intent-filter into manifest file.
Is there any other way i could try? Or does anyone know about a working solution? I have been searching and testing this one functionality for 5 days now and i am getting really angry. =D
I found the answer for the exact same issue you describe here.
All you need to do is add intent-filter to an activity you want to start by long pressing the media button.
WEB_SEARCH is called when the screen is unlocked.
VOICE_SEARCH_HANDS_FREE is called when the screen is locked.
Manifest file with added actions to the MainActivity:
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:screenOrientation="portrait">
<intent-filter android:priority="1000000">
<action android:name="android.speech.action.VOICE_SEARCH_HANDS_FREE" />
<action android:name="android.speech.action.WEB_SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The first time you launch your app and long press the button a small dialog box on the bottom of the screen will appear. The dialog box asks if you want to complete the action with the Google app or your own app, they both listen to the same actions.
Don't forget to add this in your Activity or it will not show on your lockscreen.
#Override
public void onAttachedToWindow() {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
}
So I have such tricky thing declared in Manifest to be able to start my app from a browser:
<activity android:name=".BrowserIntentCallBackActivity"
android:noHistory="true" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="*"
android:scheme="trickyapp" />
</intent-filter>
</activity>
this workds ok and starts my BrowserIntentCallBackActivity as expected. If my app wasnt previously started then finishing the BrowserIntentCallBackActivity returns user back to browser and its fine.
However if my app was previuosly started and is running on background finishing the BrowserIntentCallBackActivity returns user to previuos activity of my app. But I want to send user back to browser where he/she clicked the very special link and from where my BrowserIntentCallBackActivity was launched by intent. And I dont want ot kill my running app.
How to achieve this?
Try setting the launch mode of your activity:
https://developer.android.com/guide/topics/manifest/activity-element.html#lmode
I believe the correct mode for your task will be "singleTask" or maybe "singleInstance", although I have limited experience with those so you will have to experiment to be sure.
I do not know if you get my question right, so let me explain:
My app is shown in the chooser after user clicks a web link in any other app, then user clicks on my app and my app shows the link in a webview. This works fine.
But, the problem is when the user launches the other app again (which invoked my app earlier) with a click on the icon that is on the home screen or app drawer, it opens my app again instead of the origin app.
Manifest:
<activity
android:name="com.droidfox.app.browser"
android:label="#string/browser"
android:theme="#android:style/Theme.Black.NoTitleBar"
android:launchMode="singleTop"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:scheme="https" />
</intent-filter>
</activity>
Back Button Override:
#Override
public void onBackPressed ()
{
moveTaskToBack(true);
}
Does anyone have a clue why the other app launches my app and how can I fix this?
Try to set android:noHistory for your given activity.
Taken from documentation:
Whether or not the activity should be removed from the activity stack and finished (its finish() method called) when the user navigates away from it and it's no longer visible on screen — "true" if it should be finished, and "false" if not. The default value is "false".
A value of "true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it.
If you want to start Google Now on Nexus devices, you swipe-up with a simple gesture from the navbar. I want go replace this feature with my own app. if you swipe the, app-picker comes and asks you which app do you want to start: my app or Google Now. How can I implement this?
I have found an example which runs without root.
Add the following <intent-filter> to the Activity you want launched from the gesture:
<intent-filter>
<action android:name="android.intent.action.ASSIST" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
You will now see an option for your app when you make the swipe now, and will have to select it as the default handler by selecting Always, as shown below.
It's just a matter of setting the correct intent filter:
<intent-filter>
<action android:name="android.intent.action.ASSIST"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
I need my app to be started automatically once after the phone reboot and Power-on.
I used the code provided at AutoStart an Application at boot up and now my android app starts automatically after the phone reboot (restart).
Now, consider that instead of doing a phone reboot, I have used Phone power off (Shut phone down) option. After the phone power on, my app is not started automatically as expected. Can you please explain what I have missed.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:enabled="true" android:name=".BootUpReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
public class BootUpReceiver extends BroadcastReceiver
{
private static SharedPreferences aSharedSettings;
#Override
public void onReceive(Context context, Intent intent)
{
aSharedSettings = context.getSharedPreferences("MYPreferences", Context.MODE_PRIVATE);
boolean isUserLoggedIn = aSharedSettings.getBoolean(kEY.AUTHENTICATED, false);
if(isUserLoggedIn)
{
Intent aServiceIntent = new Intent(context, HomeView.class);
aServiceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(aServiceIntent);
}
}
}
Thank You.
Does it work as intended if you launch the application manually at least once before rebooting the phone, and not 'Force-close' it?
Have a look at:
Android : android.intent.action.BOOT_COMPLETED on ICS and Gingerbread
Boot Completed Regression Confirmed
There are a few things you can try.
Firstly check that your app installLocation in the AndroidManifest.xml is set to android:installLocation="internalOnly" this makes sure that the app is on the local storage. Apps installed to the sdcard will not receive the BOOT_COMPLETE intent.
Also I would remove <category android:name="android.intent.category.DEFAULT" /> it is not necessary.
And the last thing you can try is using a full package name:
<receiver android:enabled="true"
android:name="com.myapp.receivers.BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Try to add
<category android:name="android.intent.category.LAUNCHER" />
instead of
<category android:name="android.intent.category.DEFAULT" />.
And also check the value of isUserLoggedIn.
Are you using a HTC device? If so, you may have a feature enabled called "Fast-Boot"
See this link for details.
Detect if HTC "Fast boot" is enabled
the name of the Activity from where you are starting your application, Add this line in your tag.....And let me know it worked or not
<category android:name="android.intent.category.HomeView" />