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 8332947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:51:59+00:00 2026-06-09T02:51:59+00:00

I have this Service class: public class BluetoothService extends Service { private static Activity

  • 0

I have this Service class:

 public class BluetoothService extends Service {

    private static Activity mActivity;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        this.registerReceiver(bluetoothReceiver, intentFilter);     
    }

    @Override
    public void onDestroy() {
        if (bluetoothReceiver != null) {
            this.unregisterReceiver(bluetoothReceiver);
        }       
    }

    @Override
    public void onStart(Intent intent, int startid) {
           //
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {        
        return START_STICKY;
    }

    public static BroadcastReceiver bluetoothReceiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
                TextView tvStatus = (TextView) mActivity.findViewById(R.id.tvtatus);
                Messaging.appendMessage(tvStatus, Bluetooth.getDeviceState(state));
                if (Bluetooth.isBluetoothEnabled()) {
                    Messaging.appendMessage(tvStatus, Bluetooth.showMessage());
                }               
            }
        }
    };  
}

And in my Activity class, I have this:

public class MainActivity extends Activity {

    private TextView tvStatus;
    private Intent intentBluetooth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       tvStatus = (TextView)findViewById(R.id.tvtatus);

       intentBluetooth = new Intent(this, BluetoothService.class);
       startService(intentBluetooth);
    }
}

The BroadcastReceiver method (bluetoothReceiver) in the Service class is never called. I don’t know why. If I have the IntentFilter and the BroadcastReceiver codes above all in an Activity, then it works – but not in a [separate] Service. I’m stumped.

My AndroidManifest.xml is:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.onegoal.androidexample"
        android:versionCode="1"
        android:versionName="1.0.0" 
        android:installLocation="auto"
        android:hardwareAccelerated="true">

        <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="15" />

        <uses-permission android:name="android.permission.BLUETOOTH" />
        <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
        <uses-feature android:name="android.hardware.bluetooth" android:required="false" /> 

        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" 
            android:debuggable="true" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name"
                android:theme="@android:style/Theme.NoTitleBar" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <service android:name=".BluetoothService">
            </service>
        </application>  
    </manifest>

I’m new to Android so what I’m doing may not be the best. Hope someone can help me.

  • 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-06-09T02:52:00+00:00Added an answer on June 9, 2026 at 2:52 am

    maybe the fact that your receiver is static causing the problem.

    BroadcastReceiver should never be static. it can cause lots of problems.

    other really bad design problem with your code – holding reference to activity inside service, and using it to modify views is really wrong thing to do. it can cause easily to memory leek.
    the right why to communicate between Service and Activity is by implement android’s Messanger, or sending broadcasts between them via BroadcastReceiver.

    if you’ll listen to my advice – you won’t be have to make your receiver static (I guess you’ve made it static only because you are using the mActivity static instance inside)
    and I’m pretty sure it will solve your problem

    you can read about Messanger here: http://developer.android.com/reference/android/os/Messenger.html

    sure you’ll find lots of usage examples in the net.

    example of broadcasting updates to the activity from service:

    public class MyService extends Service {
    
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    
    @Override
    public void onCreate() {
        super.onCreate();
    
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        this.registerReceiver(bluetoothReceiver, intentFilter);
    }
    
    @Override
    public void onDestroy() {
        if (bluetoothReceiver != null) {
            this.unregisterReceiver(bluetoothReceiver);
        }
        super.onDestroy();
    }
    
    public BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
                updateUIWithNewState(state);
            }
        }
    };
    
    protected void updateUIWithNewState(int state) {
        Intent intent = new Intent("serviceUpdateReceivedAction");
        intent.putExtra("state", state);
        sendBroadcast(intent);
    }
    
    }
    

    and that’s the activity:

    public class MyActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        Intent intent = new Intent(this, MyService.class);
        startService(intent);
    }
    
    @Override
    protected void onResume() {
        super.onResume();
    
        registerReceiver(mServiceUpdatesReceiver, new IntentFilter("serviceUpdateReceivedAction"));
    }
    
    @Override
    protected void onPause() {
        unregisterReceiver(mServiceUpdatesReceiver);
        super.onPause();
    }
    
    private BroadcastReceiver mServiceUpdatesReceiver = new BroadcastReceiver() {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            int state = intent.getIntExtra("state", -1);
            // do what ever you want in the UI according to the state
        }
    };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this service: public class AAAService extends Service { private Intent requestService; private
I have this code that describes a service: public class navigation_web extends Service {
I have this test class to test a remote service: public class CoreServiceBasicTest extends
I have two entities like this: public class Service { public virtual int ServiceId
I currently have service classes that look something like this public class UserService :
I have this code in my WCF Service: public class MyImage { public Image
Lets say I have this service: public class Service { ... public Service(IFactory factory)
So let's say I have something like this: public class Service : IService {
based on this example : @Service public class Purchase { @PersistenceContext private EntityManager em;
I have the following situation : Class ServiceCaller: public class ServiceCaller{ private Service service;

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.