Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6255227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:19:33+00:00 2026-05-24T14:19:33+00:00

I’m fairly new to Android programming and to Java in general, but I have

  • 0

I’m fairly new to Android programming and to Java in general, but I have extensive experience as a system-level C coder. I wanted to create an API or library that could be used by a suite of Android apps that I plan to develop and, after much research, decided that a bound service was the Android way of doing that (I would have normally created a .so or a .dll). If this assumption is incorrect, then my entire question could very well be moot.

At any rate, I created my service’s framework in one project and I created an activity in another to test the service. I stubbed out the service’s handleMessage() method with calls to Toast just to see if it was working and then added two buttons to my activity: one to bind the service and the second to unbind it. After about an hour of not being able to run my activity because it couldn’t see my service, I figured out how to get Eclipse to see the source code of the service from the activity and thought I was well on my way. Then I ran the activity.

Everything looked good. Two buttons, one to bind, one to unbind. However, when I pressed the bind button, it force closed the app. I used the debugger to see what was going on and it is force closing on the bindService() call in my activity. Of course, the bindService() call was copied from another example in which a new Intent() was created on the same line, so I separated out the two lines of code and it is really dying when I attempt to create the Intent.

Here is the code for my activity:

package net.william.android.myAPI.tester;

import net.william.android.myAPI.MyApiService;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Toast;

public class MyApiTester extends Activity
{
    boolean mIsBound = false;

    private ServiceConnection mConnection = new ServiceConnection()
    {
        public void onServiceConnected(ComponentName className, IBinder service)
        {
            Toast.makeText(FaceClientTester.this, "Remote Service Connected", Toast.LENGTH_SHORT).show();
        }

        public void onServiceDisconnected(ComponentName className)
        {
            Toast.makeText(FaceClientTester.this, "Remote Service Disconnected", Toast.LENGTH_SHORT).show();
        }
    };

    public void doBindService(View view)
    {
        if (!mIsBound)
        {
            Toast.makeText(this, "Binding Service", Toast.LENGTH_SHORT).show();
            Intent myIntent = new Intent(MyApiTester.this, MyApiService.class); 
            bindService(myIntent, mConnection, Context.BIND_AUTO_CREATE);
            mIsBound = true;
        }
    }

    public void doUnbindService(View view)
    {
        if (mIsBound)
        {
            unbindService(mConnection);
            mIsBound = false;
            Toast.makeText(this, "Service Unbound", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        return;
    }
}

This is the code for my service:

package net.william.android.myAPI

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.widget.Toast;

public class MyApiService extends Service
{
    static final int API_FN_1   = 101;
    static final int API_FN_2   = 102;

    class MyApiHandler extends Handler
    {
        @Override
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
                case API_FN_1:
                    ApiFn1();
                    break;
                case API_FN_2:
                    ApiFn2();
                    break;
                default:
                    super.handleMessage(msg);
                    break;
            }
        }
    }

    final Messenger myMessenger = new Messenger(new MyApiHandler());

    @Override
    public void onCreate()
    {
        Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroy()
    {
        Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show();
    }

    @Override
    public IBinder onBind(Intent intent)
    {
        Toast.makeText(this, "Service Bound", Toast.LENGTH_SHORT).show();
        return myMessenger.getBinder();
    }

    private void ApiFn1()
    {
        Toast.makeText(this, "API Function 1", Toast.LENGTH_SHORT).show();
        return;
    }

    private void ApiFn2()
    {
        Toast.makeText(this, "API Function 2", Toast.LENGTH_SHORT).show();
        return;
    }
}

I changed some of the names of things, but that’s really the meat of my code so far. Just a bunch of calls to Toast so that I could hopefully see that the thing is working.

All of the tutorials that I have found on the subject have completely ignored the manifest. I poked around a little last night trying to tweak manifest options thinking that that might have something to do with it, but to no avail. At any rate, for completeness, the following is the manifest code for the activity:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.william.android.myAPI.tester"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MyApiTester" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

and the service:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.william.android.myAPI"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <service android:name=".MyApiService"></service>
    </application>
</manifest>

I tried adding the following inside of the <service> block, but it didn’t seem to help:

            <intent-filter>
                <action android:name="net.william.android.myAPI.MyApiService" />
            </intent-filter>

Anyway, this is my first post to your forum, so I apologize if there is too much or too little information, or if I missed some really easy fix out there somewhere on the web. I’ve been up most of the night reading all of the documentation on services that Google had and reading post after post in forums, so, at this point, I could very easily overlook something. Thanks in advance to anyone who finds the time to help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T14:19:34+00:00Added an answer on May 24, 2026 at 2:19 pm

    If these are to be distributed as two separate APK files, as indicated via your pair of manifests, you cannot use the Intent constructor that takes a Java class as a parameter. The fact that this even compiles indicates your project setup is somewhat scary — your client should not be within a country mile of your service from a build-time standpoint.

    Add an <intent-filter> to your <service> advertising how you want to be connected to, and use that structure in your client for the Intent it creates.

    For example, in this pair of sample projects, the service has:

    <service android:name=".BshService">
        <intent-filter>
            <action android:name="com.commonsware.android.advservice.IScript" />
        </intent-filter>
    </service>
    

    and the client therefore uses:

    new Intent("com.commonsware.android.advservice.IScript")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.