1

I have two files.

IntentServiceTest1Activity.java

 public class IntentServiceTest1Activity extends Activity {
     /** Called when the activity is first created. */

     public class ResponseReceiver extends BroadcastReceiver {

         public static final String ACTION_RESP = "com.mamlambo.intent.action.MESSAGE_PROCESSED";@Override
         public void onReceive(Context context, Intent intent) {
             // TODO Auto-generated method stub 
             TextView result = (TextView) findViewById(R.id.txt_result);
             String text = intent.getStringExtra(SimpleIntentService.PARAM_OUT_MSG);
             result.setText(text);
         }
     }
     private ResponseReceiver receiver;

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

         IntentFilter filter = new IntentFilter(ResponseReceiver.ACTION_RESP);
         filter.addCategory(Intent.CATEGORY_DEFAULT);
         receiver = new ResponseReceiver();
         registerReceiver(receiver, filter);

         Button button = (Button) findViewById(R.id.button);
         button.setOnClickListener(new OnClickListener() {
             public void onClick(View v) {
                 EditText input = (EditText) findViewById(R.id.txt_input);
                 String strInputMsg = input.getText().toString();
                 Intent msgIntent = new Intent(IntentServiceTest1Activity.this, SimpleIntentService.class);
                 msgIntent.putExtra(SimpleIntentService.PARAM_IN_MSG, strInputMsg);
                 startService(msgIntent);
             }
         });
     }
 }

SimpleIntentService.java

 public class SimpleIntentService extends IntentService {

     public static final String PARAM_IN_MSG = "imsg";
     public static final String PARAM_OUT_MSG = "omsg";
     public SimpleIntentService()
     {
         super("SimpleIntentService");
     }
     @Override
     protected void onHandleIntent(Intent intent) {
         // TODO Auto-generated method stub 
         String msg = intent.getStringExtra(PARAM_IN_MSG);
         SystemClock.sleep(30000); // 30 seconds         
         String resultTxt = msg + " " + DateFormat.format("MM/dd/yy h:mmaa", System.currentTimeMillis());

         Intent broadcastIntent = new Intent();
         broadcastIntent.setAction(ResponseReceiver.ACTION_RESP);
         broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
         broadcastIntent.putExtra(PARAM_OUT_MSG, resultTxt);
         sendBroadcast(broadcastIntent);
     }
 } 

Via Log.d(), I can see the onHandleIntent was never called even though the service was started. My Log.d trail goes after startService(msgIntent); and stops there. No Log.d from within onHandleIntent was displayed. What is wrong? And what can I do to make it work?

Thanks!

Vipul
  • 27,808
  • 7
  • 60
  • 75
Jason Ching
  • 1,037
  • 4
  • 19
  • 27

1 Answers1

1

I solved mine by adding onStartCommand but not overriding it.

 public int onStartCommand(Intent intent, int flags, int startId) //sometimes overriding onStartCommand will not call onHandleIntent
{

    Logger_.logtoFile(tag, "here..!", 1);
    return super.onStartCommand(intent,flags,startId);
}
mboy
  • 744
  • 7
  • 17
  • 1
    I'm confused... how is this not overriding `onStartCommand`? You are calling `super.onStartCommand()` with the same arguments... so that means you are overriding it? – Alex Lockwood May 24 '13 at 05:05
  • @AlexLockwood in this approach only called `onStartCommand()` from the parent, if you want to override the method you must use `@Override` annontation. – Agna JirKon Rx Jun 25 '15 at 19:18
  • 4
    Methods are still overriden in Java whether or not you add an "@Override" annotation. Adding annotations doesn't affect the runtime behavior of any Java program. – Alex Lockwood Jun 25 '15 at 19:42