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.
Related
I am working on a launcher application for Android Wear, and would like to include icons for the default actions such as "Set Alarm", "Set Timer", "Show Alarms" or the Stopwatch.
I found the way to start alarm clock related activities using the following intents:
AlarmClock.ACTION_SET_TIMER or "android.intent.action.SET_TIMER"
AlarmClock.ACTION_SET_ALARM or "android.intent.action.SHOW_ALARMS"
AlarmClock.ACTION_SHOW_ALARMS or "android.intent.action.SET_ALARM"
I can start them by:
Intent i= new Intent(AlarmClock.ACTION_SET_TIMER);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
context.startActivity(i);
If started without any extras, they simply bring up the corresponding UI. Perfect!
Except, I can't find the corresponding action for the Stopwatch. Do you know the intent? Where should I look?
Use #promanowicz 's answer like this:
Intent intent = new Intent("com.google.android.wearable.action.STOPWATCH" );
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
startActivity(intent);
Maybe this will help someone. At the moment there is intent action 'com.google.android.wearable.action.STOPWATCH' which you can use.
According to this training- Adding Voice Capabilities
The starting and stopping Stopwatch is not exposed via public API or intent
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
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 have my app running in the background and I want the app to be shown on the top(launched) of the android phone when the code below is ran. (I know the code is ran for sure)
This seems like a simple thing but I spent a couple hours on this site and everyone seems to be suggesting something like this:
Intent intent = new Intent(myActivity.this, myActivity.class);
startActivity(intent);
However, it is not bringing the app to the front and launching it.
I got it to work from a PendingIntent launched from a notification. Which I done by the code below. But I want the app to launch by itself without the user clicking on the notification.
Intent intent = new Intent(myActivity.this, myActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, REQUEST_CODE, intent, 0);
notification.setLatestEventInfo(this, "title", "msg", contentIntent);
I also tried:
Intent intent = new Intent("android.intent.action.MAIN");
startActivity(intent);
and flagging the intent:
intent.setFlags(Intent.FLAG_FROM_BACKGROUND);
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
But doesn't seem to do anything, any help appreciated.
You should be able to call your own application like this:
Intent intent = new Intent("android.intent.category.LAUNCHER");
intent.setClassName("com.your.package", "com.your.package.MainActivity");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Edit: Forgot to add intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
From what I understand, you want a service that is running in the background and on a certain event, you want your application's activity to come in front i.e. on the users current screen whatever he is doing. It is not advisable to let a background service launch an application without a user's action. The android developer website says
A status bar notification should be used for any case in which a
background service needs to alert the user about an event that
requires a response. A background service should never launch an
activity on its own in order to receive user interaction. The service
should instead create a status bar notification that will launch the
activity when selected by the user.
Hence, do not try to make it launch on its own.
I An not behind my laptop atm so I am nog sure, but I think you have toe pass a context object hand then do context.startactivity(intent);
Sorry for not wel formated I am at my phone atm
Hope It helps
I am clutching at straws here, but you wrote:
MyActivity is launched first, then I either navigate to another app or just hit the home screen to have my app running in the background.
So the situation is that your original Activity is NOT running in the background, when you pressed HOME it might well could have been stopped and destroyed. Your background task remained orphan and MyActivity.this is null at this point.
Try and test what does Log.i(TAG,MyActivity.this); print into LogCat.
I ended up using a pending intent and instead of stright up trying to use a intent.
Something like this: seems a lot more simple.
Intent.send(this, 0, intent);
Thanks.
Also, I’ve seen since compileSdkVersion 29 it's not possible, unless a few restrictions:
The activity started very recently.
The app called finish() very recently.
Through a PendingIntent, but only after a few seconds after the notification was sent.
The app has been granted the SYSTEM_ALERT_WINDOW permission by the user.
...
https://developer.android.com/guide/components/activities/background-starts
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.