Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to start service Intent { cmp=com.marie.mainactivity/.BackgroundService }: not found
    primarykey
    data
    text
    <p>I've been studying from the book "Pro Android 2." I'm working through a Service example that consists of two classes: BackgroundService.java and MainActivity.java. The MainActivity claims (erroneously?) it starts the Service as indicated by output to logcat from the Log.d call below:</p> <pre><code> 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)); } }); } } </code></pre> <p>What puzzles me is the UI provides two buttons: Bind and UnBind as shown above. But according to the <a href="http://developer.android.com/guide/topics/fundamentals/services.html" rel="nofollow">documentation</a> if onBind() as shown below returns null that indicates you don't want to allow binding. But as shown above the onClick() method of (the Bind button) bindBtn.setOnClickListener(new OnClickListener() calls startService(backgroundService) which gives this error:"Unable to start service Intent { cmp=com.marie.mainactivity/.BackgroundService }: not found"</p> <pre><code> 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); } } </code></pre> <p>I don't understand the point of this example. If onBind() returns null what's the point of having a Bind button (bindBtn)? I thought the point was to show how to start a BackgroundService. But it doesn't seem to work unless I'm missing something.</p> <p>I should add I have added to my AndroidManifest.xml:</p> <pre><code> &lt;service android:name=".BackgroundService"&gt;&lt;/service&gt; </code></pre> <p>as follows:</p> <pre><code>&lt;application android:icon="@drawable/icon" android:label="@string/app_name"&gt; &lt;activity android:name=".MainActivity" android:label="@string/app_name"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;service android:name=".BackgroundService"&gt;&lt;/service&gt; &lt;/activity&gt; &lt;/application&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload