Send SMS every x minutes

  • Replies:1
Bernd Roth
  • Forum posts: 98

Nov 24, 2012, 9:41:06 PM via Website

Hi guys!

I am having a problem with my app.

I can send an SMS but I want to send it every x minutes and I also want to stop sending an SMS.
Therefore I have two buttons: SMS send and Cancel.

Now when the user defines to send the same SMS every 10 Minutes then the UI stucks!

I think I have to handle the problem with a Thread or maybe with the "doInBackgrund" method, but I am not sure.

Maybe someone could give me a hint?

Thank you very much!

Here is my code:

1package com.example.smsmessageregulary;
2
3import android.app.Activity;
4import android.app.PendingIntent;
5import android.content.ContentResolver;
6import android.content.Intent;
7import android.database.Cursor;
8import android.os.Bundle;
9import android.provider.Contacts;
10import android.provider.ContactsContract;
11import android.telephony.gsm.SmsManager;
12import android.view.View;
13import android.widget.Button;
14import android.widget.EditText;
15
16public class SMSRegulary extends Activity
17{
18 Button btnSendSMS, PhonePicker;
19 EditText txtPhoneNo;
20 EditText txtMessage;
21 EditText txtCounter;
22 int counter = 0;
23
24
25 /** Called when the activity is first created. */
26 @Override
27 public void onCreate(Bundle savedInstanceState)
28 {
29 super.onCreate(savedInstanceState);
30 setContentView(R.layout.activity_smsregulary);
31
32 btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
33 txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
34 txtMessage = (EditText) findViewById(R.id.txtMessage);
35 txtCounter = (EditText) findViewById(R.id.txtCounter);
36 PhonePicker = (Button) findViewById(R.id.picker);
37
38 PhonePicker.setOnClickListener(new View.OnClickListener(){
39
40 public void onClick(View v) {
41 // TODO Auto-generated method stub
42 ContentResolver cr = getContentResolver();
43 Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
44 null, null, null, null);
45 if (cur.getCount() > 0) {
46 while (cur.moveToNext()) {
47 String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
48 String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
49 System.out.println(name);
50 if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
51 //Query phone here. Covered next
52 System.out.println("HERE");
53 }
54 }
55 }
56
57 }
58
59 });
60
61 btnSendSMS.setOnClickListener(new View.OnClickListener()
62 {
63 public void onClick(View v)
64 {
65 String phoneNo = txtPhoneNo.getText().toString();
66 String message = txtMessage.getText().toString();
67 String Repeater = txtCounter.getText().toString();
68 Repeater = Repeater + "000";
69 Integer r = Integer.valueOf(Repeater);
70 long l = r.longValue();
71 if (phoneNo.length()>0 && message.length()>0 && Repeater.length() == 0)
72 sendSMS(phoneNo, message);
73 else {
74 while ( counter <= 99 ) {
75 sendSMS(phoneNo, message);
76 try {
77 Thread.sleep(l);
78 } catch (InterruptedException e) {
79 // TODO Auto-generated catch block
80 e.printStackTrace();
81 }
82 }
83 }
84 }
85 });
86 }
87
88 //---sends an SMS message to another device---
89 private void sendSMS(String phoneNumber, String message)
90 {
91 PendingIntent pi = PendingIntent.getActivity(this, 0,
92 new Intent(this, SMSRegulary.class), 0);
93 SmsManager sms = SmsManager.getDefault();
94 sms.sendTextMessage(phoneNumber, null, message, pi, null);
95 }
96}

Reply
Yasser Alnounou
  • Forum posts: 4

Nov 27, 2012, 5:51:32 PM via Website

You probably have an error in this line

ursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);

Would you please post the logcat?
I am waiting for you.

Reply