Just wondering if its possible to launch an install application from a background service. I have the packagename as well.
An installed application can be invoked using PackageManager class
startActivity(BackgroundService.this.getPackageManager()
.getLaunchIntentForPackage(packageName)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
Yes, you can launch an activity from a service.
Intent intent= getPackageManager().getLaunchIntentForPackage("com.example.package_name");
startActivity( intent);
For more information you can see package manager and getLaunchIntentForPackage
yes you can launch an activity from a service. use this code this is worked for me
Intent mIntent = new Intent(getApplicationContext(), YourActivity.class);
mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(mIntent);
don't forget to called mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) otherwise its gives error
Related
I'm having a requirement where i will registering few alarm manager to perform specific task and on onReceive() i'm doing the specific task.
As of now this is working only when the task is in background that is not killed.
I'm planning to open a application using package name when the application is not in background on onReceive(). As per my research, it is only possible to open activity from onReceive() but not the application using package name.
If possible could you please shed some light on how to proceed further?
Thank you
Yes, try this
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("package");
startActivity( LaunchIntent );
OR
startActivity(getPackageManager().getLaunchIntentForPackage("package"));
If it is your own app then you can use this
Intent intentone = new Intent(context.getApplicationContext(), "LauncherActivity");
intentone.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentone);
This will work in my case.
let me know if it does for you.
i am launching another application through my application,so i want to check is any instance of that application is already in stack or not? if exist then clear all instance before launch.
Would you be looking for the following flags to add to the Intent you start the other application with:
FLAG_ACTIVITY_CLEAR_TASK
Try adding this flags to your intent
Intent intent= getPackageManager().getLaunchIntentForPackage("com.package.address");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I am new to Android. Say, I open an app and I would like to open another App after clicking a button. How can I accomplish this task? Would appreciate if you can provide me some tutorial on this.
Intent intent = new Intent();
intent.setClassName("**package.name**", "**package.name.LauncherActivityName**");
startActivityForResult(intent,REQUEST_CODE);
You need to know the package and class names of the activity to call
Intent i = new Intent(Intent.ACTION_MAIN);
PackageManager manager = getPackageManager();
i = manager.getLaunchIntentForPackage("app package name");
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
Use this code:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.example.package");
startActivity(launchIntent);
The app you want to launch must be on the device.
Intent appIntent = getPackageManager().getLaunchIntentForPackage("your app package name ");
startActivity(appIntent );
If the other application is a pre-packaged application mean, this tutorial may help you.
If the other application is going to be your application, then you need to learn Implicit Intent tutorials.
Also include the activity of the other application which you are planning to call in the Manifest file of the calling package also.
I am trying to start an activity from a Receiver after the device boot:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(ComponentName.unflattenFromString("other.apps.package.name/.ActivityName"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
context.startActivity(intent);
This code just works fine when I call it from my activity however it fails when my BroadcastReceiver executes it after bootup. My Logcat shows:
ActivityNotFoundException: Have you declared the activity in your AndroidManifest.xml?
Any pointers will be greatly appreciated. Thanks in advance.
Intent intent = new Intent(context, activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
i think this 3 lines only needed and the context will be the context u receive in broadcast receiver.
You probably spelled or made a error when you declared your Activity in your manifest. Make sure you put it in there and spelled everything correctly
I think the problem is in the following Line.what is the name of your Activity?is it "ActivityName"?Also Check package name.
intent.setComponent(ComponentName.unflattenFromString("other.apps.package.name/.ActivityName"))
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);