hi I am developing an application where I need to get the unread SMS from my phone. I need to check my inbox once a fixed amount of time. I have written the code to retrieve the unread SMS. I have included a timer which checks the unread SMS every 30 seconds. But the problem is it checks only when the app is open. I need to check even when the app is not open. Please provide any suggestions and modification I need to make. Below is my code.

MainActivity

```
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends ActionBarActivity {

MyReceiver myReceiver;
ListView lViewSMS;
ArrayList datapassed;
//private static final String TAG_SUCCESS = "success";

//String URL = "http://10.10.234.232/test_android/index2.php";

// JSONParser jsonParser = new JSONParser();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lViewSMS = (ListView) findViewById(R.id.listViewSMS);

    //String []r;
    // r=new String[2];

    // if (r != null) {
    //@SuppressWarnings("unchecked")
    //ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, fetchInbox());
    //lViewSMS.setAdapter(adapter);/
    //AttemptLogin attemptLogin = new AttemptLogin();
    //attemptLogin.execute(r[0],r[1]);
  //  @SuppressWarnings("unchecked")
   // ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, datapassed);
    //lViewSMS.setAdapter(adapter);

}

@Override
protected void onStart() {
    // TODO Auto-generated method stub

    //Register BroadcastReceiver
    //to receive event from our service
    myReceiver = new MyReceiver();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(MyService.MY_ACTION);
    registerReceiver(myReceiver, intentFilter);

    //Start our own service
    Timer t = new Timer();
    t.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {

            Intent intent = new Intent(MainActivity.this, MyService.class);
            startService(intent);
        }
    },
            0,
            30000);
    super.onStart();
}

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    unregisterReceiver(myReceiver);
    super.onStop();
}

private class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub

        datapassed = arg1.getStringArrayListExtra("DATAPASSED");
       // @SuppressWarnings("unchecked")
       // ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, datapassed);
       // lViewSMS.setAdapter(adapter);
        Toast.makeText(MainActivity.this,
                "Triggered by Service!\n"
                        + "Data passed: " + datapassed,
                Toast.LENGTH_LONG).show();

    }

}
}

```

MyService.java

```
import android.app.Service;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.IBinder;

import java.util.ArrayList;

public class MyService extends Service {
final static String MY_ACTION = "MY_ACTION";

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub

    MyThread myThread = new MyThread();
    myThread.start();

    return super.onStartCommand(intent, flags, startId);
}
public class MyThread extends Thread{

    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
            //Thread.sleep(5000);

            Intent intent = new Intent();
            intent.setAction(MY_ACTION);
            if(fetchInbox()!=null) {
                intent.putExtra("DATAPASSED", fetchInbox());

                sendBroadcast(intent);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        stopSelf();
    }

}
ArrayList fetchInbox (){

    final Uri SMS_INBOX = Uri.parse("content://sms/inbox");

    //Retrieves all SMS (if you want only unread SMS, put "read = 0" for the 3rd parameter)
    Cursor cursor = getContentResolver().query(SMS_INBOX, null,"read=0", null, null);
    ArrayList sms = new ArrayList();
    //Get all lines
    String read=" ";
    String body=" ";
    String[] arr = new String[2];
    while (cursor.moveToNext()) {
        //Gets the SMS information
        String address = cursor.getString(cursor.getColumnIndex("address"));
        String person = cursor.getString(cursor.getColumnIndex("person"));
        String date = cursor.getString(cursor.getColumnIndex("date"));
        String protocol = cursor.getString(cursor.getColumnIndex("protocol"));
        read = cursor.getString(cursor.getColumnIndex("read"));
        String status = cursor.getString(cursor.getColumnIndex("status"));
        String type = cursor.getString(cursor.getColumnIndex("type"));
        String subject = cursor.getString(cursor.getColumnIndex("subject"));
        body = cursor.getString(cursor.getColumnIndex("body"));

        sms.add(address+"\n"+person+"\n"+body+"\n");
        //Do what you want

        //   else
        // {
        //   AttemptLogin attemptLogin = new AttemptLogin();
        // attemptLogin.execute("", "");
        //}
    }
    //return arr;
    //return body;
    return sms;
}
}

```