Unable to start service Absicht { cmp=com.marie.mainactivity/.BackgroundService }: nicht gefunden

Ich studiert habe aus dem Buch "Pro Android 2." Ich arbeite über ein Service-Beispiel besteht aus zwei Klassen: BackgroundService.java und MainActivity.java. Die MainActivity Ansprüche (irrtümlich?) es beginnt der Service, wie angezeigt, durch Ausgabe in logcat aus dem Protokoll.d Anruf unter:

    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));
                }
            });
        }
    }

Was mich verwirrt ist die Benutzeroberfläche bietet zwei Schaltflächen: Bind-und UnBind-wie oben gezeigt. Aber nach der Dokumentation wenn onBind (), wie unten gezeigt, wird null zurückgegeben, der angibt, die Sie nicht zulassen möchten, verbindlich. Aber, wie oben gezeigt, wird die onClick () - Methode (die Schaltfläche "Binden") bindBtn.setOnClickListener(new OnClickListener () - Aufrufe startService(backgroundService), wodurch dieser Fehler:"Unable to start service Absicht { cmp=com.marie.mainactivity/.BackgroundService }: nicht gefunden"

    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);
        }
    }

Ich nicht verstehen, den Punkt dieses Beispiel. Wenn onBind() gibt null zurück, was ist der Punkt, der mit einem Bind-Taste (bindBtn)? Ich dachte, der Punkt war, zu zeigen, wie man einen BackgroundService. Aber es scheint nicht zu funktionieren, es sei denn, ich bin fehlt etwas.

Sollte ich hinzufügen, habe ich Hinzugefügt, um meine AndroidManifest.xml:

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

wie folgt:

<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>
  • Haben Sie Ihren .BackgroundService an Ihre manifest.xml ?
  • ja, ich habe wie man oben sehen kann.
  • aber ich habe es in der falschen Stelle, wie Sie sehen können, von @CaspNZ Antwort unten.
InformationsquelleAutor Marie | 2011-06-11
Schreibe einen Kommentar