In my app whenever we get the message, need to display one pop up with pre filled message. For this i used the following code in manifest file
<receiver
android:name="com.cte.broadcast.SMS_Receive_BroadCast"
android:enabled="false">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
And whenever app is login, register the broadcast receiver by using the following code
ComponentName component = new ComponentName(getApplicationContext(),
SMS_Receive_BroadCast.class);
getApplicationContext().getPackageManager().setComponentEnabledSetting(component,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
And whenever app is login unregister the broadcast receiver by using the following code
ComponentName component_sms = new ComponentName(context.getApplicationContext(),
SMS_Receive_BroadCast.class);
getApplicationContext().getPackageManager().setComponentEnabledSetting(component_sms,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
so here everything is working fine. when ever we logged in and whenever we get the message broadcast receiver trigger and getting pop up even app is closed. and logged out not getting trigger broadcast receiver and not getting pop up. so up to this everything fine. But the problem is after one day whenever we get the message the broadcast receiver wont trigger even it is logged in. But whenever we open the app and close it then it will working fine. so the problem is after some time broadcast receiver is automatically unregisterd i think..
So how to resolve this problem..Thanks In Advance..
Some applications abort your Broadcast on the intent which will prevent other applications from receiving the intent.
The solution is to increase the android:priority attribute in the intent-filter tag:
<intent-filter android:priority="priority value">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
I had a similar trouble. I dont know exactly why, but depending on you put your registerBroadcast, android will kill that instance, it´s something related to processes execution stack on linux.
What i did to solve the problem: create a background service instead of a BroadcastReceiver, there you register your BR and it will work fine, several days =))) !
Related
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.
In my application, there's a feature that allows users to dial a specific number and brings up an activity to front. I have the following receiver, and the only receiver registered in AndroidManifest.xml.
<receiver android:name="com.example.myapp.OutgoingCallListener" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
Please note there's no BOOT_COMPLETED intent or service.
Now here's the thing I couldn't figure out. When I reboot my device, go check the Running Apps, my application is not listed there. But, if I dial the specific number, my application starts and the activity is brought to front.
My question is: If the app is not a service, and not started on boot, how could it recieve intent from Android? That is, in my case, how could my app listen to NEW_OUTGOING_CALL while it's not started at all?
A BroadcastReceiver that is registered in the manifest is always capable of responding to a matching broadcast. If your process is not running for any reason, Android will start up a process for you.
I have two apps that I have complete control over. Both are signed with the same cert and both use the exact same intent filter. One sends the broadcast from a fragment, the other is suppose to receive it and do something. This however is not working:
Strings.FILTER_INIT_REGISTER = "com.app.FILTER_INIT_REGISTER"
Intent intent = new Intent(Strings.FILTER_INIT_REGISTER);
getActivity().sendBroadcast(intent);
I have registered the receiver in the Manifest app tag for the app containing the ReportingReceiver class:
<receiver
android:name=".receivers.ReportingReceiver"
android:exported="true"
>
<intent-filter>
<action android:name="com.app.FILTER_INIT_REGISTER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Curious why the ReportingReceiver class is not getting the intent call?
If your application only has a service and receivers then this won't work in Android 3.1 and later. The reason is that the system will not send broadcast Intents to application that are in the STOPPED STATE. An application is in the STOPPED STATE when it is first installed. It is removed from the STOPPED STATE when the user manually starts the application for the first time. It is returned to the STOPPED STATE if the user forces the application to stop using the application manager tool.
Since your application has no Activities, there is no way for the user to "start" it. Therefore it will never come out of the stopped state.
See http://developer.android.com/about/versions/android-3.1.html#launchcontrols
As Android Addict says in his comment to David Wasser's answer ... there is a way around this behaviour.
Just add the following flag to the calling Intent. This will ensure that you also reach broadcast receivers from "stopped" applications.
http://developer.android.com/reference/android/content/Intent.html#FLAG_INCLUDE_STOPPED_PACKAGES
You can read more about this Android 3.1 change here
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
and here
http://code.google.com/p/android/issues/detail?id=18225
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.
I am working on broadcast receiver and stuck in a problem.
I am receiving a broadcast receiver in Manifest file.
<receiver class=".MyClass" android:name=".MyClass">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.ConnectivityManager.CONNECTIVITY_ACTION" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
this is working fine and it is calling MyClass whenever there is change in connectivity.
Now the problem is whenever my application is not running still this class will receive broadcast receiver. I want it to receive whenever the application is running.
I tried it by extending BroadcastReceiver registering and unregistering broadcast in that class file and it works. But i want to achieve the same by Manifest file.
My problem will solve if it is not receiving anything when application is not opened.
What you are talking is not possible. The whole purpose of having the intent filter in the manifest is being able to receive the intent whether or not your application is running. The only way to do what you want to is by registering/unregistering the receiver from the code [registerReceiver]
The question was asked a long time ago but in case some one landed on this page while searching, It is possible to register and unregister broadcast receiver from code instead of doing that from manifest file. (Checking the Networking Connectivity using BroadcastReceiver in Android)
You said "My problem will solve if it is not receiving anything when application is not opened".
Here how I understand your question and appropriate answer.
android:enabled
Whether or not the broadcast receiver can be instantiated by the system — "true" if it can be, and "false" if not. The default value is "true".
If you want to enable your receiver at runtime, you can set the state to disabled initially. You can do so in the manifest file:
<receiver
android:name=".YourReceiver"
android:enabled="false" >
<!-- your intent filter -->
</receiver>
Source:
http://developer.android.com/guide/topics/manifest/receiver-element.html#enabled
http://www.grokkingandroid.com/enabling-and-disabling-broadcastreceivers/