0

I am new to android platform and I wish to made an application in which there are no of end devices and a coordinator and they will communicate via sms.

I have built it in j2me but now in android I want to make a service that receive sms in background and update the database uninterruptedly. I have written some code that works fine in simple class but in service how can I bind it>

public class SMSReceiver extends BroadcastReceiver {
    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    private SMSSync sync;
    private Context context;
    public SMSReceiver(Context ctx){
        sync = new SMSSync(ctx);
        this.context = ctx;
    }

    @Override
    public void onReceive(Context context,final Intent intents){
        if (intents.getAction().equals(SMS_RECEIVED)) {
            new Thread(){
                Context context;
                Thread Set(Context ctx){
                    this.context=ctx;
                    return this;
            }
        public void run(){
            try{
            SmsManager sms = SmsManager.getDefault();
            Bundle bundle = intents.getExtras();
            /*String uriContent =intent.getDataString();
            String[] str = uriContent.split(":");
            String strPort = str[str.length-1];
            short Port =  Short.valueOf(strPort, 16);*/
            //if(Port==5000){*/
            if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            SmsMessage[] messages = new SmsMessage[pdus.length];
            for (int i = 0; i < pdus.length; i++)
            messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            String msg=null;
            String address = null;
                for (SmsMessage message : messages) {
                     msg = message.getMessageBody();
                     address = message.getOriginatingAddress();
                }
                ////////////sending to SMSSync class//////////////
                MainThread th = new MainThread(sync,msg);
                try{
                    th.thread.join();
                }catch(Exception e){
                    Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
                if(msg.substring(3, 4).equals("2"))
                    ConstantClass.isAuditrequestSend = true;
//              sync.smsProcess(msg);
                //Toast.makeText(context, "Message Received Successfully", Toast.LENGTH_SHORT).show();              
            }
//          }
            ////////////////after receiving the sms opening the Main Screen./////////////////////
            Intent intent = new Intent(context,ZigbeeActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);


        }catch(Exception e){
            Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
        }
        }
    }.Set(context).start();
    }
    }
//}
}
midhunhk
  • 5,470
  • 7
  • 50
  • 79
Op Kaushik
  • 150
  • 2
  • 17

0 Answers0