I was searching by myself but failed. Can I start an application like Google translator in the background? Here below-listed program runs the Translator very well, but at the same time, my app goes to the background. I would like to have my app foreground while the Translator is started in the background. Many thanks in advance!
Intent app_to_launch = getPackageManager().
getLaunchIntentForPackage("com.google.android.apps.translate");
if (app_to_launch != null) {
startActivity(app_to_launch);
}
Updated:
Following the subject. One thing is observed. Here below-listed code is working fine:
Intent app_to_launch = getPackageManager().getLaunchIntentForPackage("com.google.android.apps.translate");
if (app_to_launch != null) {
startActivity(app_to_launch);
}
try{Thread.sleep(1000);}catch (Exception e){};
Intent intent = new Intent(MainActivity.this, WordsStatus.class);
startActivity(intent);
But if I remove the 1 sec pause (Thread.sleep) there is no sign that the Google translator has been run. It seems like the launching of the second Intend (my application starting) suppresses the first launch (Google translator starting). If I restore the 1-sec pause everything works as it should be.
Sure just do this:
Intent app_to_launch = getPackageManager().getLaunchIntentForPackage("com.package.of.background.app");
if (app_to_launch != null) {
startActivity(app_to_launch);
}
Intent app_to_launch = getPackageManager().getLaunchIntentForPackage("com.package.of.forground.app");
if (app_to_launch != null) {
startActivity(app_to_launch);
}
(The second app launch doesn't need to use the package manager, you can use anyone of starting an Activity, i.e. if you want to start your own app's internal activity, then just use the normal approach startActivity(new Intent(this, MyActivity.class))
That will start two apps, and the second one starting will move the first one second in the stack.
Note that apps don't really "run in the background" any app that isn't showing on the screen is usually in the paused state, (it may have started a background service to do some work though).
Related
I've been facing a problem for a while regarding startActivity(intent). What i'm developing is a kind of key word detection like "ok google" that triggers an alert when the user says the word. To achieve that when the user is not using the app i have a LifecycleService that runs in foreground and listens to the user. When the user says the word and the app is killed it opens the activity i need using startActivity from the service, but the problem is that if i keep listening and change to other activity (using the app normally) the detection works (because i hear the sound i put when the word is recognized) but the app doesn't start the activity it should (although the startActivity(intent) is called). I'm pretty sure that the problem has to be with the context that maybe is not the correct one when i open the app with startActivity from the service, but i don't know how to fix it. I tried to user some other variables like applicationContext or the androidContext() from Koin but it is not working.
class SpeechRecognitionService : LifecycleService() {
...
//onStartCommand starts the audio recognizer and startAlert() is triggered when the alert is recognized. It is always correctly called
private fun startAlert() {
//This is not showing MainActivity although i execute it
startActivity(MainActivity.getDialogIntent(this))
//I always hear this audio when the app detects word
val audio = MediaPlayer.create(this, R.raw.alert_detected_audio)
audio.start()
}
}
The MainActivity.getDialogIntent(this) is just a common Intent
fun getDialogIntent(context: Context): Intent {
val intent = Intent(context, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.putExtra(SHOW_ALERT_DIALOG_KEY, true)
return intent
}
The problem only happens if the app is started with the voice recognition (after being killed). If i for example kill the app but open the app again pressing the app icon it works correctly. If i start the app with voice (so, from the startActivity i have above) i worked that time and open the app but when i change to other activity it fails to start.
You need to use a proper "launcher Intent". The easiest way to get one is to call
PackageManager pm = getPackageManager()
Intent intent = pm.getLaunchIntentForPackage("my.package.name")
Use this Intent to start your MainActivity instead of calling MainActivity.getDialogIntent(this)
I have a simple Android app that needs to launch another app under certain condition, and I will need to check the condition upon the app launch. That is, either continue to launch my app or just launch another app:
if (A == true) {
launch another activity of another app
leave the current app without creating the main activity
} else {
launch the main activity of the current app
}
Could anyone please let me know how to deal with A == true case? I am able to launch another app's activity but I have trouble leaving the current app without even opening the main activity.
Any help will be greatly appreciated!
You can launch other application using following intent
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.example.abc");//pass the packagename of app you want to open
startActivity( LaunchIntent );
If you don't know the package name of application that you wanted to launch then try your hand on
PackageManager pm;
pm = getPackageManager();
// get a list of installed apps.
packages = pm.getInstalledApplications(0);
Pass the packagename of app you want to open
You can use this if A == true
else You can launch the MainActivity as
startActivity(new Intent(CurrentActivity.this,MainActivity.class));
If you want to start another activity of another app without the normal IntentFilter then the easiest solutions is:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.anotherapp.package","com.anotherapp.package.MainActivity"));
startActivity(intent);
Of course, you would need to know the package name and the Activity name you want to start
As for finishing your application call
finishAndRemoveTask();
I've set my application to be launched at system startup and it does so. The problem is that it takes about ~30 seconds between the homescreen showing up and my app to be launched. This may be a problem if the user doesn't wait.
My idea is to prevent the app from being re-launched when the system broadcasts the message IF it is already running.
So, basically, I'm looking for either solution:
Decrease wait time between system start and app start AND / OR;
Prevent app from being launched when the message is received if it is already running.
The first topic is something I want to achieve regardless, but I don't control when android will fire the message. If I could achieve both, it would be great, but I'd settle for the second option.
I don't think it's necessary to show this, but I've set everything in the Manifest (i.e BOOT_COMPLETED) and this is my code, where MyNamedActivity is my main activity (working code):
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED) && AppController.getUsuarioLogado() == null) {
Intent i = new Intent(context, MyNamedActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
Note: Removing addFlags line causes the app to cash.
I am developing an application which can launch number of other applications. I can successfully launch new applications from my app. The problem is when the launched application crashes, my application is taken back to the previous screen(From the screen where new app is launched).How can i make sure my app stays in the same screen even if the newly launched app crashes.I tried to set the intent flag Intent.FLAG_ACTIVITY_NEW_TASK while launching new app but this didnt work.
Intent LauncherIntent = getActivity().getPackageManager().getLaunchIntentForPackage(package.trim());
if(LauncherIntent != null){
LauncherIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getActivity().startActivity(LauncherIntent);
}
This code snippet is used to launch the new app.The 'package' will have the package info of newly launched app.
Try this:
Intent LauncherIntent = getActivity().getPackageManager().getLaunchIntentForPackage(package.trim());
if(LauncherIntent != null){
LauncherIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getActivity().startActivity(LauncherIntent);
finish();
}
Here is my problem -
I copied my .apk file onto phone memory card and launch my application clicking on it and it allows me to install my application.I install my application.Finally,I got system installation pop up containing two options "Open" and "Done".When i click "Open" my application got launched.Up to this point everything is working without any problem.
Now in my application I click on a button and some download is taking place as a result(Showing progress dialog).Now I press a Home button,so my application goes to background.
Now I again launch my application by going inside Menu and clicking on my application icon.
Expected result - Still I Should see Progress Dialog for downloading.
Actual result - A new instance/session of my application is getting started.
So how to avoid this so that only one and one instance/session of my application should run.
#Palejandro, here you are. Put the code below into your main activity onCreate() method:
// Possible work around for market launches. See
// http://code.google.com/p/android/issues/detail?id=2373
// for more details. Essentially, the market launches the main activity
// on top of other activities.
// We never want this to happen. Instead, we check if we are the root
// and if not, we finish.
if (!isTaskRoot()) {
final Intent intent = getIntent();
final String intentAction = intent.getAction();
if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) {
Log.w(TAG, "Main Activity is not the root. Finishing Main Activity instead of launching.");
finish();
return;
}
}
I used this piece of code in my projects and it works fine!
I believe you need to put
<activity
android:launchMode="singleInstance"
</activity>
in the manifest file.
what do your OnPause, OnResume and OnCreate?
I will bet money you are not saving anything OnPause, and starting a new instance all the time via OnCreate.
You should read the notes on Activity Lifecycles.
If you haven't got this sorted yet, I would say your app is actually being killed when home is pressed, or perhaps you have a bug that doesn't latch onto whatever object is keeping state.
// put below code in your launcher activity before call super and setcontentview()
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
// get the info from the currently running task
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(10);
boolean alreadyTask=false;
for(ActivityManager.RunningTaskInfo info : taskInfo){
ComponentName componentInfo = info.topActivity;
String value= componentInfo.getPackageName();
if(value.contains(getPackageName()) && !info.topActivity.getClassName().contains(getPackageName()+".LauncherActivity")){
alreadyTask=true;
Log.i(TAG, "second instance found!!!");
break;
}
}
if(alreadyTask){
finish();
}
I don't have a solution but the problem is that the intent used to start the app is different when you open it directly from install compared to opening it from your home screen. Since it will get started by two different intents it will open a new instance the second time round.
A quick work around is to avoid pressing "Open" when you have installed the application. Press "Done" and then find the application yourself.
See: http://code.google.com/p/android/issues/detail?id=2373