无法启动服务Intent {cmp = com.marie.mainactivity / .BackgroundService}:未找到

时间:2011-06-11 03:03:22

标签: android service bind

我一直在研究“Pro Android 2”一书。我正在研究一个由两个类组成的Service示例:BackgroundService.java和MainActivity.java。 MainActivity声称(错误地?)它启动服务,如下面的Log.d调用输出到logcat所示:

    public class MainActivity extends Activity {
        private static final String TAG = "MainActivity";

        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            Log.d(TAG, "starting service");

            Button bindBtn = (Button)findViewById(R.id.bindBtn);
            bindBtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    Intent backgroundService = new Intent(MainActivity.this, com.marie.mainactivity.BackgroundService.class);
                    startService(backgroundService);
                }
            });

            Button unbindBtn = (Button)findViewById(R.id.unbindBtn);
            unbindBtn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    stopService(new Intent(MainActivity.this, BackgroundService.class));
                }
            });
        }
    }

让我感到困惑的是UI提供了两个按钮:Bind和UnBind,如上所示。但是根据documentation如果onBind()如下所示返回null,表示你不想允许绑定。但是如上所示,(Bind按钮)的onClick()方法bindBtn.setOnClickListener(新的OnClickListener()调用startService(backgroundService),它给出了这个错误:“无法启动服务Intent {cmp = com.marie.mainactivity / .BackgroundService }:未找到“

    public class BackgroundService extends Service {
        private NotificationManager notificationMgr;

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

            notificationMgr = NotificationManager)getSystemService(NOTIFICATION_SERVICE);

            displayNotificationMessage("starting Background Service");

            Thread thr = new Thread(null, new ServiceWorker(), "BackgroundService");
            thr.start();
        }   

        class ServiceWorker implements Runnable
        {
            public void run() {
                // do background processing here...

                //stop the service when done...
                //BackgroundService.this.stopSelf();
            }
        }

        @Override
        public void onDestroy()
        {
            displayNotificationMessage("stopping Background Service");
            super.onDestroy();
        }

        @Override
        public void onStart(Intent intent, int startId) {
            super.onStart(intent, startId);
        }

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

        private void displayNotificationMessage(String message)
        {
            Notification notification = new Notification(R.drawable.note, message, System.currentTimeMillis());

            PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);

            notification.setLatestEventInfo(this, "Background Service", message, contentIntent);

            notificationMgr.notify(R.id.app_notification_id, notification);
        }
    }

我不明白这个例子的意义。如果onBind()返回null,那么有一个Bind按钮(bindBtn)是什么意思?我认为重点是展示如何启动BackgroundService。但它似乎没有用,除非我遗漏了什么。

我应该添加我添加到我的AndroidManifest.xml:

    <service android:name=".BackgroundService"></service>

如下:

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".MainActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <service android:name=".BackgroundService"></service>
    </activity>

</application>

1 个答案:

答案 0 :(得分:4)

从活动内部删除服务。它与应用程序中的活动处于同一级别。例如:

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".MainActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".BackgroundService"></service>

</application>