how to start a new intent from a service class (android) [duplicate] - android

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to start an Activity from a Service?
apologies for my terminology in advance I am very new to android programming.
What i am trying to do:
i am trying to create an alarm clock application.
What i have so far:
so far i have a class that starts an alarm service after 5 seconds (this works fine). then when this service class starts i want it to start another class (called AlarmRinging) but this is where I come unstuck.
Any answers or avenues to check out would be greatly appreciated.

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.

Related

Android Explicit Intent code [duplicate]

This question already has answers here:
Using Intent in an Android application to show another activity
(11 answers)
Closed 7 years ago.
How can I pass the control from one Activity to another using the explicit Intent?
I had an issue in androidManifest.xmnl as it marked the Mainactivity file As an error.
If you just want to navigate from one activity to another you can use explicit intent as :
Intent next = new Intent(YourCurrentActivity.this , NextActivity.class);
startActivity(next);

how to add a button in the running activity [duplicate]

This question already has an answer here:
Which is the best way to add a button?
(1 answer)
Closed 7 years ago.
I am very new to android development and I am having problem displaying a new button to a activity which has already run. I am using this tutorial http://developer.android.com/training/basics/firstapp/starting-activity.html. Here they display Hello World in the new activity. I would like to add a button called "SKIP"! So that the user can go to a another new activity called Functions. How do I do that with the intent? My intent is already being used to display the string hello world! I am using android studio.
Appreciate any help thanks
To create a button you need to go to your main XML file, add a button. Then in your Activity create a onClick listener for the button.
When it comes to going to another activity you need to create a new Intent with
Intent intent = new Intent(this, Functions.class);
startActivity(intent)
You would call your new activity with the following snippet within the OnClickListener of your button:
Intent intent = new Intent(this, Functions.class);
startActivity(intent);
And define your Functions class in your AndroidManifest.xml file similar to what the originally defined Activity is defined as. But you should only have one launcher Activity.

Which function does intent have? [duplicate]

This question already has answers here:
What is an Intent in Android?
(14 answers)
Closed 8 years ago.
I dont know much about intent (or android) so.. Can someone please explain me what is it exactly? i have search on the internet, A LOT.
Also what does each line of this code do?
Intent intent = new Intent (this, DisplayMessageActivity.class);
intent.putExtra("a", "b");
Thanks in advance
I suggest reading Android Intents
You couldn't have search for very long, since this is very basic topic.
I suggest you read more of Android's API guides.
Line 1 = Create message that describes what to do, in this case start "DisplayMessagActivity"
Line 2 = Add content to the message
Intent intent = new Intent (this, DisplayMessageActivity.class);
For this line, its function is to create a navigation from the current activity/page to the displaymessageactivity page.
it is like from here to there.
For this intent.putExtra("a", "b"); the purpose of this is to put like a temp storage/variable to pass to the next page for retrieval. In this case, you put the value "b" in the variable "a". With this method, you can use the value on the other activity or page.
All the above are just storing of info, it is not executed yet. if you want to execute the intent do the following
startActivity(intent);
The best example to state the behavior of Intent is it behaves like a POSTMAN that delivers message to the stated address.
Whether it may be calling service ,BroadCastRecivers ,Activity they are used in number of occassion.
Intents are asynchronous messages which allow application components
to request functionality from other Android components. Intents allow
you to interact with components from the same applications as well as
with components contributed by other applications. For example, an
activity can start an external activity for taking a picture.
Intents are objects of the android.content.Intent type. Your code can
send them to the Android system defining the components you are
targeting. For example, via the startActivity() method you can define
that the intent should be used to start an activity.
An intent can contain data via a Bundle. This data can be used by the
receiving component.
Intents can be used to start Service, call Activty, call Sub Activity, transfer the data between Activity or retrieve the data from Activity

android startActivity from intent in service [duplicate]

This question already has answers here:
Start Activity from Service in Android
(10 answers)
Closed 8 years ago.
i try to use intent in service but when i try this :
Intent intent_facebook = new Intent (this,MainUploadToYoutube.class);
intent_facebook.putExtra("vid", vid);
startActivity(intent_facebook);
got this error on logcat :
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
so i tried this from here :
android start activity from service
Intent intent_facebook = new Intent(getBaseContext(), MainUploadToYoutube.class);
intent_facebook.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity( intent_facebook);
but this do nothing and i did not get error in logcat
what wrong ?
Have you tried your own code (using this as context), but just add the flags as the error tells you?
Intent intent_facebook = new Intent (this, MainUploadToYoutube.class);
intent_facebook.putExtra("vid", vid);
intent_facebook.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent_facebook);
this may help
in Service class you get context and initialize with Context mContext
Intent intent = new Intent(mContext,MainUploadToYoutube.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
((Activity)mContext).startActivity(intent);
There is nothing wrong with your code. It should work. Your problem is something else. Make sure MainUploadToYoutube activity is defined in the manifest and app may not crash once this activity is lunched.

Start Android activity 'manually'

Is it possible to maintain app lifecycle with my code.
I mean to create and control activity ctreation.
Maybe my Windows programming experience mess my Android programming attempts.
For example I want to have list of started activities and to start new activity using code in my application class. Is this possible?
"I want to have list of started activities"
= It's managed by Android as Activity Stack
"to start new activity using code in my application class"
= possible using Intents
Intent intent = new Intent(context, RequiredActivity.class);
startActivityForResult(intent, 0);
hope this will help...

Categories

Resources