Start Activity in a BroadcastReceiver - android

I've built a small app. The only thing it does is, catch an outgoing call and show some activity when it happens. There is just an Activity and a BroadcastReceiver.
I wanted to integrate my code with another application, I removed the BroadcastReceiver from the Manifest.xml and created (and registered) it dynamically from the main activity. My receiver fired well but the activity is not shows up.
What is the difference between the two methods?
How can I make the activity to show up?
from MainActivity.java:
callInterceptor = new InterceptOutgoingCall();
IntentFilter callInterceptorIntentFilter = new IntentFilter("android.intent.action.NEW_OUTGOING_CALL");
callInterceptorIntentFilter.setPriority(100);
registerReceiver(callInterceptor, callInterceptorIntentFilter);
and from the function receiver.onReceive(Context,Intent):
Intent alertIntent = new Intent(context, AlertActivity.class);
alertIntent.putExtra("callnumber", phonenbr);
alertIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(alertIntent);
my activity is declared in the manifest like this:
<activity android:name=".AlertActivity"
android:screenOrientation="portrait"/>

I found the answer at two threads:
Android launch an activity from a broadcast receiver
Activity started from notification opened on top of the activity stack
In the manifest, the activity should be declared with android:taskAffinity.
And when starting the intent I had to add a flag = Intent.FLAG_ACTIVITY_NEW_TASK

Related

How to relaunch current activity with new data from notification click intent on Android

I'm using the following intent with a notification and the issue is that if the current activity is the same as the intent nothing happens. How do I open the same activity with the new data?
intent = new Intent(context, PackViewActivity.class);
intent.putExtra("pid", pack_id);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Here is the details fromt he Manaifest:
<activity
android:name=".PackViewActivity"
android:configChanges="orientation|keyboardHidden|screenLayout|screenSize"
android:launchMode="singleTask"/> //I've also tried singleInstance with no success.
Currently everything works fine unless the current activity is PackViewActivity. In that case nothing happens.
Like #Vivek mentioned, use Intent.FLAG_ACTIVITY_CLEAR_TOP and remove Intent.FLAG_ACTIVITY_CLEAR_TASK. Now, if your activity is already running the new intent will be delivered in onNewIntent(). That is where you should put your intent reading code. Also, get rid of android:launchMode in your activity manifest description since it brings a lot of problems with it.
If the required activity is already in foreground then you need not to push a notification. Alternatively you can register the activity as a listener to the service or activity class which pushes the notification through NotificationManager. Then handle the notification appropriately.

Custom intent to other app

I have a simple question but i can't find anything on google, maybe i use the wrong key word.
I'm developping an app with a service in background. This service is always started. I have a phone with a custom button than can start an app. But i want to use this button to start an action on my service don't start any activity.
To do that, i have think about an ugly solution : I configure my custom button to start an other app. This app is a blanck activity and on the onCreate() event i just send an itent to my service and after finish the activity.
My question is how can i send a custom intent to an other app ?
my idea : in the blanck activity write this
#Override
public void onCreate(){
super.onCreate();
Intent customIntent = new Intent("com.customIntent.action");
startActivity(customIntent);
finish();
}
On my service doing something like that :
IntentFilter it = new IntentFilter();
it.addAction("com.customIntent.action");
registerReceiver(myReceiver, it);
Thanks for your help !
You are registering receiver and it will only be catched when sendBroadCast will be called with the intent. Secondly, you are starting Activity with that Intent action. There is no Activity in xml/code which handles this action. Thirdly, you can add this Intent Filter in AndroidManifest against specific reciever and in Activity use
Intent customIntent = new Intent("com.customIntent.action");
LocalBroadcastManager.getInstance(this).sendBroadcast(customIntent);
AndroidManifest.xml
<receiver android:name="." >
<intent-filter>
<action android:name="com.customIntent.action" />
</intent-filter>
</receiver>
Hope this helps.
I dont't know if I understand your question correctly but this tutorial explains very well how you use an intent to interact with another app:
http://developer.android.com/training/basics/intents/index.html

Prevent activity run from recent app list in android

I have an activity A which start when a custom broadcast received. This activity is not the launcher activity.
receiver
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context,A.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtras(intent.getExtras());
context.startActivity(i);
}
A receive the intent value (Intent has some values) and full fill some task. The whole procedure is working fine.
Problem is that, when i open recent application list (Long press home button) this activity appears (not a launcher activity) and when i click on it, activity start with intent value !!. So i can't check is this activity start from broadcast receiver or from other.
How can i fix this?? This activity should only start when a broadcast receive.
Edit
Manifest
android:name".A"
android:screenOrientation="portrait"
android:launchMode="singleInstance"
android:noHistory="true"
android:taskAffinity=""
Did you try:
android:name".A"
android:excludeFromRecents="true"
This will prevent your activity from being displayed in recent list.
See this
when your activity is the root of a new task, it will not appear in the recent list. if it's not the root , try adding taskAffinity property.

resuming activity in Android using Service

I have a mainActivity.
When starts, it starts a service and bonds it.
There is a timer which will send the mainActivity (this) to back after X seconds, while the service keep running and listening, i use moveTaskToBack (true).
When the service listener triggered, the service starts the activity, but instead of the activity to be called through onResume() (since it was sent to back) its called through onCreate(), to call the activity currently i use :
Intent dialogIntent = new Intent(getBaseContext(), MainActivity.class);
dialogIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
getApplication().startActivity(dialogIntent);
Which explains why the activity is created all over again.
I tried using FLAG_ACTIVITY_REORDER_TO_FRONT but got an exception.
add this attribute in activity of AndroidManifest.xml android:launchMode="singleInstance" in AndroidManitest.xml
AndroidMenifest.xml
<application>
<activity
android:launchMode="singleInstance">
</activity>
</application>

Prevent opening Activity for multiple times

I have a common menu on my app with icons. Clicking an icon will start an Activity. Is there a way to know if an activity is already running and prevent it from starting multiple times (or from multiple entries)? Also can I bring an activity that is in onPause state to the front?
Use this:
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
while starting Activity.
from documentation:
If set in an Intent passed to Context.startActivity(), this flag will
cause the launched activity to be brought to the front of its task's
history stack if it is already running.
In your activity declaration in Manifest file, add the tag android:launchMode="singleInstance"
I got it perfectly working by doing the following.
In the caller activity or service (even from another application)
Intent launchIntent = getPackageManager().getLaunchIntentForPackage(APP_PACKAGE_NAME);
//the previous line can be replaced by the normal Intent that has the activity name Intent launchIntent = new Intent(ActivityA.this, ActivityB.class);
launchIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(launchIntent);
and in the manifest of the receiver activity (the I want to prevent opening twice)
<activity android:name=".MainActivity"
android:launchMode="singleTask"
>
This works for me :
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
from official documentation
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.
also, you can use FLAG_ACTIVITY_NEW_TASK with it.
then the code will be :
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
Just use
Intent i = new Intent(ActivityA.this, ActivityB.class);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
create an instance of your activity which you dont want to start multiple times like
Class ExampleA extends Activity {
public static Activity classAinstance = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
classAinstance = this;
}
}
Now where ever u want to crosscheck i mean prevent it from starting multiple times, check like this
if(ExampleA.classAinstance == null) {
"Then only start your activity"
}
please add this in menifest file
<activity
android:name=".ui.modules.profile.activity.EditProfileActivity"
android:launchMode="singleTask" // <<this is Important line
/>

Categories

Resources