undefined intent constructor错误?

我在Activity中尝试启动Service,但出现“undefined intent constructor”的报错信息。
MyService.java代码如下:

public class MyService extends Service { @Override
public IBinder onBindIntent intent { return null;
} public static boolean isInstanceCreated { return instance != null; } @Override
public void onCreate { Toast.makeTextthis, "My Service Created", Toast.LENGTH_LONG.show; Log.dTAG, "onCreate"; instance = this;
} @Override
public void onDestroy { Toast.makeTextthis, "My Service Stopped", Toast.LENGTH_LONG.show; Log.dTAG, "onDestroy"; instance = null; } @Override
public void onStartIntent intent, int startid { Toast.makeTextgetBaseContext, "Service started",Toast.LENGTH_SHORT.show; } }

启动SampleService.java的代码如下:

 public class SampleService extends Activity{
@Override
protected void onCreateBundle savedInstanceState { super.onCreatesavedInstanceState; setContentViewR.layout.grid_activity; Intent myintent =new IntentSampleService.this,MyService.this;//Error show here.. startServicemyintent; } }

在manifest file中设定service的初值如下:

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

请大家帮我解决这个错误。

原问题:Error:undefined intent constructor when start service in android

答:kalyan pvs最佳答案)
你不应该使用Service.this,而应该按如下方法改变class:

Intent myintent =new IntentSampleService.this,MyService.Class;

答:Raghunandan
做如下调整:

Intent myintent =new IntentSampleService.this,MyService.this;

变为:

Intent myintent =new IntentSampleService.this,MyService.Class; // first param is a context second param is a class in your case a MyServiceClass

你没有设置类似于IntentSampleService, MyService的构造函数,在intent constructor参数设定上出现错误。

public Intent Context packageContext, Class<?> cls Added in API level 1
Create an intent for a specific component. All other fields action, data, type, class are null, though they can be modified later with explicit calls. This provides a convenient way to create an intent that is intended to execute a hard-coded class name, rather than relying on the system to find an appropriate class for you; see setComponentComponentName for more information on the repercussions of this. Parameters
packageContext A Context of the application package implementing this class.
cls The component class that is to be used for the intent.

发表评论

电子邮件地址不会被公开。 必填项已用*标注