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
John Conde
  • 217,595
  • 99
  • 455
  • 496
user1123931
  • 479
  • 1
  • 8
  • 24

1 Answers1

2

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

user1123931
  • 479
  • 1
  • 8
  • 24