微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐