user user - 2 months ago 20
JSON Question

send sms using services in android

I am able to send the SMS in the normal way.Here is my code:

public class MainActivity extends Activity {
// Log tag
private static final String TAG = MainActivity1.class.getSimpleName();

// Movies json url
private static final String url = "http://xxx.xxx.x.xx/sms/read_file.php";
private ProgressDialog pDialog;
private List<SmsClass> movieList = new ArrayList<SmsClass>();
private ListView listView;
private CustomListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, movieList);
listView.setAdapter(adapter);

pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();

// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();

// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
SmsClass movie = new SmsClass();


movie.setTitle(obj.getString("contents"));
movie.setRating(obj.getString("mobno"));
movieList.add(movie);

String phoneNo = "9876543210";
String msg = obj.getString("contents");
sendSMS(phoneNo, msg); //working perfect without monitoring

} catch (JSONException e) {
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});

// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
}

@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}

private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
//working perfect
private void sendSMS(String phoneNumber, String message) {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
}


Here am getting the message body from json data and sending sms.How can I do this same function in background without having any userinterface i.e by using services?

Answer

Declare service manifest file in application tag.

   <application
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name"
       android:theme="@style/AppTheme" >

       <service android:name=".MyService" />

   </application>

MainActivity.java

  public class MainActivity extends Activity {

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

          startService();
       }


       // Method to start the service
       public void startService() {
          startService(new Intent(getBaseContext(), MyService.class));
       }

       // Method to stop the service
       public void stopService() {
          stopService(new Intent(getBaseContext(), MyService.class));
       }
    }

MyService.java

public class MyService extends Service {

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

   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {
      // Let it continue running until it is stopped.
      Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
      String phoneNo = "9876543210";
      String msg = obj.getString("contents");                        
      sendSMS(phoneNo, msg);
      return START_STICKY;
   }

   @Override
   public void onDestroy() {
      super.onDestroy();
      Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
   }

   private void sendSMS(String phoneNumber, String message) {
      SmsManager sms = SmsManager.getDefault();
      sms.sendTextMessage(phoneNumber, null, message, null, null);
   }
}
Comments