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

我一直在学习“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,new Intent(this,MainActivity.class),0);

            notification.setLatestEventInfo(this,"Background Service",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>

解决方法

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

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

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...