Is it possible to start an Activity from a Service? If yes, how can we achieve this?
android.app.Service is descendant of android.app.Context so you can use startActivity directly. However since you start this outside any activity you need to set FLAG_ACTIVITY_NEW_TASK flag on the intent.
For example:
Intent i = new Intent();
i.setClass(this, MyActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
where this is your service.
Even if the framework allows you to start an Activity from a Service, it's probably not a proper solution. The reason is that the Service task may or may not be the focus of the user at the time the Service wishes to interact with the user. Interrupting what the user is currently doing is considered bad design form, especially from something that is supposed to be operating in the background.
Therefore, you should consider using a Notification with Notification Service, which carries a PendingIntent to launch the desired Activity when the user decides it is time to investigate. Think of it as delayed gratification.
I had a problem starting an activity from a service, it was due to the missing FLAG_ACTIVITY_NEW_TASK intent flag.
This surely will solve your problem
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName cn = new ComponentName(this, TaxiPlexer.class);
intent.setComponent(cn);
startActivity(intent);
Related
I use Code A to start an activity with the Flags FLAG_ACTIVITY_CLEAR_TOP, will all other background Apps such as Message, Gmail,Google Map be cleared and released?
Code A
Intent intent = new Intent(mActivity, ui.UIHome.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mActivity.startActivity(intent);
No, those are other tasks. Clear top will only have influence within your own task
I'm trying to bring my app from background to foreground. In onHandleIntent() of my custom IntentService class, I have:
Intent intent = new Intent();
intent.setClass(getApplicationContext(), MainActivity.class); // Also tried with "this" instead of getApplicationContext()
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
Now this code works at first sight but I found a scenario where it doesn't work. If you have the app opened and you put it to background via home button and execute startActivity() within ~5 second, there will be a delay before your app will come to foreground. This is a known implementation and you can find the topic discussed on stackoverflow. In this scenario, the app succeeded in coming from background to foreground.
If you repeat the same experiment above, but instead of waiting for the app to come to foreground, go browse (scroll, swipe, etc) around your phone (I was browsing around the google playstore). The result is that startActivity() will get called but the app will not come to the foreground.
I'm not asking for a solution but more of an explanation on why this is happening. Is this intended behavior?
Use the context of your class.
For instance :
Intent intent= new Intent(context, other.class)
Instead of getapplicationContext()
Use the code :
private void startMenuActivity() {
Intent i = new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
}
the below code works for me,
val login = Intent(applicationContext, SignInActivity::class.java)
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
login.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
applicationContext.startActivity(login)
I have a PreferenceActivity with a custom DialogFragment for clearing the app data, so I want it when the user clicks Yes, the application to either close completely (finish all activities) or just to tell it to go to the InitialSetup activity, so the app ca be set up again anew. So far I havent been able to do it in any way...
Tried with
Intent intent = new Intent(context.getApplicationContext(), MySettings.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(intent);
but that still does not close all the activities in the back stack...
How can I do it?
What you need is FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK flags for your intent:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Or as mentioned Sartheris use IntentCompat for Android APIs below 11:
Intent intent = IntentCompat.makeRestartActivityTask(new ComponentName(context, MySettings.class));
startActivity(intent);
If you only want the app to close, with the user having to start it again later, you can use
System.exit(0);
as you would in ordinary Java.
If you want your app to restart automatically after it's closed, use a PendingIntent as the answer for this question does:
how to programmatically "restart" android app?
Is it possible to create two entry points to an application in Android, I mean can I switch the main activity programmatically?
Every exported activity is a potential entry point into your app; a foreign app can start any of them with an intent. (An intent-filter comes with an implicit android:export.) You can however only have one entry point that the launcher will respect. To simulate a second launch-point, either
Provide a completely separate app with the purpose of starting one of your exported activities, or
Give your 'launch' activity the sole purpose of immediately starting one or another activity based on some logic (a saved preference, a phase-of-moon calculation, anything).
check this one below
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName(packageName,mainActivity));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(intent);
I think you are talking about launching activity decision based on some events, then you need to add a broadcast receiver, like by clicking on app icon on launcher if you want to start Activity1. then add intent filters to this activity Action_MAIN and ACTION_LAUNCHER, if you want to start Activity2 on phone boot up, then add filter to this activity, BOOT_COMPLETED.
If you are talking about to launch other apps from your apps then this can be the code:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName(packageName,mainActivity));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(intent);
I want to develop an application where it will start on particular time & close on particular time. Similar like Alarm but not alarm application.
As per my thinking I will start an service when application first started service will check current time & particular timing to match the condition & when condition is to close application it will simply send application to background so that service will be running & when condition for wake up occurs service will bring application front.
Is this possible? If yes please give an example links or anything helpful.
Also I am trying to understand the service but it's little bit complex for me so if you have link which will help me to understand the service it will be very helpful. Thank You.
Problem Solved:
/*To close the activity/
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(intent);
MainActivity.this.finish();
OR Just finish activity;
MainActivity.this.finish();
/** To start the activity*/
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName cn = new ComponentName(this, MainActivity.class);
i.setComponent(cn);
startActivity(i);
It worked for me still I if any one have better solution please post below.
Thank You
You can create a BroadcastReceiver to listen for time changes, and at your specified times, create an Intent to launch or to close your main Activity.