无法使IntentService示例正常工作

时间:2015-06-20 17:06:54

标签: android intentservice

我正在尝试学习IntentService并遵循此示例http://code.tutsplus.com/tutorials/android-fundamentals-intentservice-basics--mobile-6183 我列出了我的代码。它启动服务但是onHandleIntent没有启动。

我做错了什么?

谢谢!

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);
    }
  }
}

服务

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 个答案:

答案 0 :(得分:0)

根据IntentService文档:

  

onStartCommand(Intent intent,int flags,int startId)

     

您不应该为IntentService覆盖此方法。

因此,您应该从班级中删除onStartCommand()(或删除@Override,但这会留下未使用的方法。)