gpt4 book ai didi

android - 错误 :undefined intent constructor when start service in android

转载 作者:太空宇宙 更新时间:2023-11-03 12:19:38 24 4
gpt4 key购买 nike

我尝试在 Activity 中启动服务。但它显示错误,如“构造函数 Intent(SampleService, MyService) 未定义”

我的服务.java

public class MyService extends Service {

@Override
public IBinder onBind(Intent intent) {
return null;
}

public static boolean isInstanceCreated() {
return instance != null;
}

@Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");

instance = this;
}

@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
instance = null;

}

@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(getBaseContext(), "Service started",Toast.LENGTH_SHORT).show();
}


}

从 SampleService.java 启动服务

  public class SampleService extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_activity);
Intent myintent =new Intent(SampleService.this,MyService.this);//Error show here..
startService(myintent);
}
}

服务在 list 文件中初始化。

    <service android:enabled="true" android:name="com.MyApp.MyService" />

帮我解决错误。

最佳答案

不是Service.this 你必须通过类(class)所以像这样改变..

Intent myintent =new Intent(SampleService.this,MyService.Class);

关于android - 错误 :undefined intent constructor when start service in android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20068783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com