Can't get IntentService example to work

1.6k views Asked by At

I'm trying to learn IntentService and followed this example http://code.tutsplus.com/tutorials/android-fundamentals-intentservice-basics--mobile-6183 I listed my code. It starts service but onHandleIntent is not started.

What I'm doing wrong?

Thanks!

MainActivity

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends Activity {

public  ResponseReceiver receiver;

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

    final Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            EditText input = (EditText) findViewById(R.id.sisesta);
            String strInputMsg = input.getText().toString();
            Intent msgIntent = new Intent(MainActivity.this, SimpleIntentService.class);

            Log.d("proov", strInputMsg);

            msgIntent.putExtra(SimpleIntentService.PARAM_IN_MSG, strInputMsg);
            startService(msgIntent);
        }
    });

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

public class ResponseReceiver extends BroadcastReceiver {
    public static final String ACTION_RESP = "alar.alar.com.intentasi.MESSAGE_PROCESSED";

    @Override
    public void onReceive(Context context, Intent intent) {
        TextView result = (TextView) findViewById(R.id.textView);
        String text = intent.getStringExtra(SimpleIntentService.PARAM_OUT_MSG);
        result.setText(text);
    }
  }
}

service

import android.app.IntentService;
import android.content.Intent;
import android.os.SystemClock;
import android.text.format.DateFormat;
import android.util.Log;
import android.widget.Toast;

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) {

    String msg = intent.getStringExtra(PARAM_IN_MSG);

    Log.d("proov", msg);

    SystemClock.sleep(3000); // 3 seconds
    String resultTxt = msg + " "
            + DateFormat.format("MM/dd/yy h:mmaa", System.currentTimeMillis());

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // Let it continue running until it is stopped.
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    return START_STICKY;
  }
}
1

There are 1 answers

0
MadEqua On

According to the IntentService docs:

onStartCommand(Intent intent, int flags, int startId)

You should not override this method for your IntentService.

So you should delete onStartCommand() from your class (or remove @Override, but that will leave a unused method behind).