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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:33:15+00:00 2026-06-01T07:33:15+00:00

I am trying to learn Android app development and wrote a very simple app

  • 0

I am trying to learn Android app development and wrote a very simple app consisting of an activity that calls a service. The service broadcasts measured acceleration to the activity. The problem is that the service runs ok but it does not send data back to the activity. i.e, onReceive on my receiver is never called. Also, when the activity ends, there is an exception saying that my receiver has not been registered. Below is my code for the service, activity and manifest.xml. Any help would be very much appreciated.

Activity calling service:

package com.practice;
import com.practice.SimpleService;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;

public class ServiceActivity extends Activity {
MyReceiver myReceiver=null;
Intent i;
static final String LOG_TAG = "ServiceActivity";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    Log.d( LOG_TAG, "onCreate" );
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.main);

    //Start service 
    i= new Intent(this, com.practice.SimpleService.class);
    Log.d( LOG_TAG, "onCreate/startService" );  
}
@Override 
public void onResume(){
    super.onResume();
    Log.d( LOG_TAG, "onResume/registering receiver" );  
    //Register BroadcastReceiver to receive accelerometer data from service
    //if (myReceiver == null){
        myReceiver = new MyReceiver();
        IntentFilter intentFilter = new IntentFilter();      
        intentFilter.addAction(SimpleService.MY_ACTION);
        startService(i);  
        registerReceiver(myReceiver, intentFilter);
    //}     
}

@Override 
public void onPause(){
    super.onPause();
    Log.d( LOG_TAG, "onPause/unregistering receiver" ); 
    stopService(i);

    if (myReceiver != null)unregisterReceiver(myReceiver);      
}

@Override
protected void onStop(){
    super.onStop();
    Log.d( LOG_TAG, "onStop" );
    if (myReceiver != null) unregisterReceiver (myReceiver);
    stopService(i);
}

private class MyReceiver extends BroadcastReceiver{
    static final String Log_Tag = "MyReceiver";
    @Override
    public void onReceive(Context arg0, Intent arg1){
        Log.d( LOG_TAG, "onReceive" );
        String measurement = arg1.getStringExtra("measurement");        
        System.out.println("I am here");
    }

}   

}

Service getting sensor data:

package com.practice;
import java.util.List;
import android.app.Service;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.util.Log;
import android.widget.TextView;

public class SimpleService extends Service implements SensorEventListener{
 final static String MY_ACTION = "MY_ACTION";
   private TextView output;
   private String reading;
   private SensorManager mgr;
   private List<Sensor> sensorList;
   static final String LOG_TAG = "SimpleService";
   Intent intent = new Intent("com.practice.SimpleService.MY_ACTION");

   @Override
   //public void onStartCommand() {
   public void onCreate() {
      Log.d( LOG_TAG, "onStartCommand" );
      mgr = (SensorManager) getSystemService(SENSOR_SERVICE);
      sensorList = mgr.getSensorList(Sensor.TYPE_ACCELEROMETER);
      for (Sensor sensor : sensorList) {
         mgr.registerListener(this, sensor,
                 SensorManager.SENSOR_DELAY_NORMAL);
      }
   }

   @Override
   public void onDestroy() {
        Log.d( LOG_TAG, "onDestroy" );
        mgr.unregisterListener(this);       
        super.onDestroy();
   }

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub      
}

@Override
public void onSensorChanged(SensorEvent event) {
      Log.d( LOG_TAG, "onSensorChanged" );
      StringBuilder builder = new StringBuilder();

      for (int i = 0; i < event.values.length; i++) {
         builder.append("   [");
         builder.append(i);
         builder.append("] = ");
         builder.append(event.values[i]);
         builder.append("\n");
      }

      reading=builder.toString();

      //Send back reading to Activity
      intent.putExtra("measurement", reading);
      sendBroadcast(intent);        
}

}

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.practice"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ServiceActivity"
            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=".SimpleService" ></service>
    </application>
</manifest>
  • 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-01T07:33:17+00:00Added an answer on June 1, 2026 at 7:33 am

    Create a Custom Intent :

    final static String MY_ACTION = "com.practice.SimpleService.MY_ACTION";
    

    In Manifest.xml

    <receiver android:name=".MyReceiver" android:enabled="true">
      <intent-filter>
         <action android:name="com.practice.SimpleService.MY_ACTION"></action>
      </intent-filter>
    </receiver>
    

    for more information for Custom Broadcast see Custom Intents and Broadcasting with Receivers

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to learn android app development .I am using IntelliJ Idea 10.When
I'm trying to learn how to build apps for Android. The first simple app,
I am an android app developer, recently I am trying to learn Marmalade to
I'm trying to learn to develop with Phonegap and JQM. My app is very
I'm rather new to Android development, and am trying to learn with a bit
I am very much new to android and trying to learn various techniques including
I'm trying to learn Html5-based web application development on android OS. But what confuses
I'm trying to learn android and for my app i have few questions. If
I am trying to learn Android Development and I was wondering how I can
I'am trying to learn how to create services in Android and I've made very

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.