how to show received sms in textview instead of toast - java

I am currently working on a simple android application to read an SMS and print it in TexView instead of Toast. but in receiver activity, we do not initialise "findbyid" so we can't print the SMS in textView. now I am showing SMS in Toast to test but I do not want it in a toast. I am also read questions/answer and also other articles but can't find what I want.
receiver activity,
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
public class read_sms extends BroadcastReceiver {
// Get the object of SmsManager
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
Log.i("SmsReciver", "senderNum: " + senderNum + ", message: " + message);
//ourSMS.getSmsDetails(senderNum, message);
// Show SMS notification
//Toast.makeText(context, "senderNum: "+ senderNum + ", message: " + message, Toast.LENGTH_LONG).show();
if(message.equals("Milind")){
Toast.makeText(context, "sms matched", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(context, "not matched", Toast.LENGTH_LONG).show();
}
} // end of for loop
} // bundle
} catch (Exception e) {
// TODO: handle exception
Log.e("SmsReciver", "Exception smsReciver" + e);
}
}
}
my home activity is empty because I can't find which code placed here.
public class home extends AppCompatActivity {
TextView SMS_textview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
SMS_textview = (TextView) findViewById(R.id.sms_text);
}
}

change that TextView to
public static TextView SMS_textview;
add method
public void recivedSms(String message)
{
try
{
SMS_textview.setText(message);
}
catch (Exception e)
{
}
}
in read_sms class add the following code when you recieve sms
home Sms = new home();
Sms.recivedSms(message );

Register your Receiver in your activity whenever you Receive a text in receiver
use this code in your Receiver class
Intent broadcastIntent = new Intent();
broadcastIntent.putExtra("your key", your Value);
broadcastIntent.setAction("link from you have recieve a text");
context.sendBroadcast(broadcastIntent);
after this register a broadcast Receiver in your activity like this
private void registerSmsReciever() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("link from you have recieve a text");
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("onReceive", "Sms recieved in progress");
String message= intent.getStringExtra("key");
textView.setText(intent.getStringExtra("key"));
}
};
registerReceiver(broadcastReceiver, intentFilter);
}

Related

How to insert TextView in Activity from other non activity class Android?

I need to set TextView object of Main3Activity (Activity class) from IncomingSms (Non activity class).
public class IncomingSms extends BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
#Override
public void onReceive(Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String senderNum = currentMessage.getDisplayOriginatingAddress();
String message = currentMessage.getDisplayMessageBody();
if (message.equalsIgnoreCase("FIRE DETECTED")) {
//Problem start here: I cant able to set TextView object of Main3Activity
Main3Activity main3Activity = new Main3Activity();
TextView tv = (TextView) main3Activity.findViewById(R.id.firealert);
tv.setText(message);
//Problem end here
Log.i("SmsReceived", "senderNum: " + senderNum + "; message: " + message);
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, message, duration);
toast.show();
}
}
}
} catch(Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);
}
}
}
create an interface class save it as java class then
public interface SmsListener {
public void messageReceived(String messageText);
}
and
final SmsManager sms = SmsManager.getDefault();
private static SmsListener mListener;
#Override
public void onReceive(Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String senderNum = currentMessage.getDisplayOriginatingAddress();
String message = currentMessage.getDisplayMessageBody();
if (message.equalsIgnoreCase("FIRE DETECTED") ){
mListener.messageReceived(message); //add this
Log.i("SmsReceived", "senderNum: "+ senderNum + "; message: " + message);
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, message, duration);
toast.show();
}
}
}
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
public static void bindListener(SmsListener listener) {
mListener = listener;
}
And in your Main3Activity onCreate method
TextView tv=(TextView) main3Activity.findViewById(R.id.firealert);
SmsReceiver.bindListener(new SmsListener() {
#Override
public void messageReceived(String messageText) {
Log.d("Text",messageText);
tv.setText(messageText);
}
});
You can't create the activity instance like that.
Solution:
1. Create a broadcast receiver in your activity and register for a custom intent.
2. Send a custom broadcast intent from your sms receiver. So basically that will reach activity receiver and then you can simply update the text there.
In case your activity is not started, simply create a activity intent and pass the sms text as part of the intent extras.

How to pass data from Receiver to Activity using interface?

I'm trying to pass the data from the brodcast receiver to the activity to display it on the view using an interface, but, it's not working.Here are my files,MainActivity.java
public class MainActivity extends AppCompatActivity implements OtpPassing {
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onReceive(String msg) {
Toast.makeText(this, "o" + msg, Toast.LENGTH_SHORT).show();
textView = (TextView) findViewById(R.id.textView);
textView.setText("OTP: " + msg);
}
}
OtpPassing.java
public interface OtpPassing {
public void onReceive(String msg);
}
Receiver.java
public class Receiver extends BroadcastReceiver {
// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
Log.i("SmsReceiver", "senderNum: " + senderNum + "; message: " + message);
String otp = getOtp(message);
Toast.makeText(context, otp, Toast.LENGTH_LONG).show(); //This is working fine
OtpPassing otpPassing = new MainActivity();
otpPassing.onReceive(otp); //This is not working
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
private String getOtp(String message) {
String otp = null;
int index = message.indexOf("otp:");
if(index != -1){
int start = index + 5;
otp = message.substring(start, start+4);
}
return otp;
}
}
The receiver is woking(it's displaying the message on a Toast) but why is displaying the message on the screen via MainActivity not working?
You can create a broadcast receiver as a data member of your activity. You register for it in onCreate and unregister in onDestroy (or onStart and onStop).
That way the receiver has a reference to the parent activity and you can do whatever.
public class MainActivity extends AppCompatActivity implements OtpPassing {
private TextView textView;
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
//Access the parent activity as follows
MainActivity.this.textView.setText("...");
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//instantiate and register the receiver
setContentView(R.layout.activity_main);
}
#Override
protected void onDestroy() {
// Unregister the receiver here
super.onDestroy();
}
Here better way (I suppose) would be to use Android Intents
in your receiver, something like this
Intent intent = new Intent(context,MainActivity.class);
intent.putExtra(.....);
startactivity(intent);
now in your MainActivity, override onNewIntent(). Here check for the 'extras' you had passed from the receiver.
Please note, this might result in having multiple MainActivities running in your phone if your "older" MainActivity was not killed by the time this intent was passed via receiver. To overcome this, use 'singleTop' or similar flags in your Manifest file

How to start service based on result from activity recognition

I am Trying to start a service if the DetectedActivity returned from Activity recognition returned is IN_VEHICLE. I have downloaded a code sample
from :
http://tutsberry.com/activity-recognition-implementation-on-android/
But i'm not sure where to put code in to start my service.
I am Trying to the check the user activity in the background using the Activity Recognition and then
start a service all in the background.
ActivityRecognitionIntentService Class
public class ActivityRecognitionIntentService extends IntentService {
//LogCat
private static final String TAG = ActivityRecognitionIntentService.class.getSimpleName();
public ActivityRecognitionIntentService() {
super("ActivityRecognitionIntentService");
}
protected void onHandleIntent(Intent intent) {
if (ActivityRecognitionResult.hasResult(intent)) {
//Extract the result from the Response
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
DetectedActivity detectedActivity = result.getMostProbableActivity();
//Get the Confidence and Name of Activity
int confidence = detectedActivity.getConfidence();
String mostProbableName = getActivityName(detectedActivity.getType());
//Fire the intent with activity name & confidence
Intent i = new Intent("ImActive");
i.putExtra("activity", mostProbableName);
i.putExtra("confidence", confidence);
Log.d(TAG, "Most Probable Name : " + mostProbableName);
Log.d(TAG, "Confidence : " + confidence);
//Send Broadcast to be listen in MainActivity
this.sendBroadcast(i);
} else {
Log.d(TAG, "Intent had no data returned");
}
}
//Get the activity name
private String getActivityName(int type) {
switch (type) {
case DetectedActivity.IN_VEHICLE:
return "In Vehicle";
case DetectedActivity.ON_BICYCLE:
return "On Bicycle";
case DetectedActivity.ON_FOOT:
return "On Foot";
case DetectedActivity.WALKING:
return "Walking";
case DetectedActivity.STILL:
return "Still";
case DetectedActivity.TILTING:
return "Tilting";
case DetectedActivity.RUNNING:
return "Running";
case DetectedActivity.UNKNOWN:
return "Unknown";
}
return "N/A";
}
}
MainActivity Class
public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener{
// LogCat
private static final String TAG = MainActivity.class.getSimpleName();
private Context mContext;
private GoogleApiClient mGApiClient;
private BroadcastReceiver receiver;
private TextView textView;
private TextView tv2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.msg);
textView.setMovementMethod(new ScrollingMovementMethod());
tv2 = (TextView) findViewById(R.id.text2);
//Set the context
mContext = this;
//Check Google Play Service Available
if (isPlayServiceAvailable()) {
mGApiClient = new GoogleApiClient.Builder(this)
.addApi(ActivityRecognition.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
//Connect to Google API
mGApiClient.connect();
} else {
Toast.makeText(mContext, "Google Play Service not Available", Toast.LENGTH_LONG).show();
}
//Broadcast receiver
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//Add current time
Calendar rightNow = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("h:mm:ss a");
String strDate = sdf.format(rightNow.getTime());
;
String v = strDate + " " +
intent.getStringExtra("activity") + " " +
"Confidence : " + intent.getExtras().getInt("confidence") + "\n";
v = textView.getText() + v;
textView.setText(v);
}
};
//Filter the Intent and register broadcast receiver
IntentFilter filter = new IntentFilter();
filter.addAction("ImActive");
registerReceiver(receiver, filter);
//Check for Google play services available on device
}
private boolean isPlayServiceAvailable() {
return GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext) == ConnectionResult.SUCCESS;
}
#Override
protected void onDestroy() {
super.onDestroy();
}
#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 void onConnected(Bundle bundle) {
Intent i = new Intent(this, ActivityRecognitionIntentService.class);
PendingIntent mActivityRecongPendingIntent = PendingIntent
.getService(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
Log.d(TAG, "connected to ActivityRecognition");
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mGApiClient, 0, mActivityRecongPendingIntent);
//Update the TextView
textView.setText("Connected to Google Play Services \nWaiting for Active Recognition... \n");
}
#Override
public void onConnectionSuspended(int i) {
Log.d(TAG, "Suspended to ActivityRecognition");
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(TAG, "Not connected to ActivityRecognition");
//Disconnect and detach the receiver
mGApiClient.disconnect();
unregisterReceiver(receiver);
}
}
MainActivity.java
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
public class MainActivity extends Activity {
IntentFilter filter;
BroadcastReceiver mReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
filter = new IntentFilter();
mReceiver = new MyReceiver();
filter.addAction("IN_VEHICLE"); //Name the action what ever you need to.
filter.addAction("OTHER_ACTION");
filter.addAction("YET_ANOTHER_ACTION)");
registerReceiver(mReceiver, filter); //Register the receiver you define with the actions you want.
}
class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if("IN_VEHICLE_RESPONSE".equals(intent.getAction())){
//Launch other activities here
Intent i = new Intent(getApplicationContext(), InVehicleActivity.class );
startActivity(i);
}else if("OTHER_ACTION_RESPONSE".equals(intent.getAction())){
//Launch desired activity
}else if("YET_ANOTHER_ACTION_RESPONSE".equals(intent.getAction())){
//Launch desired activity
}else{
//Handle unssuported actions
}
}
}
}
MyIntentService.java
import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
public class MyIntentService extends IntentService {
Bundle extras;
public MyIntentService() {
super("MyIntentService");
}
#Override
protected void onHandleIntent(Intent intent) {
extras = intent.getExtras();
if("IN_VEHICLE".equals(intent.getAction())){
inVehicle(extras);
return;
}
if("OTHER_ACTION".equals(intent.getAction())){
//otheraction method
return;
}
if("YET_ANOTHER_ACTION".equals(intent.getAction())){
//yetanotheraction method
return;
}
}
private void inVehicle(Bundle extras) {
//do work
//do work
Intent intent = new Intent();
intent.setAction("IN_VEHICLE_RESPONSE");
//put all other desired stuff in intent
getApplicationContext().sendBroadcast(intent);
}
}
Don't forget to add your receiver to your manifest.
I would suggest that you start Activities from Activity not Service layer.

How to display NDEF message after NDEF discovered/activity launched?

Have read through a number of questions similar to mine - I can only apologise if I have missed the solution to my woes, but I really can't work this out!
I have managed to get my nfc activity working - tapping the tag launches the correct activity of my app - but the ndef message is not displayed and I can't figure out why...
The tag is a Mifare Ultralight with a Plain Text message encoded. (mimetype: plain/text)
Here's my code, thank you all for your help, this forum should have a 'donate a beer' button!
package com.example.prototype02;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NdefMessage;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.Toast;
public class nfcActivity extends Activity {
private static final String TAG = "NFCReadTag";
private NfcAdapter mNfcAdapter;
private IntentFilter[] mNdefExchangeFilters;
private PendingIntent mNfcPendingIntent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nfclayout);
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
mNfcPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP), 0);
IntentFilter nfcIntent = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
nfcIntent.addDataScheme("text/plain");
mNdefExchangeFilters = new IntentFilter[] { nfcIntent };
}
#Override
protected void onPause() {
super.onPause();
if(mNfcAdapter != null) mNfcAdapter.disableForegroundDispatch(this);
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (mNfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
NdefMessage[] messages = null;
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(mNfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMsgs != null) {
messages = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
messages[i] = (NdefMessage) rawMsgs[i];
}}
else if (rawMsgs == null){
Toast.makeText(getApplicationContext(), "No NDEF Message Read", Toast.LENGTH_LONG).show();
}
if(messages[0] != null) {
String result="";
byte[] payload = messages[0].getRecords()[0].getPayload();
for (int b = 1; b<payload.length; b++) { // skip SOH
result += (char) payload[b];
}
Toast.makeText(getApplicationContext(), "Safe Location Registered - " + result, Toast.LENGTH_SHORT).show();
}
}
else Toast.makeText(getApplicationContext(), "Intent Error...", Toast.LENGTH_LONG).show();
}
}
If your activity is started by the NFC intent, then onNewIntent() will not be called (it will only be called when you scan the tag when the activity is already in the foreground). Try doing something like calling onNewIntent(getIntent()) in onCreate().
Try something like this: (taken from StickyNotes NFC sample)
#Override
protected void onResume() {
super.onResume();
mResumed = true;
// Sticky notes received from Android
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
NdefMessage[] messages = getNdefMessages(getIntent());
byte[] payload = messages[0].getRecords()[0].getPayload();
setNoteBody(new String(payload));
setIntent(new Intent()); // Consume this intent.
}
enableNdefExchangeMode();
}
#Override
protected void onPause() {
super.onPause();
mResumed = false;
mNfcAdapter.disableForegroundNdefPush(this);
}
#Override
protected void onNewIntent(Intent intent) {
// NDEF exchange mode
if (!mWriteMode && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
NdefMessage[] msgs = getNdefMessages(intent);
promptForContent(msgs[0]);
}
// Tag writing mode
if (mWriteMode && NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
writeTag(getNoteAsNdef(), detectedTag);
}
}

Image should get changed when message is received in android

package com.example.nhx;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver {
ImageView imageView1;
ImageView imageView2;
Button ON, OFF;
String msg = "";
#Override
public void onReceive(Context context, Intent intent) {
// ---get the SMS message passed in---
if (msg.contains("LED ON")) {
Toast.makeText(context, "HELLO WORLD", Toast.LENGTH_SHORT).show();
}
if (msg.contains("LED OFF")) {
Toast.makeText(context, "HELLO", Toast.LENGTH_SHORT).show();
}
if (msg == "LED ON") {
imageView1.setVisibility(imageView1.VISIBLE);
imageView2.setVisibility(imageView2.INVISIBLE);
}
if (msg == "LED OFF") {
imageView2.setVisibility(imageView2.VISIBLE);
imageView1.setVisibility(imageView1.INVISIBLE);
}
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null) {
// ---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
// str += "SMS from " + msgs[i].getOriginatingAddress();
// str += " :";
str += msgs[i].getMessageBody().toString();
str += msg;
// str += "\n";
}
}
if (msg.contains("LED ON")) {
Toast.makeText(context, "HELLO WORLD", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(context, "HELLO", Toast.LENGTH_SHORT).show();
}
if (msg == "LED ON") {
imageView1.setVisibility(imageView1.VISIBLE);
imageView2.setVisibility(imageView2.INVISIBLE);
}
if (msg == "LED OFF") {
imageView2.setVisibility(imageView2.VISIBLE);
imageView1.setVisibility(imageView1.INVISIBLE);
}
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
private void startactivity(Intent i) {
}
}
package com.example.nhx;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class blinkMe extends Activity {
Button ON;
Button OFF;
ImageView imageView1;
ImageView imageView2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ON = (Button) findViewById(R.id.ON);
OFF = (Button) findViewById(R.id.OFF);
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView2 = (ImageView) findViewById(R.id.imageView2);
ON.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
sendSMS("5556", "LED ON");
OFF.setClickable(true);
imageView1.setVisibility(imageView1.VISIBLE);
imageView2.setVisibility(imageView2.INVISIBLE);
}
private void sendSMS(String phoneNumber, String message) {
// TODO Auto-generated method stub
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
ON.setClickable(true);
}
});
OFF.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
ON.setClickable(true);
sendSMS("5554", "LED OFF");
/* Intent i = new Intent(); */
imageView2.setVisibility(imageView2.VISIBLE);
imageView1.setVisibility(imageView1.INVISIBLE);
}
private void sendSMS(String phoneNumber, String message) {
// TODO Auto-generated method stub
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
});
}
}
In my application I have two buttons like on and off and two images overlapping each other, red and green colour. When I click on button green image should come foreword and when I click off, red image should come foreword, along with it one message should be sent to predefined number when the button is clicked means two events takes place when the button is clicked
1. corresponding image switches foreword
2. message is sent to predefined number
Now the problem arises after sending message the intended receiver sends us message now based on the message received the image should automatically get changed like if the receiver reply's on then green image should come foreword and if he reply's off then red image should come foreword.

Categories