在onStartCommand中调用Intent intent,它具有Intent参数。但是为什么没有在没有Intent参数的onCreate中调用intent。你能告诉我如何调用onCreate中没有Intent的意图吗? 参数。
MainActivity.java public void onButtonClicked(View v){
Intent it = new Intent(getApplicationContext(), MyService.class);
startService(it);
}
MyService.java 公共类MyService扩展了Service {
@Override
public void onCreate() {
Activity ac = new Activity();
Intent intent = ac.getIntent();
if(intent != null) {
Log.d("MyService", "intent exists");
} else {
Log.d("MyService", "intent doesn't exist");
}
super.onCreate();
}
===> '意图不存在'在日志中。
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(intent != null) {
Log.d("MyService", "intent exists");
} else {
Log.d("MyService", "intent doesn't exist");
}
return super.onStartCommand(intent, flags, startId);
}
===>意图存在'在日志中。
答案 0 :(得分:0)
在manifest.xml文件中添加它
<service android:name=".Your_Service_NAme" />