服务上的OnBind()总是返回False - Android

Tof*_*ira 6 service notifications android android-service android-context

我正在尝试绑定服务,但onBind()总是返回false.

这是 - 的代码ServiceConnection-

private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        // This is called when the connection with our service has been established, 
        // giving us the service object we can use to interact with our service.
        mBoundService = ((ScheduleService.ServiceBinder) service).getService();
    }

    public void onServiceDisconnected(ComponentName className) {
        mBoundService = null;
    }
};
Run Code Online (Sandbox Code Playgroud)

这是对bindService()-

boolean test = getApplicationContext().bindService(new Intent(this, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE);
Run Code Online (Sandbox Code Playgroud)

这是清单中服务的声明 -

<service android:name=".Notifications.ScheduleService" android:enabled="true"/>
Run Code Online (Sandbox Code Playgroud)

我已经阅读过有关该主题的先前问题,但找不到答案(尝试使用Application上下文切换Activity上下文,但它没有帮助).

我正在使用Frgaments和ActionBarSherlock,我的Activity扩展了SlidingFragmentActivity(这就是为什么我正在使用应用程序上下文,这没有帮助).

编辑 - 这是我正在尝试启动的服务的代码 -

public class ScheduleService extends Service {

    /**
     * Class for clients to access
     */
    public class ServiceBinder extends Binder {
        public ScheduleService getService() {
            return ScheduleService.this;
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("ScheduleService", "Received start id " + startId + ": " + intent);

        // We want this service to continue running until it is explicitly stopped, so return sticky.
        return START_STICKY;
    }

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

    // This is the object that receives interactions from clients. See
    private final IBinder mBinder = new ServiceBinder();

    /**
     * Show an alarm for a certain date when the alarm is called it will pop up a notification
     */
    public void setAlarm(Calendar c) {
        // This starts a new thread to set the alarm
        // You want to push off your tasks onto a new thread to free up the UI to carry on responding
        new AlarmTask(this, c).run();
    }
}
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激 .谢谢.

Str*_*ton 1

完全限定的类名是什么ScheduleService(即包括完整的包名)?

我问这个是因为在您的 AndroidManifest.xml 文件中,您的服务名称是.Notifications.ScheduleService,这看起来有点奇怪:

这告诉我

  • 包名(的最后一部分)包含一个大写 字符......不太好。如果是这样的话,
    我会期望相反。.notifications.ScheduleService
  • ScheduleService是在名为Notifications.java 的文件中定义的。.Notifications$ScheduleService如果是这种情况,我会期望相反(美元符号而不是句号)。