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.
Related
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 =))) !
i want to make an application in which i start service which start broadcast reciever for sms_read and every time when new sms come and reciever detact that message. no matter app is running or not.
you don't have to use a Service for Reading and listening for SMS.
All you need to Do is just register a Broadcast receiver in your Application like this .
<receiver android:name=".IncomingSms" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
And just write whatever functionality you have to do upon receiving the SMS in the Broadcast receiver by starting an IntentService or a Thread
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,
My app registers in the manifest a broadcastreceiver for PHONE_STATE intent.
<receiver android:name=".receiver.PhoneStateReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
<action android:name="android.intent.action.PHONE_STATE"/>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Everything worked fine until i installed another app which handles phone calls (in specific it records audio of phone calls). From that moment my registered receiver does not fire every time. If i remote the second app all returns to be ok.
I assume that this app (as mine) registers a broadcastreceiver for PHONE_STATE intent. Is it possible that this app "consumes" the broadcastreceiver and so it will not fire mine?
i assume that the other broadcast consumer has called the abortBroadcast() method, that ... will prevent any other broadcast receivers from receiving the broadcast.
Here is a my broadcast receiver.
public class SmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context ctx, Intent intent) {
Log.d(DEBUG_TAG, SmsReceiver.class.getSimpleName()+ " action: " + intent.getAction());
// here is codes for sending intent to my running service
} }
here is broadcast receiver and service nodes in my manifest xml file.
<service android:name=".SMSGatewayService" />
<receiver android:name=".SmsReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
How can i send intent from broadcast receiver to my running service.
thanks in advance.
Ideally, you don't have a running service. Services should only be in memory when they are doing something, and odds are your code won't be doing anything when an SMS arrives.
That being said, your receiver can call startService(). This passes an Intent to onStartCommand() of the service, whether it is already running or not. Ideally, your service is an IntentService, one designed to work with the command pattern and to shut down when it is done processing a command.
Also, since SMS messages can arrive while the device is asleep, you will probably need to employ a WakeLock to ensure the device stays awake long enough for control to pass to the service and for the service to do whatever it is it is doing. One approach for this is to use my WakefulIntentService, which wraps up the IntentService+WakeLock pattern.