0

I am working on a Application that will restart my Rooted Android phone, when it receives a particular SMS from Particular Number and its working fine for me but issue is that after phone restarts it Auto generate same message again that make phone restart again and again

if (intent.getAction()
                .equals("android.provider.Telephony.SMS_RECEIVED")) {
            Bundle bundle = intent.getExtras(); // ---get the SMS message passed
                                                // in---
            SmsMessage[] msgs = null;
            String msg_from = "";
            String msg_body = "";

            if (bundle != null) {
                try {
                    Object[] pdus = (Object[]) bundle.get("pdus");
                    SmsMessage sms = SmsMessage.createFromPdu((byte[]) pdus[0]);

                    Log.i("cs.fsu",
                            "smsActivity : SMS is <" + sms.getMessageBody()
                                    + ">");

                    // strip flag
                    msg_body = sms.getMessageBody();
                    msg_from = sms.getOriginatingAddress();
                    while (msg_body.contains("FLAG"))
                        msg_body = msg_body.replace("FLAG", "");
                } catch (Exception e) {
                    Log.d("Exception caught", e.getMessage());
                }

            } else {
                Log.i("cs.fsu", "smsActivity : NULL SMS bundle");
            }

            if (msg_from.equals("+91XXXXXXX")
                    && msg_body.equals("Restart Phone")) {
                Log.e("Message Data in condition", "Message Body:-" + msg_body
                        + "Message No." + msg_from);
                Toast.makeText(context, msg_body, 550).show();
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                rebootSU();
            }

And to reboot my rooted phone i used

public static void rebootSU() {
        Runtime runtime = Runtime.getRuntime();
        Process proc = null;
        OutputStreamWriter osw = null;
        StringBuilder sbstdOut = new StringBuilder();
        StringBuilder sbstdErr = new StringBuilder();

        String command = "/system/bin/reboot";

        try { // Run Script

            proc = runtime.exec("su");
            osw = new OutputStreamWriter(proc.getOutputStream());
            osw.write(command);
            osw.flush();
            osw.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (osw != null) {
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        try {
            if (proc != null)
                proc.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (proc.exitValue() != 0) {
        }
    }
}

Any help will b Appreciated ,Thanks.

Rohit
  • 810
  • 2
  • 9
  • 34
  • Isn't the problem that you reboot the phone before the phone has marked the SMS as read? So, delay the reboot. You might need to do that from another thread (maybe the call to your function must return for the message to be marked as read?) – some Sep 06 '14 at 08:12
  • But it is regenerating message (New Message) even showing message received on SIM-2 and SIM 2 port does not have any SIM in it – Rohit Sep 06 '14 at 08:16
  • as some suggested mark the sms as read `String SmsMessageId = cursor.getString(cursor.getColumnIndex("_id"));` `ContentValues values = new ContentValues();` `values.put("read", true);` `con.getContentResolver().update(Uri.parse("content://sms/inbox"), values, "_id=" + SmsMessageId, null);` or delete the sms refer this [post](http://stackoverflow.com/q/419184/3326331) – Sagar Pilkhwal Sep 06 '14 at 08:39

0 Answers0