首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Android中启动服务

在Android中启动服务
EN

Stack Overflow用户
提问于 2011-01-22 09:16:39
回答 4查看 299.2K关注 0票数 119

我想要在某个活动开始时调用服务。因此,下面是Service类:

代码语言:javascript
复制
public class UpdaterServiceManager extends Service {

    private final int UPDATE_INTERVAL = 60 * 1000;
    private Timer timer = new Timer();
    private static final int NOTIFICATION_EX = 1;
    private NotificationManager notificationManager;

    public UpdaterServiceManager() {}

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        // Code to execute when the service is first created
    }

    @Override
    public void onDestroy() {
        if (timer != null) {
            timer.cancel();
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startid) {
        notificationManager = (NotificationManager) 
                getSystemService(Context.NOTIFICATION_SERVICE);
        int icon = android.R.drawable.stat_notify_sync;
        CharSequence tickerText = "Hello";
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, tickerText, when);
        Context context = getApplicationContext();
        CharSequence contentTitle = "My notification";
        CharSequence contentText = "Hello World!";
        Intent notificationIntent = new Intent(this, Main.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);
        notification.setLatestEventInfo(context, contentTitle, contentText,
                contentIntent);
        notificationManager.notify(NOTIFICATION_EX, notification);
        Toast.makeText(this, "Started!", Toast.LENGTH_LONG);
        timer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                // Check if there are updates here and notify if true
            }
        }, 0, UPDATE_INTERVAL);
        return START_STICKY;
    }

    private void stopService() {
        if (timer != null) timer.cancel();
    }
}

我是这样称呼它的:

代码语言:javascript
复制
Intent serviceIntent = new Intent();
serviceIntent.setAction("cidadaos.cidade.data.UpdaterServiceManager");
startService(serviceIntent);

问题是什么都没有发生。上面的代码块在活动的onCreate的末尾被调用。我已经调试过了,没有抛出异常。

有什么想法吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-01-22 10:19:05

可能您的清单中没有该服务,或者它没有与您的操作相匹配的<intent-filter>。检查LogCat (通过adb logcat、DDMS或Eclipse中的DDMS透视图)应该会发现一些可能有帮助的警告。

更有可能的是,您应该通过以下方式启动服务:

代码语言:javascript
复制
startService(new Intent(this, UpdaterServiceManager.class));
票数 287
EN

Stack Overflow用户

发布于 2012-09-24 03:01:45

代码语言:javascript
复制
startService(new Intent(this, MyService.class));

仅仅写这一行对我来说是不够的。服务仍然不起作用。只有在清单中注册了服务之后,一切才能正常工作

代码语言:javascript
复制
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

    ...

    <service
        android:name=".MyService"
        android:label="My Service" >
    </service>
</application>
票数 85
EN

Stack Overflow用户

发布于 2015-06-15 12:53:04

我喜欢让它更有活力

代码语言:javascript
复制
Class<?> serviceMonitor = MyService.class; 


private void startMyService() { context.startService(new Intent(context, serviceMonitor)); }
private void stopMyService()  { context.stopService(new Intent(context, serviceMonitor));  }

别忘了宣言

代码语言:javascript
复制
<service android:enabled="true" android:name=".MyService.class" />
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4765517

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档