首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android:从Activity传递参数给Service

Android:从Activity传递参数给Service
EN

Stack Overflow用户
提问于 2012-03-31 17:54:16
回答 3查看 42.7K关注 0票数 27

我通过这种方式绑定到服务:

活动类:

代码语言:javascript
复制
ListenLocationService mService;
@Override
public void onCreate(Bundle savedInstanceState) {
        ...
        Intent intent = new Intent(this, ListenLocationService.class);
        intent.putExtra("From", "Main");
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        ...
}

private ServiceConnection mConnection = new ServiceConnection() {

        public void onServiceConnected(ComponentName className,
                IBinder service) {
            LocalBinder binder = (LocalBinder) service;
            mService = binder.getService();         
        }

        public void onServiceDisconnected(ComponentName arg0) {
        }
    };

这是我的服务onBind方法

代码语言:javascript
复制
@Override
public IBinder onBind(Intent intent) {
    Bundle extras = intent.getExtras(); 
    if(extras == null)
        Log.d("Service","null");
    else
    {
        Log.d("Service","not null");
        String from = (String) extras.get("From");
        if(from.equalsIgnoreCase("Main"))
            StartListenLocation();
    }
    return mBinder;
}

所以我在LogCat中有" null“--尽管我在bindService之前做了intent.putExtra,但捆绑包是null

一般来说,Service工作得很好。但是,我只需要从应用程序的主活动调用StartListenLocation(); (我决定通过发送一个标志来实现这一点)。

如何将数据发送到服务?或者有另一种方法来检查启动了onBind的活动

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

https://stackoverflow.com/questions/9954878

复制
相关文章

相似问题

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