Android无法在本地服务中调用方法,而Googles示例代码将无法编译

问题描述 投票:1回答:1

我正在尝试在locakl服务上创建一个客户端调用函数。我在google上提供了以下文档。其中说本地服务可以使用Bind界面,google android上的url文件和示例代码我使用http://developer.android.com/guide/topics/fundamentals/bound-services.html#Creating但是我无法获得有服务的示例代码,要编译!代码所在的位置

public class LocalBinder extends Binder {

        // error here //    LocalService getService() {
                // Return this instance of LocalService so clients can call public methods
           // error hear //     return LocalService.this;
            }

我收到一条错误,说LocalService无法解析为某种类型。我以为我错过了一个导入。我试图谷歌android localservice我得到另一个linbk谷歌上的samople代码做同样的事情http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/LocalService.html它有同样的错误!!!!

完整的代码

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.os.Binder;
import android.widget.Toast;


    public class BackgroundService extends Service {
        // Binder given to clients
        private final IBinder mBinder = new LocalBinder();
        // Random number generator
        private final Random mGenerator = new Random();

        /**
         * Class used for the client Binder.  Because we know this service always
         * runs in the same process as its clients, we don't need to deal with IPC.
         */
        public class LocalBinder extends Binder {

            LocalService getService() {
                // Return this instance of LocalService so clients can call public methods
                return LocalService.this;
            }

        }

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

        /** method for clients */
        public int getRandomNumber() {
          return mGenerator.nextInt(100);
        }
    }
android service bind
1个回答
1
投票

您将服务称为“BackgroundService”。在粘合剂中返回。 (在两个地方将LocalSerivce更改为BackgroundSerivice)

© www.soinside.com 2019 - 2024. All rights reserved.