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

标签 android service

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

.... 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);

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

最佳答案

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

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

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

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

关于android - 单击按钮时启动服务时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13639334/

相关文章:

android - 从 Adob​​e Air android 应用程序发布到 facebook 墙

android - 空指针异常 - 获取 EditText 值

windows - 如何在安装时而不是每次启动时一劳永逸地将参数传递给 Windows 服务

Android:服务类中的 intent.putExtra 以获取通知

android - 前台服务不会在启动时启动

android - 使用可折叠工具栏和页面适配器时请求布局调用不当

Android - View.OnLayoutChangeListener 和 ViewTreeObserver.OnGlobalLayoutListener 的区别

grails - 如何将参数传递到我从 gsp 获得的服务中,并且我在 Grails 的 Controller 中拥有

c++ - 从服务启动进程

java - 在java中解码SVG图像文件(编码为base64)