Sending intent to activity from BroadcastReceiver - android

I have yet another question. I did some research into how to properly send an intent from a BroadcastReceiver to an activity. everyone suggests doing the following:
To construct an intent and use the context provided in the receiver to start that intent. However, I would always get an error when trying to do so saying
AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
So when I googled about that error, people seemed to suggest that you want to add specific flags to the intent. But even with this flags present I am getting the same error. Any help would be appreciatd.
Intent intentMain = new Intent(context.getApplicationContext(), MainActivity.class);
intentMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intentMain.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("com.hennessylabs.xxx.FROM", senderNum);
intent.putExtra("com.hennessylabs.xxx.MSG", message);
context.startActivity(intentMain);

It looks like you are trying to start MainActivity.class from your BroadcastReceiver. That is fine. Though I don't see why you have done what you did in the 2nd and 3rd lines you provided when adding flags. Why don't you just do this:
intentMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
So your whole code block should look like this:
Intent intentMain = new Intent(context.getApplicationContext(), MainActivity.class);
intentMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("com.hennessylabs.xxx.FROM", senderNum);
intent.putExtra("com.hennessylabs.xxx.MSG", message);
context.startActivity(intentMain);

Intent intent = new Intent(context,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
and then in manifest file
in mainactivity
launchmode= singleTask
or as per your requirements, you can use some other combinations also

Related

How to start an activity from intent using broadcast?

I am using the code to call an intent in Android
Intent intent = new Intent();
String PACKAGE_NAME="com...."
intent.setPackage(PACKAGE_NAME);
intent.setAction(Intent.ACTION_VOICE_COMMAND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
getApplication().startActivity(intent);
Unfortunately, in some case, I do not know the PACKAGE_NAME. So, another method is using broadcast. How can I use it? Thanks all
You cant call Activity directly if you dont know package and name of that Activity.
Since you dont know package and Activity name you can only ask OS to show all possible variants for your Intent Action. In most cases user click "always open this app for this action" what means that you will directly open another app.
So in your case your code should looks like
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VOICE_COMMAND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
//dont forget to check if user has at least one application for your Intent Action
if (intent.resolveActivity(getPackageManager()) != null) {
context.startActivity(intent);
}

startActivity() doesn't work when app is in background (In this special case)

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)

Which is the correct way to start an Activity?

Let's say the Activity I want to start is named "OccupyThePieShop"
I was previously using this methodology to start an Activity:
Intent oTPS = new Intent();
timeIntervalConfigIntent.setClassName("com.aXX3AndSpace.KeepInTouch",
"com.aXX3AndSpace.KeepInTouch.OccupyThePieShop");
startActivity(oTPS);
...but was told that this is more the norm:
Intent oTPS = new
Intent(KeepInTouchActivity.this, OccupyThePieShop.class);
KeepInTouchActivity.this.startActivity(oTPS);
...and so I replaced my calls to startActivity() with that usage.
Now, I've come across a couple more ways which seem quite "elegant," namely:
startActivity(new Intent(getApplicationContext(), OccupyThePieShop.class));
...and:
Intent intent = new Intent(this, OccupyThePieShop.class);
startActivity(intent);
Is one way preferred over the others, and if so, why?
I think this is probably an issue of personal preference. I like startActivity(new Intent(this, OccupyThePieShop.class)); because, as you said, it is elegant.

Starting another application from broadcast receiver

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"))

How to bring an activity to front in android?

I am using moveTasktoBack() function to send my activity to background.
I want to bring my activity to front when a timer in my activity finishes. I am using the phone back key to send the activity to back. How do I do that? Help please.
Exact Same issue that is mentioned on this Question.
Sloved it with following code snippet. i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); actauul which brings back the activity to front.
Intent i=new Intent(ApplicationStatus.this,NotifyActivity.class);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
i.putExtra("ID_TimeLeft",String.valueOf(TimeLeft));
startActivity(i);
I think it should be FLAG_ACTIVITY_SINGLE_TOP.
You can use intent with the appropriate flags. FLAG_ACTIVITY_NEW_TASK to create a new task to contain your activity if one doesn't already exist. FLAG_ACTIVITY_REORDER_TO_FRONT to bring your activity to the front of the task if it's not already there.
Intent intent = new Intent(context, YourActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

Categories

Resources