How can I stop sound in Android when home button is pressed - android

I have an app and a music in all activity of my app
I would like that, when user press button home, the sound stops like all game that you can download from android market.
How I can do that?
When user press home button a new intent is fired but android framework prevents to catch the main intent so I can't use a broadcast receiver with this action
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
One solution is to stop music onPause and I restart onResume but in this solution the user hear a second pause when switching from one activity to the next, is not good
I search this solution on many forum but unsuccessfully
P.S.
the android framework don't allow to catch the home key onKeyDown event
the android framework don't allow to catch the intent launched when user press on home button

You can try puting the code in onUserLeaveHint() method, it belongs to Activity and is called when you press the Home button.

Related

Start activity from deeplink and on back press resume from latest running activity

I am handling deep links in my app. When a link in the email is clicked, it opens related activity in the app. On back press, it either goes back to email or home (up to intent flags i use).
I need it to go back to latest running activity(if the app was being used before clicking the link in email) or(else) go to first activity to restart the app.
To be clearer: User is on activity C. Email notification comes, checks it and clicks the link inside. It opens up activity E. Here, if user back press, I want to end current task and resume activity C - if activity C task has not been killed by the system. If killed, go to activity A.
Without intent flags, it creates a new task(second app instance) and on back press it goes back to email client.
With NEW_TASK flag, a new tasks starts. If I use CLEAR_TASK flag with this, on back press it goes home.
Manifest
<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="https"
android:host="www.mysite.com"
android:pathPattern="/mypath/*" />
</intent-filter>
FirstActivity
if(getIntent().getData().toString().contains("keyword")){
//intent.setFlags(...);
intent.putExtra("myextra", getIntent().getData().toString());
}
startActivity(intent);
finish();
EmailResultActivity
String data = getIntent().getExtras().getString("myextra");
To achieve this you need to handle deep-link from a common activity. (Eg: If your app has a common activity say XYZ. Then every deep-link should come to XYZ activity and then according to parameters of deep-link, you should move to respective screen) Also you need to make XYZ as singleTask.

Android service receives/intercept "home long pressed" event (similar to "Home Button Launcher")

I am developing an app which needs to receive/intercept event of long pressed home button. I believe this function can be implemented by broadcast receiver intent filter because an existing app called "Home Button Launcher (https://play.google.com/store/apps/details?id=com.dynamicg.homebuttonlauncher&hl=en)" but I can't find any relevant discussion about how to do it.
When this app is installed and the home button is pressed for a long time (3~5 secs), Android ask users to choose which application need to be launched (either the default Google search or the installed Home button launcher), as shown in https://play.google.com/store/apps/details?id=com.dynamicg.homebuttonlauncher&hl=en
There are many posts discuss about how to get home button event in applications for knowing when their apps are closed by users but there is no post about how to get this broadcasted event in service.
Does anyone know how I can receive/intercept this event as what Home button launcher does?
Thank for the remind posted by Selvin. The only thing is to add this intent filter in manifest file:
<intent-filter>
<action android:name="android.intent.action.ASSIST" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>

Trying to make home replacement app?

I'm trying to make a Home Replacement app, but I'm running into a bunch of glitches. When the app launches for the first time, you go through several setup screens that allow you to configure basic settings. Once you are done with that, you get to the HomeScreen activity. In the AndroidManifest.xml I have included the following:
<activity android:name="HomeScreenMain"
android:theme="#style/Theme"
android:launchMode="singleInstance"
android:stateNotNeeded="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
In the HomeScreen activity, I have included the following methods:
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (Intent.ACTION_MAIN.equals(intent.getAction())) {
getWindow().closeAllPanels();
}
}
public void onDestroy() {
super.onDestroy();
}
Also in the HomeScreen activity, I have a button that effectively exits the entire app. The associated code is:
public void exitApp(View view){
this.finish();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
So basically what I want is that when you get to the HomeScreen activity the first time, a prompt comes up telling you to select your default Home Screen (this doesn't happen unless I press the Home Button, I want this to happen as soon as the activity is launched). Once I do set this as my default Home Screen, it works, but only fundamentally. Pressing the home button brings me back to this activity (as it should), but when I tap the Exit button, I don't get returned to the stock Home Launcher, which is what I want.
First, your Manifest is ok,
but in your exitApp you want to finish the activity, right?
in your code you finish it and then start it again..
Pressing the home button brings me back to this activity (as it should),
but when I tap the Exit button, I don't get returned to the stock Launcher,
which is what I want.
if a user had installed another home replacment app (e.g. Go Launcher Ex)
and if the user had set Go Launcher as default before defaulting to your app,
you want to return to Go Launcher Ex, right?
I assume yes.
This is partially possible,
what you can do is prompting the user which home launcher to use
after exiting your launcher:
import android.content.pm.PackageManager;
public void exitApp()
{
//call this method to exit _CLEARLY_,
//and prompt the user which launcher to use next
//clear the default for your app (to show the prompt when exiting)
final PackageManager pm = getPackageManager();
pm.clearPackagePreferredActivities(getApplicationContext().getPackageName());
//exit _CLEARLY_
//calling finish(); would be ok also,
//but there would stay a 'zombie' in the dalvik cache
//and 'zombies' only use up your memory, so kill your entire app:
android.os.Process.killProcess(android.os.Process.myPid());
}
So basically what I want is that when you get to the HomeScreen
activity the first time, a prompt comes up telling you to select your
default Home Screen (this doesn't happen unless I press the Home
Button, I want this to happen as soon as the activity is launched).
then call this function in onCreate(),
it simulates a homebutton press by calling an intent with Intent.CATEGORY_HOME:
public void showPrompt()
{
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
startActivity(i);
}
Hope this is what you wanted

Overriding Home button for a Car Home replacement app

I have been working on a replacement for the stock Car Home app for a bit, and I am completely stumped on how to override the Home button so that my app will be brought back to the foreground whenever the phone is docked. This is the way that Car Home works, so there must be a way.
It seems that BroadcastReceivers won't work, because the intent that is broadcast whenever the Home button is pressed will still cause the default homescreen app to launch; I cannot prevent it. I can override the Home button from within my app, but that does me no good since this needs to work when the user is outside my app. Car Home also does not do anything weird like set itself as the default homescreen app while it's running (I checked the logcat to make sure).
What can I try next?
Well, after many months I have finally found the answer to this question. The key is the "android.dock_home" metadata element, found here:
http://developer.android.com/reference/android/content/Intent.html#METADATA_DOCK_HOME
By using this in your AndroidManifest.xml, you can make your dock application become the home application temporarily. To do this, add this line to the AndroidManifest.xml inside the Activity tags for the dock app activity:
<meta-data android:name="android.dock_home" android:value="true" />
If the value is set to true, as long as your phone is docked the Home button will return you to the dock app. After undocking, the Home button will take you back to your normal home app.
Unfortunately, there is no way in the public APIs to override the Home button without the user confirming it.
Your best bet would be to implement a CATEGORY_HOME Intent. This means when a user pressed Home they would be presented with the option to run the standard Home or yours and make yours the default if they wanted.
When your application was launched you could then check if the phone was docked. If the phone is not docked you could then open the standard Home screen and close your app before anything is displayed.
You need to the correct intent filter in your manifest for the app to launch automatically when you dock the phone. Refer to http://developer.android.com/reference/android/content/Intent.html#CATEGORY_CAR_DOCK for the information.
I found a way to tackle HOME key. For your application set the manifest as
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY"/>
Now your application is an alternate Launcher application.
Use the adb, and disable the launcher application using package manager
pm disable com.android.launcher2.
Now the Home key press will aways stay in the same screen.

overriding the Home Key Long press in a category.HOME activity

I just created my own "Home" to replace the stock android one or Sense.
All is working fine and I get all I want. My only problem is to replace to long press on home key ( that usually show the last 6 activities you launched) by my own launcher.
I successfully replace the long press on MENU button with this code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//Log.i(TAG,"Keycode: "+keyCode);
if (keyCode == KeyEvent.KEYCODE_MENU) {
// this tells the framework to start tracking for
// a long press and eventual key up. it will only
// do so if this is the first down (not a repeat).
event.startTracking();
return true;
}
(...)
and this part part for the long press:
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
//Log.i(TAG,"LONG"+keyCode);
Toast.makeText(Launcher.this,"LONG "+keyCode, Toast.LENGTH_SHORT).show();
if (keyCode == KeyEvent.KEYCODE_MENU) {
(...)
But the problem is that I wasn't able to replace the KeyEvent.KEYCODE_MENU with KeyEvent.KEYCODE_HOME
is that something locked in the code that avoid user to use a Home long press?
Thank a lot for all the information you woulg give me.
Everything I have ever read states that this can't be done... Here is a post on Android Beginners where I asked a very similar question:
http://groups.google.com/group/android-beginners/browse_thread/thread/d8cdcd1c52d79ef1/0f4b184da6f248a9?lnk=gst&q=home+key#0f4b184da6f248a9
However, I have recently come across an app that successfully allows you to launch it by double-tapping the home key so there has got to be something that can be done. I looked into that approach for a while but couldn't get it to work. Now that I know someone else figured it out I'm going to take another stab at it....
EDIT
While overriding a long-press of the home button cannot be done, I have found a way to successfully implement a double-press of the home button. The general idea for this is as follows:
Make your app act as a home replacement app (Look at the sample home app in the SDK samples)
Allow a way in your app to specify a home app to use (it is pretty straightforward to present the user a list of home-replacement apps)
On the first press of the home button start a timer.
If the timer times out, launch the home application
If the user presses the home key a second time before the timer stops, launch your app
Essentially, the home-replacement activity does nothing more than either launch the real home app specified by the user or launch your app... It never displays its own UI.
I have found that this works pretty well, and actually have an app published in the Android Market that does this. If you would like to see it in action, it is called "Quick Launch" and the publisher name is listed as "MagouyaWare"
Hope this helps!
You can register fake activity for the long press HOME button
by adding to manifest:
<intent-filter>
...
<action android:name="android.intent.action.ASSIST" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
I found a way to tackle HOME key. For your application set the manifest as
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.HOME" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.MONKEY"/> Now ur application is an alternate Launcher application.
Use the adb, and disable the launcher application using package manager
pm disable com.android.launcher2.
Now the Home key press will laways stay in the same screen.
Actually there is not much code.I will try to explain it if it helps.
For the andriod application in the manifest file, we generally keep the intent filter as:-
Instaed we should make the intent filter look like:-
This is the same intent filter as that of LAuncher.
Now we have an alternate launchjer application. To make it the only launcher application we have to unistall/disable the existing launcher application(default launcher in android).
For this we need to connect the device/emulator and start the adb(android debug bridge).
Then follow the below steps:-
adb shell
pm list packages //This will list all the packages installed
pm disable com.android.launcher //This will disable the launcher application.
Reboot.

Categories

Resources