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
Related
I have an app that may run in the background. While it is running, it may bring a certain activity to the front. However, when the app brings the activity to the front, if the app is currently running at background, I do not want Android to bring the app itself to the front, just do it at background. The reason being is I do not want my app to interrupt what the customer is doing at the moment.
The following is my code that launches the activity.
Intent intentLogin = new Intent(getApplicationContext(), LoginActivity.class);
intentLogin.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentLogin);
The issue with the above code is every time when it is called, it always bring my app to the front even the app is running at background, that is, annoy my customer.
My question is, is there a way that I can quietly bring the activity to the front? If my app is running in front, thats great --- the customer see the new activity straight away. If my app is running at background, I want to quietly bring the activity to the front when app is running in background, and next time when my customer resume the app, they will see the new activity I brought forward.
How can I do that?
Thank you very much!
如果我没理解错的话:
App background and usual player.
When user logged out , you can clear the user data saved in local and show a notification.And when the notification is clicked, start LoginActivity.
App background and unusual player.
When the notification shows ,the user do not click it instead of run the app.You can check the user data whether existing or not by onResume()in BaseActivity.If not ,start LoginActivity.
App foreground.
clear user data and force to start LoginActivity.
If you don't like the solution above(the best user experence I think),try this:
In onCreate() in the specific Activity ,use moveTaskToBack(true) to hide the intent.But remember to check whether the app is running background or foreground.you can refer to this.
If understand correctly, I suggest this:
User leaves your app, and when user come back to your app you want to show another activity instead of .MainActivity
define another activity as Main and with NoDisplay theme.
<activity
android:name=".MainActivity"
android:theme="#android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
then define your normal activities. In MainActivity class define a static variable. for example
public static boolean appRun = false;
onCreate() of SecondActivity change its value to true.
class SecondActivity extends Activity{
#Override
protected void onCreate(Bundle Saved){
super.onCreate(Saved);
MainActivity.appRun = true;
}
}
and onPause set this.finish(); to avoid not resuming to SecondActivity and force to start from MainActivity.
then onCreate() or onResume() of MainActivity check it s value and navigate to another activity.
if(appRun == true){
/*it means user went to SecondActivity and you want to display another*/
startActivity(new Intent(context, NewActivity.class));
}
else{
//this is first time so navigate to SecondActivity
}
Also If you can use Fragments this solution can work for that too.
I'm creating a sample lock screen application in this i must override the home button, after i researched in both google and stackoverflow i got the result, it's complicated to do it. Here i mention what i did in my app,
Created a service with broadcast-receiver to show my lock screen when the screen goes to off. - working fine.
To override the home, menu, back and search buttons i used the following code,
hope we can override the home button when the application only becomes a launcher so in my manifest.xml i added this code.
<intent-filter>
<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.LAUNCHER" /> -->
</intent-filter>
Also in my Activity i used this code too
#Override
public void onAttachedToWindow() {
// TODO Auto-generated method stub
this.getWindow().setType(
WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG
| WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
So far in my sample application i successfully completed the above to. Now my problem is,
When i unlock the screen then go to any apps, then i click the Device home button, My Lock screen will appear. i tired to disable this but i don't know how can i exactly do this, for this i used some code like below,
/* This should come from a preference that let's the user select an activity that can handle the HOME intent */
String packageName = "com.android.launcher";
String packageClass = "com.android.launcher2.Launcher";
Intent home_intent = new Intent(Intent.ACTION_MAIN);
home_intent.addCategory(Intent.CATEGORY_HOME);
home_intent.setComponent(new ComponentName(packageName, packageClass));
home_intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
/* Here you should catch the exception when the launcher has been uninstalled, and let the user save themselves by opening the Market or an app list or something. Users sometimes use root apps to uninstall the system launcher, so your fake launcher is all that is left. Might as well give the poor user a hand. */
startActivity(home_intent);
No code will help me, my exact need is once i unlock the screen i need to show the default home screen until the screen goes to screen off. is any idea to handle this issue? Thanks in Advance.
Try this solution,
Create a static variable flag which is set to true when you receive the broadcast for when screen goes to off
now in your activity check if the flag is true
#Override
public void onAttachedToWindow() {
if(MyService.Flag == true){
//Continue with your code ...
//....
}else{
finish();
}
}
or do it on onCreate which ever is suitable for you
Once your screen is unlocked then
//Disable the flag
MyService.Flag = false;
Now when your user clicks the Home button the activity is called and check again the flag again and if its false then call the finish() to close the activity
I have a registration form in android wherein the user can enter data from it. The user can search for Clinic. After that searching, the user submitted the record, and logout. If the user doesn't have no business with that app, the user can exit BUT when the user pressed the Exit button, it does not exiting, instead it goes to the Searching activity all over again even if the user is logout already. Can someone help me figure out why I can't finish my activity?
Search Activity
btn_Close.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
Intent myIntent = new Intent(getApplicationContext(), RegForm.class);
myIntent.putExtra("from", "Search_Cancel");
startActivity(myIntent);
Search.this.finish();
}
});
Android Manifest
<activity
android:name=".Search"
android:noHistory="true" >
</activity>
Activities are finished with the this.finishActivity(Activity.RESULT_OK); call.
Note: You may also specify other results located in the Activity class as static finals.
finish() will make your active Activity pop from the Back stack but your others Activities will remain on that so if you want to exit whole app you should use Flag FLAG_ACTIVITY_CLEAR_TOP when you are starting your Last Activity to Clear your Back Stack
there is many similar questions of how to resume activity from notification.
But my case is slightly different.
I have an activity which should only be launched from other activity, and never from home or applications list (let me call this "standalone" mode).
This activity is designed to upload file to the server and handles only one intent:
<activity android:name="FileUploader"
android:label="..."
>
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<data android:mimeType="*/*"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
So it could be launched, for example, from Gallery or Camera by means of "Share file". But not from applications list, since there is no way to pass file which should be uploaded and I don't want to add any UI for its selection: I want to keep application as simple as possible.
Here is small example: launch Gallery from Home screen, then select photo, tap share and select my FileUploader from the list.
Activities stack is: GalleryMain > Gal2 > ... > FileUploader
Now if I tap Home key - I return to Home screen and after tapping of Gallery again, I return directly to my FileUploader - this is exactly what I need, so this is ok for now.
Now keeping in mind than upload operation takes time I add service which is actually implements upload: FileUploaderService. I also want to add notification which shows current upload progress when (and only when) user leaves FileUploader activity by pressing Home key.
Tapping on this notification should return user to the FileUploader activity, resuming it in case if it is already launched, or starting it again if activity was finished by the system. !But! resuming or starting it in context of "parent" activity (Gallery in example above), since creating it in its own context leads to unwanted behavior: FileUploader icon becomes to show in Recent Apps list, allowing user to launch it in "standalone" mode.
So 2 cases:
1. Resume if already launched
2. Recreate if was destroyed by system.
I have found I way (maybe this is dirty hack, but it works) to implement first:
String className = FileUploader.class.getCanonicalName();
Intent notificationIntent = null;
mAM = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
Iterator<ActivityManager.RunningTaskInfo> iter = mAM.getRunningTasks(1000).iterator();
int pid = -1;
while (iter.hasNext()) {
ActivityManager.RunningTaskInfo ti = iter.next();
if (className.equals(ti.topActivity.getClassName())) {
pid = ti.id;
break;
}
}
if (pid != -1) {
Iterator<ActivityManager.RecentTaskInfo> iter2 = mAM.getRecentTasks(100, 0).iterator();
while (iter2.hasNext()) {
RecentTaskInfo ti = iter2.next();
if (pid == ti.id) {
notificationIntent = ti.baseIntent;
}
}
}
With this code I'm getting intent which was used to launch "parent" activity (eg. Gallery) and then set my notification with this intent. So it resumes Gallery just like from Home screen leading user directly to my activity, which is currently on top of the stack.
But what to do with 2: how to relaunch "parent" Activity (moreover, I even don't know how to find out which activity it was) and recreate its stack to state it was before activity was destroyed?
All "parent" activities are third-party, so I can't change their code.
Any suggestions?
Well, the only way I found to implement this is:
android:excludeFromRecents="true" android:launchMode="singleTask"
This is not 100% of what I want (eg. I can't return to "parent" application) after resume, but is better than nothing.
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.