- Forum-Beiträge: 98
24.11.2012, 20:23:53 via Website
24.11.2012 20:23:53 via Website
Ich bin gerade dabei eine App zu programmieren, die mir z. B: alle 5 Minuten eine SMS versendet.
Das Versenden funktioniert bereits problemlos!
Als GUI habe ich ein paar Eingabefelder, wie die Telefonnumer, Message und einen Counter ( nicht ganz glücklich gewählt den Namen, eigentlich sollten es die Minuten sein, die der User angeben kann ), der bestimmt den Zeitraum, in denen jeweils eine SMS versendet wird!
Zudem besitzt die App noch 2 Buttons, einen der die SMS versendet und einen anderen, der das ganze Procedere stoppen sollte.
Jetzt ist es so, wenn ich den Button SMS senden drücke, dann läuft das versenden in einer while - Schleife mit einem Thread.sleep, was aber auch gleichzeitig dazu führt, dass natürlich die UI hängt und ich den Button "Cancel" nicht mehr betätigen kann!
Ich denke mir, ich muss das Versenden der SMS ev. in den Hintergrund (doInBackground) od. in einen Thread vll. auslagern!
Habe mir das mit "doInBackground" schon angesehen, nur ist das Versenden der SMS ja leider in der Activity ...
Kann mir vll. jemand hierbei weiterhelfen, nicht dass ich auf der falschen Fährte mich befinde.
Danke vielmals!
Hier mein Code einmal:
2 import android.app.Activity;
3import android.app.PendingIntent;
4import android.content.ContentResolver;
5import android.content.Intent;
6import android.database.Cursor;
7import android.os.Bundle;
8import android.provider.Contacts;
9import android.provider.ContactsContract;
10import android.telephony.gsm.SmsManager;
11import android.view.View;
12import android.widget.Button;
13import android.widget.EditText;
14
15public class SMSRegulary extends Activity
16{
17 Button btnSendSMS, PhonePicker;
18 EditText txtPhoneNo;
19 EditText txtMessage;
20 EditText txtCounter;
21 int counter = 0;
22
23 /** Called when the activity is first created. */
24 @Override
25 public void onCreate(Bundle savedInstanceState)
26 {
27 super.onCreate(savedInstanceState);
28 setContentView(R.layout.activity_smsregulary);
29
30 btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
31 txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
32 txtMessage = (EditText) findViewById(R.id.txtMessage);
33 txtCounter = (EditText) findViewById(R.id.txtCounter);
34 PhonePicker = (Button) findViewById(R.id.picker);
35
36 PhonePicker.setOnClickListener(new View.OnClickListener(){
37
38 public void onClick(View v) {
39 // TODO Auto-generated method stub
40 ContentResolver cr = getContentResolver();
41 Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
42 null, null, null, null);
43 if (cur.getCount() > 0) {
44 while (cur.moveToNext()) {
45 String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
46 String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
47 System.out.println(name);
48 if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
49 //Query phone here. Covered next
50 }
51 }
52 }
53
54 }
55 });
56
57 btnSendSMS.setOnClickListener(new View.OnClickListener()
58 {
59 public void onClick(View v)
60 {
61 String phoneNo = txtPhoneNo.getText().toString();
62 String message = txtMessage.getText().toString();
63 String Repeater = txtCounter.getText().toString();
64 Repeater = Repeater + "000";
65 Integer r = Integer.valueOf(Repeater);
66 long l = r.longValue();
67 if (phoneNo.length()>0 && message.length()>0 && Repeater.length() == 0)
68 sendSMS(phoneNo, message);
69 else {
70 while ( counter <= 99 ) {
71 sendSMS(phoneNo, message);
72 try {
73 Thread.sleep(l);
74 } catch (InterruptedException e) {
75 // TODO Auto-generated catch block
76 e.printStackTrace();
77 }
78 }
79 }
80 }
81 });
82 }
83
84 //---sends an SMS message to another device---
85 private void sendSMS(String phoneNumber, String message)
86 {
87 PendingIntent pi = PendingIntent.getActivity(this, 0,
88 new Intent(this, SMSRegulary.class), 0);
89 SmsManager sms = SmsManager.getDefault();
90 sms.sendTextMessage(phoneNumber, null, message, pi, null);
91 }
92}
2 android:orientation="vertical"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 >
6 <TextView
7 android:layout_width="fill_parent"
8 android:layout_height="wrap_content"
9 android:text="Enter the phone number of recipient"
10 />
11 <EditText
12 android:id="@+id/txtPhoneNo"
13 android:layout_width="fill_parent"
14 android:layout_height="wrap_content"
15 />
16 <TextView
17 android:layout_width="fill_parent"
18 android:layout_height="wrap_content"
19 android:text="Message"
20 />
21 <EditText
22 android:id="@+id/txtMessage"
23 android:layout_width="fill_parent"
24 android:layout_height="150px"
25 android:gravity="top"
26 />
27 <TextView
28 android:layout_width="fill_parent"
29 android:layout_height="wrap_content"
30 android:text="How often?"
31 />
32 <EditText
33 android:id="@+id/txtCounter"
34 android:layout_width="fill_parent"
35 android:layout_height="50px"
36 android:gravity="top"
37 />
38
39 <RelativeLayout
40 android:layout_width="match_parent"
41 android:layout_height="wrap_content" >
42
43 <Button
44 android:id="@+id/button1"
45 android:layout_width="wrap_content"
46 android:layout_height="wrap_content"
47 android:layout_alignParentRight="true"
48 android:layout_alignParentTop="true"
49 android:layout_marginRight="41dp"
50 android:text="Cancel" />
51
52 <Button
53 android:id="@+id/btnSendSMS"
54 android:layout_width="wrap_content"
55 android:layout_height="wrap_content"
56 android:layout_alignParentLeft="true"
57 android:layout_alignParentTop="true"
58 android:layout_marginLeft="39dp"
59 android:text="Send SMS" />
60
61 </RelativeLayout>
62
63 <LinearLayout
64 android:layout_width="match_parent"
65 android:layout_height="wrap_content"
66 android:orientation="vertical" >
67
68 <LinearLayout
69 android:layout_width="match_parent"
70 android:layout_height="wrap_content"
71 android:orientation="vertical" >
72
73 <LinearLayout
74 android:layout_width="match_parent"
75 android:layout_height="wrap_content"
76 android:orientation="vertical" >
77
78 <Button
79 android:id="@+id/picker"
80 android:layout_width="match_parent"
81 android:layout_height="wrap_content"
82 android:text="PhonePicker" />
83
84 </LinearLayout>
85
86 <LinearLayout
87 android:layout_width="match_parent"
88 android:layout_height="wrap_content"
89 android:orientation="vertical" >
90 </LinearLayout>
91
92 </LinearLayout>
93
94 <LinearLayout
95 android:layout_width="match_parent"
96 android:layout_height="wrap_content"
97 android:layout_weight="4.18" >
98
99 <ListView
100 android:layout_width="match_parent"
101 android:layout_height="wrap_content" >
102
103 </ListView>
104
105 </LinearLayout>
106 </LinearLayout>
107</LinearLayout>
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.