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

4

我一直在学习《Pro Android 2》这本书。我正在学习一个包含两个类的服务示例:BackgroundService.java和MainActivity.java。MainActivity声称(错误地?)它启动了服务,如下面的Log.d调用所示:

    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,如上所示。但根据文档,如果onBind()返回null,则表示您不想允许绑定。但是,如上所示,(Bind按钮的)bindBtn.setOnClickListener(new OnClickListener()的onClick()方法调用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,那么有一个绑定按钮(bindBtn)有什么意义呢?我以为重点是展示如何启动BackgroundService。但是它似乎不起作用,除非我漏了什么。

我应该补充说明的是,我已经将以下内容添加到我的AndroidManifest.xml文件中:

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

as follows:

<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>

你是否已将你的 .BackgroundService 添加到了 manifest.xml 文件中? - Ye Myat Min
@Ye,是的,正如您在上面所看到的,我已经这样做了。 - Marie
@Ye,但是我添加的位置不对,正如@CaspNZ下面的回答所示。 - Marie
1个回答

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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接