1
        Uri uriSMSURI = Uri.parse("content://sms/inbox");
        Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null);
        int i=0;
        while (cur.moveToNext()) {
            Phone_no=cur.getString(2);
            Time=cur.getLong(4);
            Message_body=cur.getString(11);
            Date dateObj = new Date(Time);
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");
            String Timenew = df.format(dateObj);
            Log.d(Tag,"INSIDE OF READ SMS INBOX");


           service.setClass(getBaseContext(), Background_Service.class);
           service.putExtra("Phone_no", Phone_no);
           service.putExtra("Message_body", Message_body);
           service.putExtra("Timenew", Timenew);
           getBaseContext().startService(service);
           } 

the above code read the message from the inbox. And i send those message to the service for further processing. Is this the right way to do. Should i create a queue for the service execution, if so how to create, implement a queue to the above code.

1 Answer 1

4

Yes, it is applicable way. But, i prefer another one.

Instead of starting a new service and routing new intents to it, you can create service once and bind to it, and send Messages to it.

First of all you need a messaging protocol. The easiest way it is create AIDL file with similar content.

package org.your.pkg;

interface IBackgroundService {
  void queueMsg(String phoneNo, String msgBody, String timeNew);
}

And then, you must implement it in your service.

class BackgroundService extends Service {

  IBackgroundService.Stub binder = new IBackgroundService.Stub() {
    public void queueMsg(String phoneNo, String msgBody, String timeNew) {
      // enqueue message to service thread. Do not process msg in this method.
    }
  };

  public IBinder getBinder() {
    return binder;
  }
}

And then, you must connect to your service.

private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        IBackgroundService yourService = IBackgroundService.Stub.asInterface(service);

        // calls to yourService.queueMsg(...)
    }

    public void onServiceDisconnected(ComponentName className) {
        Log.e(TAG, "Service has unexpectedly disconnected");
    }
};

There are more on this topic.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.