首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android服务无法在清单中启动...但是什么都没有?

Android服务无法在清单中启动...但是什么都没有?
EN

Stack Overflow用户
提问于 2011-03-02 12:44:57
回答 1查看 5.4K关注 0票数 0

更新:

在添加了建议的方法(doBindService()doUnbindService())以及@Nick Campion建议的来自here的调用之后

我已经尝试了一段时间来运行这个服务,但似乎什么都不起作用-我知道我可能遗漏了一个分号或其他什么:)

程序调用startNotificationService(),然后日志显示日志消息...并且该应用程序继续运行,而不显示Service。我找不到高级任务杀手服务。救命!

XML (在清单中):

代码语言:javascript
复制
    <service 
        android:icon="@drawable/icon" 
        android:label="Smart Spdate Service"
        android:name="notifyService">
    <intent-filter 
        android:label="FULL_PATH_NAME_HERE.updateService">
    </intent-filter>
</service>

服务呼叫

代码语言:javascript
复制
    Log.v("NOTICE", "Notification Service was not found running - starting");
    //startService(new Intent(this, notifyService.class));
    startService(new Intent(notifyService.class.getName()));
    //startService(new Intent(TweetCollectorService.class.getName()));

     /* FROM GOOGLE */
     void doBindService() {
    // Establish a connection with the service.  We use an explicit
    // class name because we want a specific service implementation that
    // we know will be running in our own process (and thus won't be
    // supporting component replacement by other applications).
    this.bindService(new Intent(this, updateService.class), mConnection, Context.BIND_AUTO_CREATE);
    mIsBound = true;
}

void doUnbindService() {
    if (mIsBound) {
        // Detach our existing connection.
        unbindService(mConnection);
        mIsBound = false;
    }
}
/* END OF GOOGLE CODE */
@Override
public void onDestroy() {
    web.close();
    doUnbindService(); // Added to `onDestroy` - suggested by Google page
    super.onDestroy();
    Log.v("NOTICE", "PROGRAM TERMINATED");
}

updateService.java

代码语言:javascript
复制
public class updateService extends Service {
private String TAG = "SERVICE";
public static final int INTERVAL = 60000;
private Timer timer = new Timer();
private static updateService Pointer;

public updateService() {
    Pointer = updateService.this;
}

public static class LocalBinder extends Binder {
    static updateService getService() {
        return Pointer;
    }
}

@Override
public void onCreate() {
    super.onCreate();
}

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

@Override
public void onStart(Intent intent, int startId) {
    timer.scheduleAtFixedRate(new TimerTask() {
        public void run() {
            doStuff();
        }
    }, 0, INTERVAL);
    super.onStart(intent, startId);
}

public void doStuff() {
    Log.v(TAG, "doStuff");
}

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

private final IBinder mBinder = new LocalBinder();

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-03-02 12:55:06

我看不到您的客户端绑定到您的服务的任何地方。看一看local service example.。即使调用startService也要使用绑定模式的原因是因为startService调用是异步的。您需要进行额外的调用来绑定服务,以确保在启动完成后收到回调。

我发现在NPR Open Source App中有一个非常好的clientservice服务示例,可供您学习!

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5163487

复制
相关文章

相似问题

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