android startActivity from intent in service [duplicate] - android

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.

Related

Error in Intent - Android Studio

I'm trying to modify vuforia's sample videoplayback application in Android Studio. In that sample app, video will be played whenever targeted image is focused. I try to modify like I want to go next activity when the video is played fully. So I just used Intent, but it will throw and error as "Cannot resolve constructor Intent". Here I have attached screenshot of the error.Can you guys please help me to resolve this issue. Thanks in advance.
Screenshot of the error VideoPlayerHelper imports
Intent intent = new Intent(this, SearchResult.class);
"this" should be context or child's context.
Intent intent = new Intent(type of context, SearchResult.class);
This is just a class you need to pass the context to the class(passing through constructor would be better). Then do following:
Intent intent = new Intent(context,SearchResult.class);
and better way is create listeners(interface) and call appropriate method on activity using them.
Try Intent intent = new Intent(VideoPlayerHelper.this,SearchResult.class);
Just change u're coding
Intent intent = new Intent(this,SearchResult.class);
to
Intent intent = new Intent(getIntent(),SearchResult.class);

How to start an Intent in Android

I just following a tutorial to start a basic intent, and I get an error. Doing exact the same as the tutorial, created an empty Activity and just passed as argument to the intent. What is the problem?
Using a video tutorial: Lynda.com - Developing Android Apps Essential Training (2015)
The error you've got in IDE usually happens when you try to use Activity Context (keyword this) inside some callback, listener or anonymous function. In such situation this does not refer to the Context of the Activity.
That's why solution provided by #Vishal Patoliya should fix your problem because you're explicitly referring to the Context of the concrete Activity as follows:
Intent intent = new Intent(YourActivity.this,ItemUserSettingRattingActivity.class);
Try this
Intent intent = new Intent(YourActivity.this,ItemUserSettingRattingActivity.class);
startActivity(intent);
In case of Activtiy, you are going to another activity:--
Intent intent = new
Intent(YourActivity.this,ItemUserSettingRattingActivity.class);
startActivity(intent);
If you are passing intent from a fragment to open a activity then
Intent intent = new Intent(getActivity(),ItemUserSettingRattingActivity.class);
startActivity(intent);
Have you already created a class named ItemUserSettingRattingActivity.java? maybe you get an error it's because the intent you created didn't detect the class.

Start multilple activity from broadcastReceiver

I want to start multiple activities from my broadcast receiver. I have two classes i.e ReadContacts and CallDetails. I want to start them one by one. like first calldetails activity should be started and then next. I have tried below code and it works fine.
Intent calldetails = new Intent();
calldetails.setClassName("com.simplereader", "com.simplereader.Calldetails");
calldetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(calldetails);
then I tried below code to start other activity
Intent readcontacts = new Intent();
readcontacts.setClassName("com.simplereader", "com.simplereader.ReadContacts");
calldetails.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
context.startActivity(readcontacts);
But its not working and application crashes.
You must have the Intent Flag Intent.FLAG_ACTIVITY_NEW_TASK to start an Activity from outside of an Activity context so you need to add that flag to your second Intent.
I don't know if this is your only problem but if that doesn't fix it then post your logcat so we can see the error.
Intent readcontacts = new Intent();
readcontacts.setClassName("com.simplereader", "com.simplereader.ReadContacts");
calldetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // you need this flag
context.startActivity(readcontacts);
FLAG_ACTIVITY_MULTIPLE_TASK Do not use this flag unless you are implementing your own top-level application launcher.
From the android developer documentation for intent.
You could probably just launch both activities with the new task flag.
I think you are making mistake in this line
calldetails.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
If you want to start readcontacts activity it shoul be
readcontacts.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
instead of
calldetails.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
I think this is the reason.

Application crashing when starting an activity from a broadcast receiver, sent from a service

First of all, I have already tried adding flag_activity_new_task, still its not working.
Here is the flow:
I have a service running that does http request on a background thread and fetches data. Whenever certain data is received, I need to display it to user(for example, an update to the app is available). For this, I use broadcast, and have setup a receiver. Everything works fine till here.
But, when I tru starting an activity from this receiver, app crashes saying:
"01-15 17:03:30.129: E/AndroidRuntime(28014): Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
"
Even though I have added this flag. Here are the codes:
Service part:
try
{
JSONObject mAdDetails = mResult.getJSONObject(Tags.TAG_RPC_SMALL_AD_DETAILS);
Intent mI = new Intent();
mI.setAction(ReceiverAdvertSmall.INTENT_ACTION);
mI.putExtra(AppMapKeys.KEY_AD_HEADING, mAdDetails.getString(Tags.TAG_RPC_AD_HEADING));
mI.putExtra(AppMapKeys.KEY_AD_DESCRIPTION, mAdDetails.getString(Tags.TAG_RPC_AD_DESCRIPTION));
mI.putExtra(AppMapKeys.KEY_AD_LINK, mAdDetails.getString(Tags.TAG_RPC_AD_LINK));
sendBroadcast(mI);
}
catch(JSONException e)
{
}
The receiver part:
#Override
public void onReceive(Context mContext, Intent data)
{
Intent mI = new Intent(mContext, ActivityAdvertSmall.class);
mI.putExtras(data.getExtras());
mI.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(data);
}
What Am I Doing Wrong? Iam unable to figure it out for last one hour.!!
Thanx in advance...
use
mContext.startActivity(mI);
instead of
mContext.startActivity(data);
for start Activity from onReceive because you are adding FLAG_ACTIVITY_NEW_TASK flag to mI intent but trying to start Activity using data intent
You should pass the intent you created to startActivity()
mContext.startActivity(mI);
Ok....here was the stupidiest mistake I made. I wrongly used startActivity(data) instead of startActivity(mI). Closing the question.
Just pass the intent you created to startActivity()
mContext.startActivity(mI);

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

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.

Categories

Resources