I am working on an app in which on receiving a message i need to start a service. But my Intent service is not running. Here is what I am doing:
Broadcast Receiver:
public void onReceive(Context context, Intent intent)
{
this.con=context;
Toast.makeText(context,"Broadcast received", Toast.LENGTH_LONG).show();
Bundle bundle = intent.getExtras();
Object[] messages = (Object[])bundle.get("pdus");
SmsMessage[] sms = new SmsMessage[messages.length];
for(int i=0;i<messages.length;i++)
{
sms[i] = SmsMessage.createFromPdu((byte[]) messages[i]);
}
String smsFrom = sms[0].getDisplayOriginatingAddress().toString();
Intent in= new Intent(context, ResponseService.class);
in.putExtra("sender",smsFrom);
context.startService(in);
}
IntentService:
public class ResponseService extends IntentService
{
public ResponseService(String name)
{
super(name);
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent)
{
// TODO Auto-generated method stub
Log.d("Notify", "In Response Service");
getCurrentLocation();
}
}
I don't get anything in the Log file. can anybody please help me understanding the problem? Thanks in advance.
Update
I already have declared both broadcast receiver and service in manifest.xml as follows:
<receiver android:name=".BroadCastReceiver" >
<intent-filter android:priority="10000" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<service android:name=".ResponseService" />
Moreover I again tried to run the app and after some time the app force closed giving a java.lang.instantiationexception
Can somebody please help me with this problem?
My first question would be: Is your broadcast receiver working? Is the toast in your broadcast receiver being displayed?
Second question: Did you declare your intent service in manifest? You can do so the following way:
<service android:name="yourPackage.ResponseService" >
The exception, java.lang.InstantiationException really solved my problem. I was missing a zero argument public constructor. I just changed
public ResponseService(String name)
{
super(name);
// TODO Auto-generated constructor stub
}
Into:
public ResponseService()
{
super("ResponseService");
// TODO Auto-generated constructor stub
}
And now it's working really well. Hope it might help others as well.
You should use a WakefulBroadcastReceiver:
A WakefulBroadcastReceiver is a special type of broadcast receiver that takes care of creating and managing a partial wake lock for your app. It passes off the work of processing the SMS to a your ResponseService (IntentService), while ensuring that the device does not go back to sleep in the transition, so your Intent Service will run correctly.
public class YourReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that ResponseService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
ResponseService.class.getName());
this.con=context;
Toast.makeText(context,"Broadcast received", Toast.LENGTH_LONG).show();
Bundle bundle = intent.getExtras();
Object[] messages = (Object[])bundle.get("pdus");
SmsMessage[] sms = new SmsMessage[messages.length];
for(int i=0;i<messages.length;i++)
{
sms[i] = SmsMessage.createFromPdu((byte[]) messages[i]);
}
String smsFrom = sms[0].getDisplayOriginatingAddress().toString();
Intent in= new Intent(context, ResponseService.class);
in.putExtra("sender",smsFrom)
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
And your IntentService will be like:
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
if (!extras.isEmpty()) {
//Do something
}
// Release the wake lock provided by the WakefulBroadcastReceiver.
YourReceiver.completeWakefulIntent(intent);
}
I have done alarms before but you can easily forget to declare it along your wakefulbroadcastreceiver in your manifest.xml.