I have a situation in my current app where on tapping on a dialog fragment I have to restart the current app in background (or take to home activity) and open calendar app at the same time. I tried firing 2 intents at the same time but it didn't work. I also tried to use AsyncTask's executeOnExecutor but the order is not always guaranteed. Can someone please explain me how to handle situation like these?
you have write following code to launch activity first and calendar intent after that:
public void openCalendar(Context mCtx){
Intent activityIntent = new Intent(mCtx, HomeActivity.class);
startActivity(activityIntent );
Intent i = new Intent();
//Froyo or greater (mind you I just tested this on CM7 and the less than froyo one worked so it depends on the phone...)
cn = new ComponentName("com.google.android.calendar", "com.android.calendar.LaunchActivity");
//less than Froyo
cn = new ComponentName("com.android.calendar", "com.android.calendar.LaunchActivity");
i.setComponent(cn);
startActivity(i);
}
Related
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)
Basically I'd like to start the current phone call ui from a button in one of my activities.
So far, I've been able to start a new call using
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:000000"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
or to move to the phonebook using:
Intent intent = new Intent(Intent.ACTION_CALL_BUTTON);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I've also tried to use that code, but it seems not to be working on HTC devices and some Huawei (ri is null).
Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
intentToResolve.setPackage("com.android.phone");
ResolveInfo ri = getPackageManager().resolveActivity(intentToResolve, 0);
if (ri != null)
{
Intent intent = new Intent(intentToResolve);
intent.setClassName(ri.activityInfo.applicationInfo.packageName, ri.activityInfo.name);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
What I'd like to get is move directly to the call ui. The behaviour is the same as in this procedure:
Be in a phone call
Go back to the Home screen
Expand the notification bar
Click on the call row
I don't know how to achievve and how to make it works on all devices.
Thanks for your precious help.
you have to do as suggested on this answer: android.permission.CALL_PHONE for tablets
add the call phone permission:
<uses-permission android:name="android.permission.CALL_PHONE"/>
and then call with Intent.ACTION_CALL.
Intent intentcall = new Intent(
Intent.ACTION_CALL,
Uri.parse("tel:" + phonenumber));
startActivity(intentcall);
edit:
there's a way you could do, but it won't work in Lollipop and forward, as it's been deprecated due to security reasons.
You could use the ActivityManager to get the running tasks and then analyse the content of each RunningTaskInfo to determine which one is the "phone" and then use the task ID to call moveTaskToFront. It possibly work, but as I said, it will not work with Lollipop and future versions.
The following code is launching gps setting screen in samsung device but in htc device it is launching security screen of setting.How can i write code so that it will launch gps screen independent of the devices.Please help is there any alternative solution
final ComponentName toLaunch = new ComponentName("com.android.settings","com.android.settings.SecuritySettings");
final Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(toLaunch);
startActivity(intent);
Get rid of the setComponent() and addCategory() calls and see if that helps. You should not be using those in any case, particularly the ComponentName that hard-wires in invalid package and class names.
I think this code is helpful for you
Intent intent1 = new Intent();
intent1.setClassName("com.android.settings",
"com.android.settings.SecuritySettings");
context.startActivity(intent1);
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.
I know it's kinda hard to understand the question, I'm new to Android, what I mean is that:
say I have an activity A that is currently active, then I put device sleep and wake up the device, activity A still active right now.
At this time a dialog will pop up and I press "Yes", a new activity A will be created. What I concerned is that, how do I kill the old A and then create the new A?
Right now when I click "Yes" the new A is created but it's not showing correctly.
I'm not sure what are trying to achieve, but this code snippet should do what you want.
finish();
Intent intent = new Intent(this, YourActivity.class);
startActivity(intent);
A different alternative is calling
recreate();
As the documentation says: This results in essentially the same flow as when the Activity is created due to a configuration change -- the current instance will go through its lifecycle to onDestroy() and a new instance then created after it."
Try this, hope this will help.
Intent intent = new Intent(this, yourclass.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();