Android Can';无法使IntentService示例正常工作
我试图学习IntentService,并遵循了这个示例 我列出了我的代码。它启动服务,但onHandleIntent未启动 我做错了什么 谢谢 main活动Android Can';无法使IntentService示例正常工作,android,intentservice,Android,Intentservice,我试图学习IntentService,并遵循了这个示例 我列出了我的代码。它启动服务,但onHandleIntent未启动 我做错了什么 谢谢 main活动 import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter;
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;
}
}
根据文件:
onStartCommand(Intent-Intent、int-flags、int-startId)
您不应该为您的IntentService重写此方法
因此,您应该从类中删除
onStartCommand()
(或删除@Override
,但这将留下一个未使用的方法)。您怎么知道它没有被调用?您是否在AndroidManifest.xml中添加了IntentService名称?我有日志,但什么也没有发生,最后调用的是Toast“Service started”。AADTechnical-是,它被添加,服务被启动。我删除了这个过度帮助的onStart命令,现在它可以工作了。