2

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

.... tb1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

    // Auto-generated method stub
    startService(Intent this.Main);
}
});
}

public void onStart(Intent intent, int startid) {

    Toast.makeText(context, "yessssss", Toast.LENGTH_LONG).show();
 //and do something//

}

当用户单击“tb1”按钮时,我想启动服务,

我试过了:

startService(new Intent(this, Main.class));

startService(Main.class);

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

4

4 回答 4

4

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

Intent intent = new Intent(Current_Activity.this, Main.class);
startService(intent); 

并确保您已在 Manifast.xml 中将您的服务注册为:

<service
    android:name=".Main"/>
于 2012-11-30T05:08:50.363 回答
0

试试这个

Intent myIntent = new Intent(this, Main.class);
startService(myIntent); 

还将您的服务添加到清单中

<service android:name="packagename.Main"></service> 
于 2012-11-30T05:07:42.423 回答
0
Intent myIntent = new Intent(this, Main.class);
startService(myIntent); 

没有编译删除参数以匹配 'Intent()' 出现带有 X 标记。

于 2013-09-19T08:25:58.877 回答
0
Intent myIntent = new Intent(this, Main.class);
startService(myIntent); 

没有编译。删除要匹配Intent()的参数以 X 标记出现。

一个小的改变就会起作用。

Intent myIntent = new Intent(MainActivity.this, MyService.class); //is working both
startService(myIntent); and stopService(myIntent); // also working.
于 2013-09-19T08:30:31.807 回答