Alle x Minuten eine SMS versenden

  • Antworten:13
Bernd Roth
  • Forum-Beiträge: 98

24.11.2012, 20:23:53 via Website

Hallo Forum!

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:

1package com.example.smsmessageregulary;
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}

1<LinearLayout xmlns:android="..."
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>

Antworten
Carsten M.
  • Forum-Beiträge: 33.204

24.11.2012, 21:45:11 via App

Du brauchst einen Service. Dieser sorgt für den SMS Versand...

Herzliche Grüße

Carsten

Ich komm' mir langsam vor wie jemand, der ich bin // #cäthe

Antworten
Bernd Roth
  • Forum-Beiträge: 98

24.11.2012, 22:00:09 via Website

Hallo!

Danke Dir vielmals!

Dann werde ich mir das einmal mit dem Service ansehen!

Nur eine kurze Frage zum Service noch:
Ich muss vermutlich die Methode, in welcher mein SMS Versand im Moment gestartet wird, dann in die neue Klasse, welche auch die Klasse Service ableitet auslagern, oder irre ich mich da?

lG
Bernd

Antworten
Bernd Roth
  • Forum-Beiträge: 98

22.12.2012, 17:02:00 via Website

Hallo!

Ich habe meine Android SMS App soweit erweitert, dass nun in der Klasse SendSMS, welche die Klasse Service abgeleitet hat eine SMS verschickt werden kann. Jedoch ist mir weiters noch nicht bewusst, wie ich zum Beispiel alle 12 Minuten eine SMS versenden kann, dabei aber nicht den Hauptthread UI dabei blockiere!

Ich möchte gerne im Hauptthread UI 2 Buttons anzeigen: Send und Cancel Buttons und im Hintergrund soll nach dem Drücken des Send Buttons alle 12 Minuten eine SMS verschickt werden und mit dem Cancel Button soll das Ganze dann auch wieder abgebrochen werden!

Wenn mir jemand vll. bitte weiterhelfen könnte!
Danke Euch schon im vorhinein vielmals!

Hier mal mein Code:

1ste Klasse:

1package com.example.smsmanager;
2
3import android.os.Bundle;
4import android.app.Activity;
5import android.content.Intent;
6import android.view.Menu;
7import android.view.View;
8import android.widget.Button;
9import android.widget.EditText;
10
11public class MainActivity extends Activity {
12
13 Button send, cancel;
14 EditText phone;
15 EditText message;
16 EditText counter;
17 SendSMS sms;
18
19 @Override
20 protected void onCreate(Bundle savedInstanceState) {
21 super.onCreate(savedInstanceState);
22 setContentView(R.layout.activity_main);
23 send = (Button) findViewById(R.id.send);
24 cancel = (Button) findViewById(R.id.cancel);
25 phone = (EditText) findViewById(R.id.phone);
26 message = (EditText) findViewById(R.id.message);
27 counter = (EditText) findViewById(R.id.counter);
28
29 send.setOnClickListener(new View.OnClickListener() {
30 public void onClick(View v) {
31 explicitStart();
32 }
33 });
34 }
35
36 protected void explicitStart() {
37 // TODO Auto-generated method stub
38 Intent intent = new Intent(this, SendSMS.class);
39 startService(intent);
40 }
41
42 @Override
43 public boolean onCreateOptionsMenu(Menu menu) {
44 // Inflate the menu; this adds items to the action bar if it is present.
45 getMenuInflater().inflate(R.menu.activity_main, menu);
46 return true;
47 }
48}

2te Klasse:

1package com.example.smsmanager;
2
3import android.app.PendingIntent;
4import android.app.Service;
5import android.content.Intent;
6import android.os.IBinder;
7import android.telephony.SmsManager;
8
9public class SendSMS extends Service {
10 public void onCreate() {
11 super.onCreate();
12 sendSMS();
13 }
14
15 private void sendSMS() {
16 // TODO Auto-generated method stub
17 PendingIntent pi = PendingIntent.getActivity(this, 0,new Intent(this, SmsManager.class), 0);
18 SmsManager sms = SmsManager.getDefault();
19 sms.sendTextMessage("+436763506625", null, "test", pi, null);
20 }
21
22 @Override
23 public IBinder onBind(Intent intent) {
24 // TODO Auto-generated method stub
25 return null;
26 }
27}

Antworten
Gelöschter Account
  • Forum-Beiträge: 68

22.12.2012, 17:10:34 via App

Wenn die App fertig unbedingt mir zeigen !!!
Ich such sowas schon lange.

Antworten
Gelöschter Account
  • Forum-Beiträge: 281

22.12.2012, 21:22:23 via Website

Wirf doch mal ein paar Blicke auf den AlarmManager... Es kann ja nicht sein, dass da bei Dir die ganze Zeit ein Service läuft, der dann nur alle paar Minutern mal was Sinnvolles tut!

Antworten
Bernd Roth
  • Forum-Beiträge: 98

23.12.2012, 00:33:46 via Website

hallo!

danke dir vielmals!

genau mit dem alarmmanager arbeite ich schon seit stunden und habe es nun anscheinend hinbekommen!
werde das dann mal posten, sobald es meinen überprüfungen standgehalten hat :)

lG

Antworten
Melody Reitler
  • Forum-Beiträge: 3

11.07.2013, 08:57:42 via Website

Bernd Roth
hallo!

danke dir vielmals!

genau mit dem alarmmanager arbeite ich schon seit stunden und habe es nun anscheinend hinbekommen!
werde das dann mal posten, sobald es meinen überprüfungen standgehalten hat :)

lG

Kannst du es bitte posten bzw. mir per Mail schicken? Ich brauch genau das selbe und es ist sehr dringend. Danke im Voraus! LG (-:

Antworten
Bernd Roth
  • Forum-Beiträge: 98

11.07.2013, 21:50:00 via Website

Hallo!

Hier der Code, fett markiert ist die Klasse, welche wichtig ist.

lG

1public class Parken extends Activity {
2
3 protected static final int PICK_CONTACT = 0;
4
5String smsNumber, smsText, smsMinutes, googleMaps;
6EditText edittextSmsNumber, edittextSmsText, edittextSmsMinutes;
7boolean activate = false;
8Button buttonStart, buttonCancel;
9
10 private PendingIntent pendingIntent;
11
12 /** Called when the activity is first created. */
13 @SuppressLint("NewApi")
14 @Override
15 public void onCreate(Bundle savedInstanceState) {
16 super.onCreate(savedInstanceState);
17 setContentView(R.layout.main);
18
19 edittextSmsNumber = (EditText)findViewById(R.id.smsnumber);
20 edittextSmsText = (EditText)findViewById(R.id.smstext);
21 edittextSmsMinutes = (EditText)findViewById(R.id.smsminutes);
22
23 buttonStart = (Button)findViewById(R.id.startalarm);
24 buttonCancel = (Button)findViewById(R.id.cancelalarm);
25 buttonCancel.setEnabled(false);
26 buttonStart.setEnabled(false);
27 ((Button)findViewById(R.id.contactName)).setOnClickListener(new OnClickListener() {
28 @SuppressLint("NewApi")
29 @Override
30 public void onClick(View v) {
31 Intent intent = new Intent(Intent.ACTION_PICK);
32 intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
33 startActivityForResult(intent, 1);
34 }
35 });
36
37 ((EditText)findViewById(R.id.smsnumber)).addTextChangedListener(new TextWatcher() {
38
39 @Override
40 public void onTextChanged(CharSequence s, int start, int before, int count) {
41 // TODO Auto-generated method stub
42 String text = edittextSmsNumber.getText().toString();
43 if ( text.isEmpty() ) {
44 buttonCancel.setEnabled(false);
45 buttonStart.setEnabled(false);
46 } else {
47 buttonCancel.setEnabled(true);
48 buttonStart.setEnabled(true);
49 }
50 }
51
52 @Override
53 public void beforeTextChanged(CharSequence s, int start, int count,
54 int after) {
55 // TODO Auto-generated method stub
56 }
57
58 @Override
59 public void afterTextChanged(Editable s) {
60 // TODO Auto-generated method stub
61 }
62 });
63
64 buttonStart.setOnClickListener(new Button.OnClickListener(){
65 @Override
66 public void onClick(View arg0) {
67 // TODO Auto-generated method stub
68 smsNumber = edittextSmsNumber.getText().toString();
69 smsText = edittextSmsText.getText().toString();
70 smsMinutes = edittextSmsMinutes.getText().toString();
71
72 Intent myIntent = new Intent(Parken.this, MyAlarmService.class);
73
74 Bundle bundle = new Bundle();
75 bundle.putCharSequence("extraSmsNumber", smsNumber);
76 bundle.putCharSequence("extraSmsText", smsText);
77 myIntent.putExtras(bundle);
78
79 pendingIntent = PendingIntent.getService(Parken.this, 0, myIntent, 0);
80
81 AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
82
83 Calendar calendar = Calendar.getInstance();
84 calendar.setTimeInMillis(System.currentTimeMillis());
85 calendar.add(Calendar.SECOND, 10);
86
87 if(smsMinutes.equals(""))
88 alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
89 else {
90 Long smsMinutesInLong = Long.parseLong(smsMinutes);
91 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), smsMinutesInLong*60000, pendingIntent);
92 }
93 Toast.makeText(Parken.this,
94 "Start Alarm with \n" +
95 "smsNumber = " + smsNumber + "\n" +
96 "smsText = " + smsText + "\n" +
97 "smsMinutes = " + smsMinutes,
98 Toast.LENGTH_LONG).show();
99 }});

Antworten
Melody Reitler
  • Forum-Beiträge: 3

12.07.2013, 08:21:52 via Website

Bernd Roth
Hallo!

Hier der Code, fett markiert ist die Klasse, welche wichtig ist.

lG

1public class Parken extends Activity {
2
3 protected static final int PICK_CONTACT = 0;
4
5String smsNumber, smsText, smsMinutes, googleMaps;
6EditText edittextSmsNumber, edittextSmsText, edittextSmsMinutes;
7boolean activate = false;
8Button buttonStart, buttonCancel;
9
10 private PendingIntent pendingIntent;
11
12 /** Called when the activity is first created. */
13 @SuppressLint("NewApi")
14 @Override
15 public void onCreate(Bundle savedInstanceState) {
16 super.onCreate(savedInstanceState);
17 setContentView(R.layout.main);
18
19 edittextSmsNumber = (EditText)findViewById(R.id.smsnumber);
20 edittextSmsText = (EditText)findViewById(R.id.smstext);
21 edittextSmsMinutes = (EditText)findViewById(R.id.smsminutes);
22
23 buttonStart = (Button)findViewById(R.id.startalarm);
24 buttonCancel = (Button)findViewById(R.id.cancelalarm);
25 buttonCancel.setEnabled(false);
26 buttonStart.setEnabled(false);
27 ((Button)findViewById(R.id.contactName)).setOnClickListener(new OnClickListener() {
28 @SuppressLint("NewApi")
29 @Override
30 public void onClick(View v) {
31 Intent intent = new Intent(Intent.ACTION_PICK);
32 intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
33 startActivityForResult(intent, 1);
34 }
35 });
36
37 ((EditText)findViewById(R.id.smsnumber)).addTextChangedListener(new TextWatcher() {
38
39 @Override
40 public void onTextChanged(CharSequence s, int start, int before, int count) {
41 // TODO Auto-generated method stub
42 String text = edittextSmsNumber.getText().toString();
43 if ( text.isEmpty() ) {
44 buttonCancel.setEnabled(false);
45 buttonStart.setEnabled(false);
46 } else {
47 buttonCancel.setEnabled(true);
48 buttonStart.setEnabled(true);
49 }
50 }
51
52 @Override
53 public void beforeTextChanged(CharSequence s, int start, int count,
54 int after) {
55 // TODO Auto-generated method stub
56 }
57
58 @Override
59 public void afterTextChanged(Editable s) {
60 // TODO Auto-generated method stub
61 }
62 });
63
64 buttonStart.setOnClickListener(new Button.OnClickListener(){
65 @Override
66 public void onClick(View arg0) {
67 // TODO Auto-generated method stub
68 smsNumber = edittextSmsNumber.getText().toString();
69 smsText = edittextSmsText.getText().toString();
70 smsMinutes = edittextSmsMinutes.getText().toString();
71
72 Intent myIntent = new Intent(Parken.this, MyAlarmService.class);
73
74 Bundle bundle = new Bundle();
75 bundle.putCharSequence("extraSmsNumber", smsNumber);
76 bundle.putCharSequence("extraSmsText", smsText);
77 myIntent.putExtras(bundle);
78
79 pendingIntent = PendingIntent.getService(Parken.this, 0, myIntent, 0);
80
81 AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
82
83 Calendar calendar = Calendar.getInstance();
84 calendar.setTimeInMillis(System.currentTimeMillis());
85 calendar.add(Calendar.SECOND, 10);
86
87 if(smsMinutes.equals(""))
88 alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
89 else {
90 Long smsMinutesInLong = Long.parseLong(smsMinutes);
91 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), smsMinutesInLong*60000, pendingIntent);
92 }
93 Toast.makeText(Parken.this,
94 "Start Alarm with \n" +
95 "smsNumber = " + smsNumber + "\n" +
96 "smsText = " + smsText + "\n" +
97 "smsMinutes = " + smsMinutes,
98 Toast.LENGTH_LONG).show();
99 }});

Vielen Dank für deine schnelle Antwort! Da ich leider ein ziemlicher Anfänger bin und damit noch immer nicht auf das richtige Ergebnis komme bitte ich dich auf Knien und von tiefstem Herzen: kannst du mir das ganze Projekt per Email schicken?

Antworten
Bernd Roth
  • Forum-Beiträge: 98

12.07.2013, 20:47:25 via Website

Hallo!

Hier ist der komplette Code.
Einfach ein neues Projekt anlegen und das Ganze kompilieren!

lG

Parken - Klasse:

1package com.exercise.Parken;
2
3import java.util.Calendar;
4import com.exercise.Parken.R;
5import android.annotation.SuppressLint;
6import android.app.ActionBar;
7import android.app.Activity;
8import android.app.AlarmManager;
9import android.app.PendingIntent;
10import android.content.Intent;
11import android.database.Cursor;
12import android.net.Uri;
13import android.os.Bundle;
14import android.provider.ContactsContract;
15import android.text.Editable;
16import android.text.TextWatcher;
17import android.view.Menu;
18import android.view.MenuItem;
19import android.view.View;
20import android.view.View.OnClickListener;
21import android.widget.Button;
22import android.widget.EditText;
23import android.widget.Toast;
24
25public class Parken extends Activity {
26
27 protected static final int PICK_CONTACT = 0;
28
29String smsNumber, smsText, smsMinutes, googleMaps;
30EditText edittextSmsNumber, edittextSmsText, edittextSmsMinutes;
31boolean activate = false;
32Button buttonStart, buttonCancel;
33
34 private PendingIntent pendingIntent;
35
36 /** Called when the activity is first created. */
37 @SuppressLint("NewApi")
38 @Override
39 public void onCreate(Bundle savedInstanceState) {
40 super.onCreate(savedInstanceState);
41 setContentView(R.layout.main);
42
43 edittextSmsNumber = (EditText)findViewById(R.id.smsnumber);
44 edittextSmsText = (EditText)findViewById(R.id.smstext);
45 edittextSmsMinutes = (EditText)findViewById(R.id.smsminutes);
46
47 buttonStart = (Button)findViewById(R.id.startalarm);
48 buttonCancel = (Button)findViewById(R.id.cancelalarm);
49 buttonCancel.setEnabled(false);
50 buttonStart.setEnabled(false);
51 ((Button)findViewById(R.id.contactName)).setOnClickListener(new OnClickListener() {
52 @SuppressLint("NewApi")
53 @Override
54 public void onClick(View v) {
55 Intent intent = new Intent(Intent.ACTION_PICK);
56 intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
57 startActivityForResult(intent, 1);
58 }
59 });
60
61 ((EditText)findViewById(R.id.smsnumber)).addTextChangedListener(new TextWatcher() {
62
63 @Override
64 public void onTextChanged(CharSequence s, int start, int before, int count) {
65 // TODO Auto-generated method stub
66 String text = edittextSmsNumber.getText().toString();
67 if ( text.isEmpty() ) {
68 buttonCancel.setEnabled(false);
69 buttonStart.setEnabled(false);
70 } else {
71 buttonCancel.setEnabled(true);
72 buttonStart.setEnabled(true);
73 }
74 }
75
76 @Override
77 public void beforeTextChanged(CharSequence s, int start, int count,
78 int after) {
79 // TODO Auto-generated method stub
80 }
81
82 @Override
83 public void afterTextChanged(Editable s) {
84 // TODO Auto-generated method stub
85 }
86 });
87
88 buttonStart.setOnClickListener(new Button.OnClickListener(){
89 @Override
90 public void onClick(View arg0) {
91 // TODO Auto-generated method stub
92 smsNumber = edittextSmsNumber.getText().toString();
93 smsText = edittextSmsText.getText().toString();
94 smsMinutes = edittextSmsMinutes.getText().toString();
95
96 Intent myIntent = new Intent(Parken.this, MyAlarmService.class);
97
98 Bundle bundle = new Bundle();
99 bundle.putCharSequence("extraSmsNumber", smsNumber);
100 bundle.putCharSequence("extraSmsText", smsText);
101 myIntent.putExtras(bundle);
102
103 pendingIntent = PendingIntent.getService(Parken.this, 0, myIntent, 0);
104
105 AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
106
107 Calendar calendar = Calendar.getInstance();
108 calendar.setTimeInMillis(System.currentTimeMillis());
109 calendar.add(Calendar.SECOND, 10);
110
111 if(smsMinutes.equals(""))
112 alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
113 else {
114 Long smsMinutesInLong = Long.parseLong(smsMinutes);
115 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), smsMinutesInLong*60000, pendingIntent);
116 }
117 Toast.makeText(Parken.this,
118 "Start Alarm with \n" +
119 "smsNumber = " + smsNumber + "\n" +
120 "smsText = " + smsText + "\n" +
121 "smsMinutes = " + smsMinutes,
122 Toast.LENGTH_LONG).show();
123 }});
124
125 buttonCancel.setOnClickListener(new Button.OnClickListener(){
126 @Override
127 public void onClick(View arg0) {
128 // TODO Auto-generated method stub
129 AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
130 alarmManager.cancel(pendingIntent);
131 Toast.makeText(Parken.this, "Cancel!", Toast.LENGTH_LONG).show();
132 }});
133 }
134
135 @Override
136 public boolean onCreateOptionsMenu(Menu menu){
137 super.onCreateOptionsMenu(menu);
138 CreateMenu(menu);
139 return true;
140
141 }
142
143 @Override
144 public boolean onOptionsItemSelected(MenuItem item){
145 return MenuChoice(item);
146 }
147
148 @SuppressLint("ShowToast")
149 private boolean MenuChoice(MenuItem item) {
150 // TODO Auto-generated method stub
151 switch (item.getItemId()) {
152 case 0:
153 for (int i=0; i < 1; i++) {
154 Toast.makeText(this, "Info: \n" + "Developer: Bernd Roth\n" +
155 "1050 Vienna / Austria\n" +
156 "If you have questions regarding this programm,\n" +
157 "please feel free to contact me by Email:\n" +
158 "berndroth0@gmail.com", Toast.LENGTH_LONG).show();
159 }
160 return true;
161 }
162 return false;
163}
164
165 private void CreateMenu(Menu menu) {
166 // TODO Auto-generated method stub
167 MenuItem m1 = menu.add(0, 0, 0, "Info");
168 {
169 m1.setIcon(R.drawable.ic_launcher);
170 m1.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
171 }
172}
173
174 @SuppressLint("InlinedApi")
175 @Override
176 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
177 if (data != null) {
178 Uri uri = data.getData();
179
180 if (uri != null) {
181 Cursor c = null;
182 try {
183 c = getContentResolver().query(uri, new String[]{
184 ContactsContract.CommonDataKinds.Phone.NUMBER,
185 ContactsContract.CommonDataKinds.Phone.TYPE },
186 null, null, null);
187
188 if (c != null && c.moveToFirst()) {
189 String number = c.getString(0);
190 int type = c.getInt(1);
191 edittextSmsNumber.setText(number);
192 showSelectedNumber(type, number);
193 }
194 } finally {
195 if (c != null) {
196 c.close();
197 }
198 }
199 }
200 }
201 }
202
203 private void showSelectedNumber(int type, String number) {
204 Toast.makeText(this, "You have selected this number: " + number, Toast.LENGTH_LONG).show();
205 }
206}
207
208MyAlarmService - Klasse:

1package com.exercise.Parken;
2
3import android.app.Service;
4import android.content.Intent;
5import android.os.Bundle;
6import android.os.IBinder;
7import android.telephony.SmsManager;
8import android.widget.Toast;
9
10public class MyAlarmService extends Service {
11
12 String smsNumberToSend, smsTextToSend;
13
14 @Override
15 public void onCreate() {
16 // TODO Auto-generated method stub
17
18 Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();
19 }
20
21 @Override
22 public IBinder onBind(Intent arg0) {
23 // TODO Auto-generated method stub
24 Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();
25 return null;
26 }
27
28 @Override
29 public void onDestroy() {
30 // TODO Auto-generated method stub
31 super.onDestroy();
32 Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
33 }
34
35 @SuppressWarnings("deprecation")
36@Override
37 public void onStart(Intent intent, int startId) {
38 // TODO Auto-generated method stub
39 super.onStart(intent, startId);
40
41 Bundle bundle = intent.getExtras();
42 smsNumberToSend = (String) bundle.getCharSequence("extraSmsNumber");
43 smsTextToSend = (String) bundle.getCharSequence("extraSmsText");
44
45 Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
46 Toast.makeText(this,
47 "MyAlarmService.onStart() with \n" +
48 "smsNumberToSend = " + smsNumberToSend + "\n" +
49 "smsTextToSend = " + smsTextToSend,
50 Toast.LENGTH_LONG).show();
51
52 SmsManager smsManager = SmsManager.getDefault();
53 smsManager.sendTextMessage(smsNumberToSend, null, smsTextToSend, null, null);
54 }
55
56 @Override
57 public boolean onUnbind(Intent intent) {
58 // TODO Auto-generated method stub
59 Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show();
60 return super.onUnbind(intent);
61 }
62
63}

Manifest - Datei

1<?xml version="1.0" encoding="utf-8"?>
2<manifest xmlns:android=""
3 package="com.exercise.Parken"
4 android:versionCode="1"
5 android:versionName="1.0">
6 <uses-sdk android:minSdkVersion="3" />
7 <uses-permission android:name="android.permission.SEND_SMS" />
8 <uses-permission android:name="android.permission.INTERNET"/>
9 <uses-permission android:name="android.permission.READ_CONTACTS"/>
10 <application android:label="@string/app_name"
11 android:icon="@drawable/parken">
12 <uses-library android:name="com.google.android.maps"/>
13 <activity android:name="com.exercise.Parken.Parken" android:label="@string/app_name" android:theme="@android:style/Theme.WithActionBar">
14 <intent-filter>
15 <action android:name="android.intent.action.MAIN" />
16 <category android:name="android.intent.category.LAUNCHER" />
17 </intent-filter>
18 </activity>
19 <service android:name="com.exercise.Parken.MyAlarmService" />
20 </application>
21
22
23</manifest>

main.xml

1<?xml version="1.0" encoding="utf-8"?>
2<LinearLayout xmlns:android=""
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6
7 <TextView
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:text="Enter Phone Number or get it via Contact Button:" />
11
12 <EditText
13 android:id="@+id/smsnumber"
14 android:layout_width="fill_parent"
15 android:layout_height="wrap_content"
16 android:inputType="phone" />
17
18 <requestFocus />
19
20 <TextView
21 android:layout_width="fill_parent"
22 android:layout_height="wrap_content"
23 android:text="Enter Phone SMS Text:" />
24
25 <EditText
26 android:id="@+id/smstext"
27 android:layout_width="fill_parent"
28 android:layout_height="wrap_content" />
29
30 <TextView
31 android:id="@+id/textView1"
32 android:layout_width="match_parent"
33 android:layout_height="wrap_content"
34 android:text="Enter Minutes" />
35
36 <EditText
37 android:id="@+id/smsminutes"
38 android:layout_width="match_parent"
39 android:layout_height="wrap_content"
40 android:ems="10"
41 android:inputType="phone" >
42 </EditText>
43
44 <Button
45 android:id="@+id/contactName"
46 android:layout_width="match_parent"
47 android:layout_height="wrap_content"
48 android:text="ContactName" />
49
50 <Button
51 android:id="@+id/map"
52 android:layout_width="match_parent"
53 android:layout_height="wrap_content"
54 android:text="GoogleMapsLookUp" />
55
56 <Button
57 android:id="@+id/startalarm"
58 android:layout_width="fill_parent"
59 android:layout_height="wrap_content"
60 android:text="Start" />
61
62 <Button
63 android:id="@+id/cancelalarm"
64 android:layout_width="fill_parent"
65 android:layout_height="wrap_content"
66 android:text="Cancel" />
67
68</LinearLayout>

Antworten
Melody Reitler
  • Forum-Beiträge: 3

15.07.2013, 07:58:51 via Website

Bernd Roth
Hallo!

Hier ist der komplette Code.
Einfach ein neues Projekt anlegen und das Ganze kompilieren!

lG

Parken - Klasse:

1package com.exercise.Parken;
2
3import java.util.Calendar;
4import com.exercise.Parken.R;
5import android.annotation.SuppressLint;
6import android.app.ActionBar;
7import android.app.Activity;
8import android.app.AlarmManager;
9import android.app.PendingIntent;
10import android.content.Intent;
11import android.database.Cursor;
12import android.net.Uri;
13import android.os.Bundle;
14import android.provider.ContactsContract;
15import android.text.Editable;
16import android.text.TextWatcher;
17import android.view.Menu;
18import android.view.MenuItem;
19import android.view.View;
20import android.view.View.OnClickListener;
21import android.widget.Button;
22import android.widget.EditText;
23import android.widget.Toast;
24
25public class Parken extends Activity {
26
27 protected static final int PICK_CONTACT = 0;
28
29String smsNumber, smsText, smsMinutes, googleMaps;
30EditText edittextSmsNumber, edittextSmsText, edittextSmsMinutes;
31boolean activate = false;
32Button buttonStart, buttonCancel;
33
34 private PendingIntent pendingIntent;
35
36 /** Called when the activity is first created. */
37 @SuppressLint("NewApi")
38 @Override
39 public void onCreate(Bundle savedInstanceState) {
40 super.onCreate(savedInstanceState);
41 setContentView(R.layout.main);
42
43 edittextSmsNumber = (EditText)findViewById(R.id.smsnumber);
44 edittextSmsText = (EditText)findViewById(R.id.smstext);
45 edittextSmsMinutes = (EditText)findViewById(R.id.smsminutes);
46
47 buttonStart = (Button)findViewById(R.id.startalarm);
48 buttonCancel = (Button)findViewById(R.id.cancelalarm);
49 buttonCancel.setEnabled(false);
50 buttonStart.setEnabled(false);
51 ((Button)findViewById(R.id.contactName)).setOnClickListener(new OnClickListener() {
52 @SuppressLint("NewApi")
53 @Override
54 public void onClick(View v) {
55 Intent intent = new Intent(Intent.ACTION_PICK);
56 intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
57 startActivityForResult(intent, 1);
58 }
59 });
60
61 ((EditText)findViewById(R.id.smsnumber)).addTextChangedListener(new TextWatcher() {
62
63 @Override
64 public void onTextChanged(CharSequence s, int start, int before, int count) {
65 // TODO Auto-generated method stub
66 String text = edittextSmsNumber.getText().toString();
67 if ( text.isEmpty() ) {
68 buttonCancel.setEnabled(false);
69 buttonStart.setEnabled(false);
70 } else {
71 buttonCancel.setEnabled(true);
72 buttonStart.setEnabled(true);
73 }
74 }
75
76 @Override
77 public void beforeTextChanged(CharSequence s, int start, int count,
78 int after) {
79 // TODO Auto-generated method stub
80 }
81
82 @Override
83 public void afterTextChanged(Editable s) {
84 // TODO Auto-generated method stub
85 }
86 });
87
88 buttonStart.setOnClickListener(new Button.OnClickListener(){
89 @Override
90 public void onClick(View arg0) {
91 // TODO Auto-generated method stub
92 smsNumber = edittextSmsNumber.getText().toString();
93 smsText = edittextSmsText.getText().toString();
94 smsMinutes = edittextSmsMinutes.getText().toString();
95
96 Intent myIntent = new Intent(Parken.this, MyAlarmService.class);
97
98 Bundle bundle = new Bundle();
99 bundle.putCharSequence("extraSmsNumber", smsNumber);
100 bundle.putCharSequence("extraSmsText", smsText);
101 myIntent.putExtras(bundle);
102
103 pendingIntent = PendingIntent.getService(Parken.this, 0, myIntent, 0);
104
105 AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
106
107 Calendar calendar = Calendar.getInstance();
108 calendar.setTimeInMillis(System.currentTimeMillis());
109 calendar.add(Calendar.SECOND, 10);
110
111 if(smsMinutes.equals(""))
112 alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
113 else {
114 Long smsMinutesInLong = Long.parseLong(smsMinutes);
115 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), smsMinutesInLong*60000, pendingIntent);
116 }
117 Toast.makeText(Parken.this,
118 "Start Alarm with \n" +
119 "smsNumber = " + smsNumber + "\n" +
120 "smsText = " + smsText + "\n" +
121 "smsMinutes = " + smsMinutes,
122 Toast.LENGTH_LONG).show();
123 }});
124
125 buttonCancel.setOnClickListener(new Button.OnClickListener(){
126 @Override
127 public void onClick(View arg0) {
128 // TODO Auto-generated method stub
129 AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
130 alarmManager.cancel(pendingIntent);
131 Toast.makeText(Parken.this, "Cancel!", Toast.LENGTH_LONG).show();
132 }});
133 }
134
135 @Override
136 public boolean onCreateOptionsMenu(Menu menu){
137 super.onCreateOptionsMenu(menu);
138 CreateMenu(menu);
139 return true;
140
141 }
142
143 @Override
144 public boolean onOptionsItemSelected(MenuItem item){
145 return MenuChoice(item);
146 }
147
148 @SuppressLint("ShowToast")
149 private boolean MenuChoice(MenuItem item) {
150 // TODO Auto-generated method stub
151 switch (item.getItemId()) {
152 case 0:
153 for (int i=0; i < 1; i++) {
154 Toast.makeText(this, "Info: \n" + "Developer: Bernd Roth\n" +
155 "1050 Vienna / Austria\n" +
156 "If you have questions regarding this programm,\n" +
157 "please feel free to contact me by Email:\n" +
158 "berndroth0@gmail.com", Toast.LENGTH_LONG).show();
159 }
160 return true;
161 }
162 return false;
163}
164
165 private void CreateMenu(Menu menu) {
166 // TODO Auto-generated method stub
167 MenuItem m1 = menu.add(0, 0, 0, "Info");
168 {
169 m1.setIcon(R.drawable.ic_launcher);
170 m1.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
171 }
172}
173
174 @SuppressLint("InlinedApi")
175 @Override
176 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
177 if (data != null) {
178 Uri uri = data.getData();
179
180 if (uri != null) {
181 Cursor c = null;
182 try {
183 c = getContentResolver().query(uri, new String[]{
184 ContactsContract.CommonDataKinds.Phone.NUMBER,
185 ContactsContract.CommonDataKinds.Phone.TYPE },
186 null, null, null);
187
188 if (c != null && c.moveToFirst()) {
189 String number = c.getString(0);
190 int type = c.getInt(1);
191 edittextSmsNumber.setText(number);
192 showSelectedNumber(type, number);
193 }
194 } finally {
195 if (c != null) {
196 c.close();
197 }
198 }
199 }
200 }
201 }
202
203 private void showSelectedNumber(int type, String number) {
204 Toast.makeText(this, "You have selected this number: " + number, Toast.LENGTH_LONG).show();
205 }
206}
207
208MyAlarmService - Klasse:

1package com.exercise.Parken;
2
3import android.app.Service;
4import android.content.Intent;
5import android.os.Bundle;
6import android.os.IBinder;
7import android.telephony.SmsManager;
8import android.widget.Toast;
9
10public class MyAlarmService extends Service {
11
12 String smsNumberToSend, smsTextToSend;
13
14 @Override
15 public void onCreate() {
16 // TODO Auto-generated method stub
17
18 Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();
19 }
20
21 @Override
22 public IBinder onBind(Intent arg0) {
23 // TODO Auto-generated method stub
24 Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();
25 return null;
26 }
27
28 @Override
29 public void onDestroy() {
30 // TODO Auto-generated method stub
31 super.onDestroy();
32 Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
33 }
34
35 @SuppressWarnings("deprecation")
36@Override
37 public void onStart(Intent intent, int startId) {
38 // TODO Auto-generated method stub
39 super.onStart(intent, startId);
40
41 Bundle bundle = intent.getExtras();
42 smsNumberToSend = (String) bundle.getCharSequence("extraSmsNumber");
43 smsTextToSend = (String) bundle.getCharSequence("extraSmsText");
44
45 Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
46 Toast.makeText(this,
47 "MyAlarmService.onStart() with \n" +
48 "smsNumberToSend = " + smsNumberToSend + "\n" +
49 "smsTextToSend = " + smsTextToSend,
50 Toast.LENGTH_LONG).show();
51
52 SmsManager smsManager = SmsManager.getDefault();
53 smsManager.sendTextMessage(smsNumberToSend, null, smsTextToSend, null, null);
54 }
55
56 @Override
57 public boolean onUnbind(Intent intent) {
58 // TODO Auto-generated method stub
59 Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show();
60 return super.onUnbind(intent);
61 }
62
63}

Manifest - Datei

1<?xml version="1.0" encoding="utf-8"?>
2<manifest xmlns:android=""
3 package="com.exercise.Parken"
4 android:versionCode="1"
5 android:versionName="1.0">
6 <uses-sdk android:minSdkVersion="3" />
7 <uses-permission android:name="android.permission.SEND_SMS" />
8 <uses-permission android:name="android.permission.INTERNET"/>
9 <uses-permission android:name="android.permission.READ_CONTACTS"/>
10 <application android:label="@string/app_name"
11 android:icon="@drawable/parken">
12 <uses-library android:name="com.google.android.maps"/>
13 <activity android:name="com.exercise.Parken.Parken" android:label="@string/app_name" android:theme="@android:style/Theme.WithActionBar">
14 <intent-filter>
15 <action android:name="android.intent.action.MAIN" />
16 <category android:name="android.intent.category.LAUNCHER" />
17 </intent-filter>
18 </activity>
19 <service android:name="com.exercise.Parken.MyAlarmService" />
20 </application>
21
22
23</manifest>

main.xml

1<?xml version="1.0" encoding="utf-8"?>
2<LinearLayout xmlns:android=""
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6
7 <TextView
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:text="Enter Phone Number or get it via Contact Button:" />
11
12 <EditText
13 android:id="@+id/smsnumber"
14 android:layout_width="fill_parent"
15 android:layout_height="wrap_content"
16 android:inputType="phone" />
17
18 <requestFocus />
19
20 <TextView
21 android:layout_width="fill_parent"
22 android:layout_height="wrap_content"
23 android:text="Enter Phone SMS Text:" />
24
25 <EditText
26 android:id="@+id/smstext"
27 android:layout_width="fill_parent"
28 android:layout_height="wrap_content" />
29
30 <TextView
31 android:id="@+id/textView1"
32 android:layout_width="match_parent"
33 android:layout_height="wrap_content"
34 android:text="Enter Minutes" />
35
36 <EditText
37 android:id="@+id/smsminutes"
38 android:layout_width="match_parent"
39 android:layout_height="wrap_content"
40 android:ems="10"
41 android:inputType="phone" >
42 </EditText>
43
44 <Button
45 android:id="@+id/contactName"
46 android:layout_width="match_parent"
47 android:layout_height="wrap_content"
48 android:text="ContactName" />
49
50 <Button
51 android:id="@+id/map"
52 android:layout_width="match_parent"
53 android:layout_height="wrap_content"
54 android:text="GoogleMapsLookUp" />
55
56 <Button
57 android:id="@+id/startalarm"
58 android:layout_width="fill_parent"
59 android:layout_height="wrap_content"
60 android:text="Start" />
61
62 <Button
63 android:id="@+id/cancelalarm"
64 android:layout_width="fill_parent"
65 android:layout_height="wrap_content"
66 android:text="Cancel" />
67
68</LinearLayout>

Vieeelen Dank!

Antworten
Gelöschter Account
  • Forum-Beiträge: 442

22.07.2013, 19:44:56 via App

Auch wenn das jetzt nichts zur Sache tut: Warum??? :) Willst du einen Virus machen oder was

Hi, bin 13 Jahre alt :D Erstes Android Spiel "Strategic Labyrinth": http://goo.gl/Q0Wbd

Antworten