peekService返回null即使startService不 [英] peekService returns null even though startService does not

查看:1026
本文介绍了peekService返回null即使startService不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Android 服务广播接收器。在服务住在不同的应用程序比广播接收器。我的理解是这样做的正确方法是先调用上下文#startService ,后跟广播接收器#peekService 。到 startService 呼叫似乎正常工作,因为它返回预期的组件名。然而,当我拨打电话到 peekService ,返回。对我在做什么任何想法错了吗?

感谢您!下面是相关部分的code上市。

  //有问题的服务是com.tingley.myapp.MyService
//注意,这个接收器在不同的应用程序
包com.tingley.myotherapp;公共类MyReceiver扩展广播接收器{  @覆盖
  公共无效的onReceive(上下文的背景下,意图意图){    //开始服务
    意图serviceIntent =新的Intent();
    serviceIntent.setClassName(com.tingley.myapp,com.tingley.myapp.MyService);
    组件名componentNameOfStartedService = context.startService(serviceIntent);
    Log.d(,启动服务名称:+ componentNameOfStartedService);    //获取一个IBinder到服务
    的IBinder的ServiceBinder = peekService(背景下,serviceIntent);
    Log.d(,从服务偷看了粘结剂:+的ServiceBinder);
  }
}

登录报表打印以下内容:

 启动的服务名称:ComponentInfo {com.tingley.myapp / com.tingley.myapp.MyService}
得到了来自粘结剂偷看服务:空


解决方案

有关文档<一个href=\"http://developer.android.com/reference/android/content/BroadcastReceiver.html#peekService(android.content.Context,%20android.content.Intent)\"相对=nofollow> peekService()的不完全描述以获得一个IBinder所需的条件。作为戴安娜Hackborn(谷歌Android工程师)解释这个帖子: 叫 startService()是不够的 - 你需要有一个名为 bindService()的目的相同,所以该系统已检索到的的IBinder 吧。

I'm trying to use an Android Service from a BroadcastReceiver. The Service lives in a different application than the BroadcastReceiver. My understanding is that the right way to do this is to first call Context#startService, followed by BroadcastReceiver#peekService. The call to startService seems to work correctly, as it returns the expected ComponentName. However, when I make the call to peekService, null is returned. Any thoughts on what I'm doing wrong?

Thank you! Here is a code listing with the relevant parts.

// The Service in question is com.tingley.myapp.MyService
// Note that this Receiver is in a different application
package com.tingley.myotherapp;

public class MyReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {

    // Start the Service
    Intent serviceIntent = new Intent();
    serviceIntent.setClassName("com.tingley.myapp", "com.tingley.myapp.MyService");
    ComponentName componentNameOfStartedService = context.startService(serviceIntent);
    Log.d("", "Started service name: " + componentNameOfStartedService);

    // Get an IBinder to the Service
    IBinder serviceBinder = peekService(context, serviceIntent);
    Log.d("", "Got binder from peeking service: " + serviceBinder);
  }
}

The Log statements print the following:

Started service name: ComponentInfo{com.tingley.myapp/com.tingley.myapp.MyService}
Got binder from peeking service: null

解决方案

The documentation for peekService() does not fully describe the conditions required to get an IBinder. As Dianne Hackborn (Google Android engineer) explains in this post: "Calling startService() is not enough -- you need to have called bindService() for the same intent, so the system had retrieved the IBinder for it".

这篇关于peekService返回null即使startService不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆