Reputation: 2524
I have a service in my app that is used to send http post requests to the server, called Communicator
. I usually bind to it from my activities but now I need to call it from a broadcast receiver (because I need to send a request to the server once a minute) so I use startService(intent)
After some debugging, it seems that when the startService(intent)
is called I'm getting the following error:
08-04 06:07:10.447: W/ActivityManager(1138): Unable to start service Intent { act=ping_states flg=0x14 cmp=com.example.imhere/.Pinger (has extras) } U=0: not found
This is my class that extends the BroadcastReceiver
:
public class Pinger extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//start the communicator and request to iterate over locations with true 'here state'
Log.d("tom.debug", "Pinger class is calling to start the service"); //acording to the log, this part of the code is reached
/*EDIT - THE ISSUE WAS FOUND HERE - See Imtiyaz's answer*/
context.startService(intent); //so this is probably the reason I'm seeing the error
}
}
and here is the way I create the intent (according to the question here: How to run a method every X seconds ):
private void startPinging() {
Date when = new Date(System.currentTimeMillis() + (MINUTE));
try{
Intent pingerIntent = new Intent(this.getApplicationContext(), Pinger.class);
pingerIntent.setAction(getString(R.string.intent_action_ping_states));
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(),
0,
pingerIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarms = (AlarmManager) this.getApplicationContext().getSystemService(
Context.ALARM_SERVICE);
alarms.set(AlarmManager.RTC_WAKEUP,
when.getTime(),
pendingIntent);
alarmSet = true;
Log.d("tom.debug", "startPinging: alarmSet is now true");
} catch(Exception e){
e.printStackTrace();
}
}
I assume the manifest file is also relevant:
...
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:enabled="true"
android:exported="false"
android:name="com.example.imhere.Pinger" >
<intent-filter>
<action android:name="@string/intent_action_ping_states" />
</intent-filter>
</receiver>
<service
android:name="com.example.imhere.Communicator"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="@string/intent_action_ping_states" />
</intent-filter>
</service>
...
</application>
...
Upvotes: 0
Views: 2990
Reputation: 2053
You can not directly start a intent like you did.
Try it this way:
@Override
public void onReceive(Context context, Intent intent) {
//start the communicator and request to iterate over locations with true 'here state'
Log.d("tom.debug", "Pinger class is calling to start the service"); //acording to the log, //this part of the code is reached
//context.startService(intent); //so this is probably the reason I'm seeing the error
//if some data you want from this broadcast event then you can do like
// String tempMessage = intent.getStringExtra("KEY");
Intent serviceIntent = new Intent(context , YOUR_SERVICE.class);
context.startService(serviceIntent );
}
Thanks.
Upvotes: 4