1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
| package com.example.dda.sms;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends ActionBarActivity {
Button sendSMS;
EditText msgText;
EditText numText;
IntentFilter intentFilter;
Button historyBtn;
Button helpBtn;
private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
TextView intTxt = (TextView) findViewById(R.id.textMsg);
intTxt.setText(intent.getExtras().getString("sms"));
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentFilter = new IntentFilter();
intentFilter.addAction("SMS_RECEIVED_ACTION");
// id команды для кнопок и текста
historyBtn = (Button) findViewById(R.id.historyBtn);
historyBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentopenhistry = new Intent(Main.this, Log.class);
startActivity(intentopenhistry);
}
});
sendSMS = (Button) findViewById(R.id.sendBtn);
msgText = (EditText) findViewById(R.id.message);
numText = (EditText) findViewById(R.id.numberTxt);
sendSMS.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v){
String myMsg = msgText.getText().toString(); //string values
String theNumber = numText.getText().toString(); //string values
sendMsg(theNumber,myMsg);
}
});
}
protected void sendMsg(String theNumber, String myMsg){
String SENT = "Message sent";
String DELIVERED = "Message Delivered";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT),0); //обработка доставки //подтвержедение отправки
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED),0);//подтверждение доставки
t
registerReceiver(new BroadcastReceiver() {
public void onReceive(Context arg0, Intent arg1) {
//context - ситуация и intent - содержание
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(Main.this, "SMS sent", Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic Failure", Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No Service", Toast.LENGTH_LONG).show();
break;
}
}
}, new IntentFilter(SENT));
registerReceiver(new BroadcastReceiver()
{
public void onReceive(Context arg0, Intent arg1)
{
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered", Toast.LENGTH_LONG).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_LONG).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(theNumber, null, myMsg, sentPI, deliveredPI);
}
protected void onResume (){
registerReceiver(intentReceiver, intentFilter);
super.onResume();
}
protected void onPause(){
unregisterReceiver(intentReceiver);
super.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
} |