android - sending intents between two different services in two different applications - android

i am trying to pass data (string\int) between two service in two different application
service A in app1 and service B in app2.
say i want to pass from application 1 -> 2 , so i defined a custom receiver in the manifest in app 2 with an intent filter with an action string
<receiver android:name=".blaReceiver">
<intent-filter>
<action android:name="com.bla.blabla.RANDOM_ACTION" />
</intent-filter>
</receiver>
but how do i send the intent from app 1 ?
there are no activities only services, i thought of startService from app1 but there is no where i can define it to sent the intent to app2
Thanks.

Use broadcast receivers. Send broadcast [with unique action name]from app A and register a broadcast receiver in app B. [broadcast will be sent system-wide]. Receive broadcast in you app B and verify it by action name.

You can use sendBroadcast(intent); in your service. It works for sure.

Related

Communication between two closed android applications

I have 2 apps on one device. The server sends push to both applications. I want the first application that received the push to show a notification, and the second should do nothing. How can I check that the first app already showed a notification?
Apps can be closed. I use FirebaseMessagingService for wake up.
I need this because the user can have only one app installed or both.
Best way is just your server send notification to only one app. But if it is not possible, you can use Broadcast receiver I think.
You can use Broadcast receiver to communicate between two application.
In app B, for example declare broadcast receiver like follow:
<receiver android:name=".MyBCReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="what.ever.you.want" />
</intent-filter>
</receiver>
And from app A.
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.setAction("what.ever.you.want");
intent.putExtra("key","data");
intent.setComponent(new ComponentName("com.app.b","com.app.b.MainActivity"));
sendBroadcast(intent);
Then your app B, you can just make a mark using sharedPreference to not make a notification again.

Doubts about Intents and BroadcastReceiver

Im quite new on Android programming, so I have some basic doubts.
There is an App here that do not have the BroadcastReceiver, but I used other class instead, and I am sure that it works properly.
I read in some topics, that i need to register It in manifest, but I have no clue how to do It; and I got confused about com.google.android.c2dm.permission.SEND and etc, I do not know how to set it.
Question: Can I ask for someone explain to me, in simple way, what I need to do to my method be executed while the app is closed, AND, how i register it on manifest?
Thanks!
Send an Intent is the way of Android of telling to everyone that some event occured.
For instance where your device receive a call an Intent is broadcast. But to be specific to some event every Intent has an action. For instance the Intent broascast when you receive a SMS has the "android.provider.Telephony.SMS_RECEIVED" action.
In your AndroidManifest.xml you can register objects for specific intents. You can register Activity, Service and BroadcastReceiver.
To register a BroadcastReceiver to "receive sms action" you do the following in your manifest :
<receiver android:name="your.receiver.class">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
This means that every time an Intent with the specified action is fired, it will be pass to your receiver. This means that his onReceive method will be called with the intent as param.
So to create some code that will be executed will your app is closed, follow this steps :
Create a class that extends BroadcastReceiver.
Put your code in the onReceive method. This method will be call every time your receiver receive an intent.
Register your receiver for the desired action in your AndroidManifest.xml file.
BroadcastTeceiver as the name imply is component that could receive data that someone send via Intents. The sender could be the system, other App or your App itself.
There are to ways to regiester BroadcastReceiver:
In the manifest by exlixit the Intent you want to listen.
In code by give it Intent_filter programmatically.

What is the difference between intent filter in activity and broadcast receiver?

Can anybody tell me when should I use intent filter and broadcast receiver?
<activity>
<intent-filter></intent-filter>
</activity>
and
<receiver>
<intent-filter></intent-filter>
</receiver>
I think you are confused about implicit intent and broadcast receiver. Intent Filter in Activity is for receiving implicit intent, while that in Receiver is for receiving broadcast. The OS deliver an broadcast msg to all Receivers, while it deliver an implicit intent to a certain one activity. See here
From the documentation:
A broadcast receiver is a component that responds to system-wide
broadcast announcements. Many broadcasts originate from the system—for
example, a broadcast announcing that the screen has turned off, the
battery is low, or a picture was captured. Applications can also
initiate broadcasts—for example, to let other applications know that
some data has been downloaded to the device and is available for them
to use. Although broadcast receivers don't display a user interface,
they may create a status bar notification to alert the user when a
broadcast event occurs. More commonly, though, a broadcast receiver is
just a "gateway" to other components and is intended to do a very
minimal amount of work. For instance, it might initiate a service to
perform some work based on the event.
You can use broadcast receiver by two ways.
1) Registering and un-registering in your activity.When you register with your activity you need to pass an action for which it will take care and it will fire when we send broadcast with that action from our application.
2) Second way to use broadcast reciver to register in manifest file and mention action in intent filter for that in manifest file.
Intent filter is nothing but in simple words "It is filter just we use in our usual lives." It will filter the actions for calling it.
Intent filter is same for activity and broadcast receiver.Its main functioning is to filtering the action.It depends on us how to utilize it.One major example is in our each application in manifest file we specify
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
in our launcher activity.It suggests this activity is our launcher activity and will run first at start of our application.If you not specify it then your application will not launch.Also we can not specify these types of filter in broadcast receiver's intent filter.They are not launcher of apps,

How to start service using external intent?

I'd like to know if it's possible to start service using intents sent from another app? I've a Broadcast Receiver for android.intent.action.BOOT_COMPLETEDand it works even though at the time when the intent is received BrodcastReceiver class for it is not instancionated. I did something similar for external intents from tasker but it doesn't work.
<receiver android:name="BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I'd like to know if it's possible to start service using intents sent from another app?
Sure.
I've a Broadcast Receiver for android.intent.action.BOOT_COMPLETEDand it works even though at the time when the intent is received BrodcastReceiver class for it is not instancionated.
That is because manifest-registered BroadcastReceiver objects are not instantiated ahead of time. They are only created when a matching broadcast is sent.
I did something similar for external intents from tasker but it doesn't work.
"it doesn't work" is insufficient information for anyone to help you.
But, if you have a <service> with an <intent-filter>, other apps can create an Intent matching your <intent-filter> and use that to start (or bind to) your service. There are two exceptions:
If you add android:exported="false" to the <service>, third party apps cannot invoke it at all, though you would be better served simply getting rid of the <intent-filter> in that case
If you use android:permission on the <service> element, the other app needs to hold your stated permission in order to start or bind to your service

SMS Receiver as a Service

I have created SMS Receiver app... but i want to create it as an service, it should run in the background (i.e no separate UI for this app, want to work like alarm app) and even if mobile restarts it automatically starts... could any one help on this?
My previous SMS Receiver app code was here
Unable to instantiate activity ComponentInfo in Android Receive Sms App
it should run in the background
Your existing BroadcastReceiver for the (undocumented) android.provider.Telephony.SMS_RECEIVED already runs in the background.
and even if mobile restarts it automatically starts
Your existing BroadcastReceiver for the (undocumented) android.provider.Telephony.SMS_RECEIVED already is available after the device reboots.
If you want your service to run at phone startup, you should simply declare a broadcast receiver with this intent filter:
<receiver android:name="MyStartupIntentReceiver">
<intent-filter>
<action
android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
In the broadcast receiver onReceive() method, just launch your service:
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent();
serviceIntent.setAction("myPackage.MyService");
context.startService(serviceIntent);
}
And be sure to link the service in your manifest with the same name of the intent you launch in the broadcast receiver:
<service android:name="MyService">
<intent-filter>
<action
android:name="myPackage.MyService" />
</intent-filter>
</service>
As CommonsWare has said there is no need to actually have a "Service" run in the background for you to receiver SMS broadcasts, if registered in your manifest properly your BroadcastReceiver with "android.provider.Telephony.SMS_RECEIVED" as the intent filter will fire every time an SMS is received no other action required.
Depending on what exactly you want to do, from that broadcast receiver you can then do you work via an actual Service, or probably a better option would to be to use an IntentService. This is because the thread used for the broadcast will get killed shortly after starting, so you should not do any extensive work in it.
It is generally recommend to not use an actual "Service" unless explicitly required....BUT if that is what you need then you need Davide should get you the right direction.

Categories

Resources