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.
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)
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
Is there any way to close android app ? I cannot use finish because it shows previous activity and I have lot off activities behind ( for example from 1st to 2nd .. to 10th and on 10th I want to exit from app ). How to achieve this ?
How about using android:noHistory
Whether or not the activity should be removed from the activity stack and finished (its finish() method called) when the user navigates away from it and it's no longer visible on screen — "true" if it should be finished, and "false" if not. The default value is "false".
A value of "true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it.
Intent intent1 = new Intent(context, Activity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent1);
CLEAR TOP clear your stack of activity and can finish the last activity you redirect
if your activity stack is proper clear top will work for you
OPTION 2:
using Borad cast receiver chk link
I also had the same question and did not find a way to definitelly KILL an app. But later I discovered it is due to the intrinsic nature which the system was designed of.
If you want to close an app, you just have to clear the Activity stack and free the usage resources. It seems strange, right? You should have the power to close your own app. But it is really not recommended on Android apps and it does not follow the Android guidelines. And also, do not provide exit options to users. For Android applications which needs to provide an "exit option" to user (for example, due to security reasons), we should provide a "logout option", clear the Activity stack, free resources and other stuff you want to do, but never kill the Main Activity.
The solutions to clear the Activity stack have been shown for other answers here: (1) to use Intent with flag FLAG_ACTIVITY_CLEAR_TOP or (2) to use Broadcast.
With FLAG_ACTIVITY_CLEAR_TOP, you put an Intent on the current Activity:
Intent intent = new Intent(getApplicationContext(), YourMainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
And the option with Broadcast, is to send a broadcast from the current Activity to all Activities you want to close.
For this, you have to SEND the broadcast (on the current Activity):
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.package.ACTION_SPECIFICNAME");
sendBroadcast(broadcastIntent);
finish();
And RECEIVE this broadcast on the others Activities (on each one you want to close):
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.package.ACTION_SPECIFICNAME");
registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("onReceive","Logout in progress");
finish();
}
}, intentFilter);
And that is it! I hope this answer helps you. Sorry for my english.
I know that it is not the answer you were waiting for, but I think that is the current (and the "correct") solution.
You can use (but it's not recommended)
System.exit(0);
or you can move your app in background (that is the way Android manages multitasking)
moveTaskToBack(true);
Place this code on exit button press
Intent intent = new Intent(ExitConfirmationActivity.this, FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
And override the onResume of initial activity with a finish();
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);
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();