Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 单击按钮启动服务时出现问题_Android_Service - Fatal编程技术网

Android 单击按钮启动服务时出现问题

Android 单击按钮启动服务时出现问题,android,service,Android,Service,我的应用程序中有以下代码: 。。。。tb1.setOnClickListener(新的OnClickListener(){ 我想在用户点击“tb1”按钮时启动服务 我试过: startService(新意图(this,Main.class)) 及 startService(主类) 但他们都没有开始服务,我该怎么办?试试这个 Intent myIntent = new Intent(this, Main.class); startService(myIntent); 还要将您的服务添加到清单中

我的应用程序中有以下代码:

。。。。tb1.setOnClickListener(新的OnClickListener(){

我想在用户点击“tb1”按钮时启动服务

我试过:

startService(新意图(this,Main.class))

startService(主类)

但他们都没有开始服务,我该怎么办?

试试这个

Intent myIntent = new Intent(this, Main.class);
startService(myIntent); 
还要将您的服务添加到清单中

<service android:name="packagename.Main"></service> 

试试这个

Intent myIntent = new Intent(this, Main.class);
startService(myIntent); 
还要将您的服务添加到清单中

<service android:name="packagename.Main"></service> 

更改您的代码,以便在按钮上启动服务单击:

Intent intent = new Intent(Current_Activity.this, Main.class);
startService(intent); 
并确保已在Manifast.xml中将您的服务注册为:

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

更改您的代码,以便在按钮上启动服务单击:

Intent intent = new Intent(Current_Activity.this, Main.class);
startService(intent); 
并确保已在Manifast.xml中将您的服务注册为:

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

这不是编译 删除参数以匹配“Intent()”以X标记出现

Intent myIntent = new Intent(this, Main.class);
startService(myIntent); 
这不是编译 删除参数以匹配“Intent()”以X标记出现

Intent myIntent = new Intent(this, Main.class);
startService(myIntent); 
未编译。删除参数以匹配
Intent()
出现时带有X标记

Intent myIntent = new Intent(this, Main.class);
startService(myIntent); 
一个小小的改变就行了

Intent myIntent = new Intent(MainActivity.this, MyService.class); //is working both
startService(myIntent); and stopService(myIntent); // also working.
未编译。删除参数以匹配
Intent()
出现时带有X标记

Intent myIntent = new Intent(this, Main.class);
startService(myIntent); 
一个小小的改变就行了

Intent myIntent = new Intent(MainActivity.this, MyService.class); //is working both
startService(myIntent); and stopService(myIntent); // also working.


它仍然没有启动服务。它没有工作。我可以补充一点,我当前的_活动与Main.class相同,我的意思是我编辑了您的代码,如下所示:Intent Intent=new Intent(Main.this,Main.class);not您可以尝试`Intent Intent Intent=new Intent(getApplicationContext(),Main.class);`intead of
Intent Intent Intent=new Intent(Main.this,Main.class);
您的服务名是什么?我的服务名是Main,我在同一个服务中使用“bt1”按钮,我的意思是我想用服务内部的按钮启动服务。@Reza\u Rg:然后使用
Intent Intent=new Intent(getApplicationContext(),Main.class);启动服务(Intent)
对于启动服务,它仍然没有启动服务。它不起作用。我可以补充一点,我当前的活动与Main.class相同,我的意思是我编辑了您的代码,如下所示:Intent Intent=new Intent(Main.this,Main.class);not您可以尝试`Intent Intent=new Intent(getApplicationContext(),Main.class)` intead of
Intent Intent Intent=new Intent(Main.this,Main.class);
您的服务名是什么?我的服务名是Main,我在同一个服务中使用“bt1”按钮,我的意思是我想用服务内部的按钮启动服务。@Reza_Rg:然后使用
Intent Intent=new Intent(getApplicationContext(),Main.class);startService(意向)
用于启动服务在清单中提供包含包名的
服务的完整路径。什么?你的意思是我使用Tab活动代替服务?我不明白。@hotveryspicy我这样做了,但它仍然不起作用。在清单中提供包含包名的
服务的完整路径。什么?你的意思是代替服务se选项卡活动?我不明白。@hotveryspicy我明白了,ST像这样“”,但它仍然不起作用。它不起作用。我可以加上这一点,我当前的_活动与Main.class相同,我的意思是我编辑了你的代码,就像这样Intent Intent=new Intent(Main.this,Main.class);它不起作用。我可以补充一点,我当前的_活动与Main.class相同,我的意思是我编辑了您的代码,就像Intent Intent=new Intent(Main.this,Main.class);