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"))
Related
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
I got log cat message from startAnotherActivity() method
private void startAnotherActivity() {
Log.i(TAG, "Entered startAnotherActivity()");
Intent intent = new Intent();
intent.setAction(ANOTHER_ACTIVITY);
intent.addCategory("android.intent.category.DEFAULT");
startActivity(intent);
}
Another activity doesn't start, no other messages in log cat.
How can I resolve this issue?
UPDATE#1:
Sorry, I forgot to mention that AnotherActivity is an Activity in the other application, and therefore ANOTHER_ACTIVITY == 'some.other.app.domain.ANOTHER_ACTIVITY'
Shouldn't Dalvik complain if it cannot find specified activity?
One possible reason may be not declaring other activity in the manifest. You can do this like the following:
<activity android:name="your.package.your.activity">
</activity>
And then you can start the activity by doing the following:
Intent intent = new Intent(CurrentActivity.this, NewActivity.class);
startActivity(intent);
Hope this helps.
Since it's an activity in another application, you may need to set the component (fully qualified package name and fully qualified activity name).
See here: How to start activity in another application?
Or here:
Launch an application from another application on Android
Finally I found out my mistake.
In the project there are two similar messages in two activities, so I thought that runs one, but that was another.
Thank you for your assistance!
I'm using the below code to start an Activity from a Service:
public void startAct(){
Intent i = new Intent();
i.setClass(this, MyActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
it works on my device, also on emulator but I gave it to others to test. some of them say it simply doesn't work, other receive a force close. how is it possible? how to solve it?
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 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.