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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:27:28+00:00 2026-06-11T15:27:28+00:00

My app requirements The application will have an activity and a service. The service

  • 0

My app requirements

  • The application will have an activity and a service.

  • The service has a sms contentobserver.

  • After application installation the Service should run all the time irrespective of
    application active or not

  • The registration and unregistration of contentobserver inside the service should
    be controlled from the activity.

  • On uninstallation of the application the service should be destroyed.

I tried some code. In oncreate of the service i have done resgitartion of content observer
and ondestroy i unregistered it. I have used start and stop service from the activity.
But even after stop service the onchange method of the content observer is still getting called.

please let me know some sample code and the manifest definition of this service.

public class MyActivity extends ListActivity implements OnInitListener {


@Override
protected void onDestroy() {

    super.onDestroy(); // let it be here as per the android TTS samples

}

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


    Intent sintent = new Intent(MyActivity.this,MyService.class);
    MyActivity.this.startService(sintent);

}



private boolean SSMyservice() {
    // TODO Auto-generated method stub


    //stop service      
    Intent sintent = new Intent(MyActivity.this,MyService.class);
    MyActivity.this.stopService(sintent);

    //do some work

    //start service again
    MyActivity.this.startService(sintent); //start service again


    return true;
} //importdata





@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
    case R.id.TTSread:
        return true;    
    case R.id.SSS:
        SSMyservice();
        return true;                        
    }

    return super.onOptionsItemSelected(item);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {




//   Log.v(TAG,"option menu created");

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.layout.omenu, menu);
    return true;
}


}

------------end MyActivity---------------------
----------------------------------

public class MyService extends Service {  

protected SmsObserver smsSentObserver=null;


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

    @Override          
    public void onCreate() 
    {

        registersmsevent();

    }   


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        super.onStartCommand(intent, flags, startId);

         return Service.START_STICKY;  

    }



    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        unregistersmsevent();
    }



    public void registersmsevent() {
        // TODO Auto-generated method stub
        if(smsSentObserver == null)
        {
        final Uri SMS_STATUS_URI = Uri.parse("content://sms");  
        smsSentObserver = new SmsObserver(new Handler());
        MyService.this.getContentResolver().registerContentObserver(SMS_STATUS_URI, true, smsSentObserver); 
        }

    }

    public void unregistersmsevent() {
        if(smsSentObserver != null)
        {
            MyService.this.getContentResolver().unregisterContentObserver(smsSentObserver); 
            smsSentObserver = null;
        }
    }



    public class SmsObserver extends ContentObserver {



        Handler handler; 

          public SmsObserver(Handler handler) {        
              super(handler);
              // TODO Auto-generated constructor stub                
              this.handler = handler; 
          }



          @Override 
        public boolean deliverSelfNotifications() {
            return true;
        }

          @Override 
        public void onChange(boolean selfChange) {

              //my code........

            super.onChange(selfChange);
        }



    }//End of class SmsObserver


}//end of service
  • 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-11T15:27:29+00:00Added an answer on June 11, 2026 at 3:27 pm

    I could fix the issue. This was a timing issue. Stopping service takes sometime.

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

Sidebar

Related Questions

I have the following requirements for an application that many people will be using
We have some special requirements: From our application, launch a ClickOnce application. It will
I have a Java web application that has a 'disconnected' Java Swing desktop app.
I have an Android application that has a service that runs in the background,
The deployment requirements for my app (a windows service written in C#) state the
An Android/Iphone app will be accessing application data from the server. [Django-Python] How can
I'm designing the intranet web application for our company. One of the app. requirements
Here's the scenario I have an ASP.NET 4.0 application which has a LOT of
Here's a scenario: I have a java front end (RCP/SWT) app which currently has
I'm working on a REST service that has a few requirements: It has to

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.