Instead you should consider using a Service (I'd prefer IntentService [1]). In that Service you can handle the message sending without block the main thread. Combine that with the AlarmManager [2] to trigger the service every time you want to send messages (so you won't need any of that ugly Thread.sleep stuff).
[1] http://developer.android.com/reference/android/app/IntentService.html
[2] http://developer.android.com/reference/android/app/AlarmManager.html
Am Samstag, 24. November 2012 21:25:29 UTC+1 schrieb Bernd Roth:
Hi Guys!I have the following problem:The user, who is using the app. should be able to define when he wants to send his or her SMS.Therefore I have a textfield, where the user can define when to send the next SMS ( in minutes! ).Now my problem is that I have two buttons: SMS send and Cancel .When the user presses the SMS send button an SMS should be sent and this SMS should be sent every x minutes ( the minutes the user has already defined in the textfield ).But the Cancel button should cancel this process and here is my problem: As soon as the user presses SMS send the Ui hangs and the user is not able to press the cancel button anymore.I know I can solve the problem with a Thread or maybe with the "doInBackground" method but I am not sure whether I am on the right track!Can anyone help me please, I am not sure whether I am right!Thank you very much!Here is my code:package com.example.smsmessageregulary; import android.app.Activity;import android.app.PendingIntent;import android.content.ContentResolver; import android.content.Intent;import android.database.Cursor;import android.os.Bundle;import android.provider.Contacts;import android.provider.ContactsContract; import android.telephony.gsm.SmsManager; import android.view.View;import android.widget.Button;import android.widget.EditText;public class SMSRegulary extends Activity{Button btnSendSMS, PhonePicker;EditText txtPhoneNo;EditText txtMessage;EditText txtCounter;int counter = 0;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState); setContentView(R.layout.activity_smsregulary); btnSendSMS = (Button) findViewById(R.id.btnSendSMS);txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);txtMessage = (EditText) findViewById(R.id.txtMessage);txtCounter = (EditText) findViewById(R.id.txtCounter);PhonePicker = (Button) findViewById(R.id.picker);PhonePicker.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) {// TODO Auto-generated method stubContentResolver cr = getContentResolver();Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);if (cur.getCount() > 0) {while (cur.moveToNext()) {String id = cur.getString(cur.getColumnIndex( ContactsContract.Contacts._ID) ); String name = cur.getString(cur.getColumnIndex( ContactsContract.Contacts. DISPLAY_NAME)); System.out.println(name);if (Integer.parseInt(cur.getString(cur.getColumnIndex( ContactsContract.Contacts.HAS_ PHONE_NUMBER))) > 0) { //Query phone here. Covered nextSystem.out.println("HERE");}}}}});btnSendSMS.setOnClickListener(new View.OnClickListener() {public void onClick(View v){String phoneNo = txtPhoneNo.getText().toString(); String message = txtMessage.getText().toString(); String Repeater = txtCounter.getText().toString(); Repeater = Repeater + "000";Integer r = Integer.valueOf(Repeater);long l = r.longValue();if (phoneNo.length()>0 && message.length()>0 && Repeater.length() == 0)sendSMS(phoneNo, message);else {while ( counter <= 99 ) {sendSMS(phoneNo, message);try {Thread.sleep(l);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}});}//---sends an SMS message to another device---private void sendSMS(String phoneNumber, String message){PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, SMSRegulary.class), 0);SmsManager sms = SmsManager.getDefault();sms.sendTextMessage(phoneNumber, null, message, pi, null); }}


You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
No comments:
Post a Comment