I am trying to make a MissCall App which sends a message automatically when a miss call is received.I had completed my app and it worked fine !
Here is
In the first version, the listener is not auto restarted on boot (and also might be killed by the system) and which is why it didn't work on boot. Also you forgot to unregister the listener.
In your second version, onReceive()
should be called whenever there is phone state changes, provided that your Manifest is setup correctly, like
<receiver android:name=".CallReceiverBroadcast" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
However, Android may recycle the Broadcast receiver between calls to onReceive()
, meaning that your checkIt
variable may not be preserved between calls to onReceive()
. You may need to use a static variable for it to work.
In your third version, you are creating and registering multiple version of the call state listener whenever onReceive()
is called. (Note that tm should point to the same instance of telephony manager), which may be why they are called multiple times.
Depending on how you turn the variables into static ones, the problem may not be solved since you may still be creating / registering new instances of CallStateListener
, although you store it into a static variable.
To solve these you should unregister the call state listener by overriding onDestroy()
of the broadcast receiver
@Override
public void onDestroy() {
tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
}
However please note that there is no guarentee that after onReceive()
returns, the process hosting the receiver is not destroyed. Your second version which performs all processing before onReceive()
returns is preferable, if you can fix it.
This is because of once you got the miss call then you are AT TelephonyManager.CALL_STATE_IDLE
stage and message will send to appropriate message but your service is running mode that's Why it will send 10 or multiple sms.
try creating a bootbroadcast receiver,and in its on receive start your service to detect calls.this ensures that your service gets activated automatically after each device restart.