0

I am trying to turn screen on and off via sms. here is my code below i don't know what has gone wrong as it is not working at all.Do help me in finding error. I am attaching manifest file too.Thank you in advance. My java file:

public class MyReceiver extends BroadcastReceiver{

String sender;
@Override
public void onReceive(Context context, Intent intent) {

    SmsMessage[] sms = null;

    Bundle b = intent.getExtras();

    String str = " SMS From : ";
    if (b != null) {

        Object[] pdus = (Object[]) b.get("pdus");

        sms = new SmsMessage[pdus.length];

        for (int i = 0; i < sms.length; i++) {

            sms[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);

            if (i == 0) {
                str +=  sms[i].getOriginatingAddress();
                str += ":"+sms[i].getMessageBody().toString();          

                }else if (sms[i].getMessageBody().equals("D")) {
                    Intent in2= new Intent(Intent.ACTION_SCREEN_OFF);
                    in2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(in2);

                }else if (sms[i].getMessageBody().equals("E")) {
                    Intent in3= new Intent(Intent.ACTION_SCREEN_ON);
                    in3.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(in3);

            }
        }
    }
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
        Log.d("Receiving", str);
    }

}
 }

MY manifest file:

<uses-permission android:name="android.permission.RECEIVE_SMS"/>

    <receiver android:name=".MyReceiver">
        <intent-filter android:priority="100">
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            <action android:name="android.intent.action.SCREEN_OFF"/>
            <action android:name="android.intent.action.SCREEN_ON"/>
            </intent-filter>

            </receiver>
</application>

Disguise
  • 99
  • 1
  • 1
  • 12

1 Answers1

0

Ok, first, regarding the receiving of SMS you might want to see my answer to this: communication between two device using sms

It is the most reliable way of receiving SMS that I have found.

Notice that SMS might not be the best option on newer devices from KitKat upwards if you do not want the message to appear on the device Hide sms notifications with a broadcastreceiver in KitKat

You might want to consider switching to push notifications from the internet instead. This is easily done using for instance parse.com

Regarding turning on and off screen I use the following piece of code.

private PowerManager.WakeLock wl;
private PowerManager pm;

public void screenWakeup(Context context, Activity activity) {

    try {
        pm = (PowerManager) context
                .getSystemService(Activity.POWER_SERVICE);
        // if (!pm.isScreenOn()) {
        if (wl == null) {
            wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP
                    | PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "Turios");
        }
        if (!wl.isHeld()) {
            wl.acquire();
        }

        final Window win = activity.getWindow();
        win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        win.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        // }

    } catch (Exception ex) {
        Log.e(TAG, ex.getMessage());
    }

}

public void screenRelease(Activity activity) {

    if (releaseWakeLock()) {
        activity.getWindow().clearFlags(
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                        | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                        | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                        | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

}  

Notice that it needs access to an activity, so it will not work unless your activity is alive. What I would suggest to do is:

  1. Receive the turn on/off message
  2. Send a new broadcast e.g. sendBroadcast(new Intent("INTENT_TURN_SCREEN_ON_OFF"))
  3. Register a listener for the broadcast inside your activity

    // in onCreate
    wakeScreenReceiver = new WakeScreenReceiver();
    registerReceiver(wakeScreenReceiver, new IntentFilter("INTENT_TURN_SCREEN_ON_OFF"));
    
    // in onDestroy
    unregisterReceiver(wakeScreenReceiver);
    
    // WakeScreenReceiver defined somewhere inside the Acitivity
    public class WakeScreenReceiver extends BroadcastReceiver {
        private static final String TAG = "WakeScreenReceiver";
    
        public RefreshModules() {
        }
    
        @Override
        public void onReceive(Context context, final Intent intent) {
              screenWakeup(MainActivity.this, MainActivity.this);
              // or 
              screenRelease(MainActivity.this);
        }
    }
    

This way the app will only ever attempt to turn on/off the screen if the Activity is alive since otherwise the broadcast will simply not be received.

Community
  • 1
  • 1
cYrixmorten
  • 7,110
  • 3
  • 25
  • 33
  • It is good. But it is very complicated for me to understand as i m basic learner. Thank you , i will try to understand this though. @cYrixmorten – Disguise May 17 '14 at 14:40