错误:在android中启动服务时未定义的意图构造函数

错误:在android中启动服务时未定义的意图构造函数,第1张

概述我尝试在activity中启动服务.但它显示错误,如“构造函数Intent(SampleService,MyService)未定义” MyService.java public class MyService extends Service {@Overridepublic IBinder onBind(Intent intent) { return null;}public 我尝试在activity中启动服务.但它显示错误,如“构造函数Intent(SampleService,MyService)未定义”

MyService.java

public class MyService extends Service {@OverrIDepublic IBinder onBind(Intent intent) {    return null;}public static boolean isinstanceCreated() {       return instance != null;    }@OverrIDepublic voID onCreate() {    Toast.makeText(this,"My Service Created",Toast.LENGTH_LONG).show();    Log.d(TAG,"onCreate");     instance = this;}@OverrIDepublic voID onDestroy() {    Toast.makeText(this,"My Service Stopped","onDestroy");    instance = null;}@OverrIDepublic voID onStart(Intent intent,int startID) {            Toast.makeText(getBaseContext(),"Service started",Toast.LENGTH_SHORT).show();    }}

从SampleService.java启动服务

public class SampleService extends Activity{@OverrIDeprotected 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);      } }

清单文件中初始化的服务.

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

帮我解决错误.

解决方法 不是Service.this你必须通过课程
所以这样改变..

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

以上是内存溢出为你收集整理的错误:在android中启动服务时未定义的意图构造函数全部内容,希望文章能够帮你解决错误:在android中启动服务时未定义的意图构造函数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1122145.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存