У меня есть две кнопки с названиями «Активный» и «Деактивный» и TextView.когда я нажимаю на кнопку «Активно», он должен отправить SMS-сообщение, содержащее сообщение «CMDSAD_ON», на номер телефона и установить «Включить» для моего TextView, а при нажатии на кнопку «Деактивировать» он должен отправить «CMD_OFF» и установить «Отключить»к тому же TextView.
Но когда я запускаю его, он только устанавливает «отключить» текстовое представление и не будет меняться всякий раз, когда я нажимаю кнопки, и даже устанавливает его перед отправкой SMS.Я не знаю, как я могу решить свои проблемы.Спасибо
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//intent to filter for SS message received.
intentFilter = new IntentFilter();
intentFilter.addAction("SMS_RECEIVED_ACTION");
// Button2 is the id of "Active button" and Button4 is the id of "Deactive button".
Button button1 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(this);
Button button2 = (Button) findViewById(R.id.button4);
button2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button2:
String mymsg = "CMDSAD_ON";
String thenumber = "916722";
sendMsg (thenumber, mymsg);
case R.id.button4:
String msg = "CMDSAD_OFF";
String phonenumber = "916722";
sendNackMsg(phonenumber, msg);
default:
}}
и функция sendNackMsg похожа на sendMsg за исключением последней ссылки, которая изменяет текстовое представление на «отключить» (-> Textstatus.setText («отключить»);)
public void sendMsg (String thenumber, String mymsg){
String SENT = "Message sent";
String DELIVERED = "Message delivered";
SmsManager smsManager = SmsManager.getDefault();
Context curContext = this.getApplicationContext();
PendingIntent sentPending = PendingIntent.getBroadcast(curContext,
0, new Intent("SENT"), 0);
curContext.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "Sent.",
Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Not Sent: Generic failure.",
Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "Not Sent: No service (possibly, no SIM-card).",
Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Not Sent: Null PDU.",
Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Not Sent: Radio off (possibly, Airplane mode enabled in Settings).",
Toast.LENGTH_LONG).show();
break;
}
}
}, new IntentFilter("SENT"));
PendingIntent deliveredPending = PendingIntent.getBroadcast(curContext,
0, new Intent("DELIVERED"), 0);
curContext.registerReceiver(
new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "Delivered.",
Toast.LENGTH_LONG).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "Not Delivered: Canceled.",
Toast.LENGTH_LONG).show();
break;
}
}
}, new IntentFilter("DELIVERED"));
PackageManager pm = this.getPackageManager();
if (!pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY) &&
!pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_CDMA)) {
Toast.makeText(this, "Sorry, your device probably can't send SMS...", Toast.LENGTH_SHORT).show();
} else{
smsManager.sendTextMessage("9167227450", null, "CMDSAD_ON_1234", sentPending, deliveredPending);
setContentView(R.layout.activity_main);
TextView Textstatus = (TextView) findViewById(R.id.Textstatus);
Textstatus.setText("enable");}
}
public void sendNackMsg(String phonenumber, String msg) {
String SENT = "Message sent";
String DELIVERED = "Message delivered";
SmsManager smsManager = SmsManager.getDefault();
Context curContext = this.getApplicationContext();
PendingIntent sentPending = PendingIntent.getBroadcast(curContext,
0, new Intent("SENT"), 0);
curContext.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "Sent.",
Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Not Sent: Generic failure.",
Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "Not Sent: No service (possibly, no SIM-card).",
Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Not Sent: Null PDU.",
Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Not Sent: Radio off (possibly, Airplane mode enabled in Settings).",
Toast.LENGTH_LONG).show();
break;
}
}
}, new IntentFilter("SENT"));
PendingIntent deliveredPending = PendingIntent.getBroadcast(curContext,
0, new Intent("DELIVERED"), 0);
curContext.registerReceiver(
new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "Delivered.",
Toast.LENGTH_LONG).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "Not Delivered: Canceled.",
Toast.LENGTH_LONG).show();
break;
}
}
}, new IntentFilter("DELIVERED"));
PackageManager pm = this.getPackageManager();
if (!pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY) &&
!pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_CDMA)) {
Toast.makeText(this, "Sorry, your device probably can't send SMS...", Toast.LENGTH_SHORT).show();
} else{
smsManager.sendTextMessage("09167227450", null, "CMDSAD_OFF_1234", sentPending, deliveredPending);
setContentView(R.layout.activity_main);
TextView Textstatus = (TextView) findViewById(R.id.Textstatus);
Textstatus.setText(disable);}
}