android - 为什么我不能启动我的 IntentService?

标签 android android-intent intentservice

我有两个相对简单的代码。一项主要 Activity 和一项 Intent 服务。

主要 Activity

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

Intent 服务

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

Logcat 说

    06-26 04:16:00.545: W/ActivityManager(67): Unable to start service Intent { 
cmp=com.intentservicetest1/.SimpleIntentService (has extras) }: not found

我该怎么做才能纠正这个问题?

最佳答案

确保您已在 AndroidManifest.xml 中将 IntentService 注册为:

<service android:name=".SimpleIntentService" />

关于android - 为什么我不能启动我的 IntentService?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11200801/

相关文章:

android - IntentFilter 的作用是什么?

java - 从通知栏打开目录

android - 在运行时更改抽屉导航项目

android - 摇动手势不适用于具有 native react 的物理设备

安卓和谷歌分析

android - 如何创建自定义锁屏小部件(我只想显示一个按钮)

android - 应用程序进程被杀死后如何管理正在运行的 IntentService

android - CountDownTimer 在 IntentService 中不工作

android - java.lang.RuntimeException 无法实例化服务 : java. lang.NullPointerException

android - 如何为 Android 同步状态图标制作动画?