Search code examples
androidandroid-activitysmsbroadcastreceiver

Sms Wakeup App is not working properly


I am trying to make an application, so that my mobile wakes up when it receives an SMS. I tried and made this. Here is the code:

package com.atiffarrukh.wakemeup;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;

import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver
{
    boolean received = false;
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        //---get the SMS message passed in---
        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 += "\n";     
                received = true;
            }
            //---display the new SMS message---
            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
            Intent i = new Intent(context,WakeUp.class); //for starting activity from broadcast
            i.putExtra("ReceivedCheck", received);//put value of received
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//flags the intent to start activity
            context.startActivity(i);
        }                         
    }
}

And the activity is:

package com.atiffarrukh.wakemeup;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;

public class WakeUp extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        boolean check = intent.getBooleanExtra("ReceivedCheck", false);
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        //Toast.makeText(getBaseContext(), "This is WAKEUP Act", Toast.LENGTH_SHORT).show();
        boolean isScreenOn = pm.isScreenOn();
        if(check && !isScreenOn ){
        /*getWindow().setFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        Toast.makeText(getBaseContext(), "This is WAKEUP SCREEN", Toast.LENGTH_SHORT).show();
        */
            //Toast.makeText(getBaseContext(), "This is WAKEUP SCREEN", Toast.LENGTH_SHORT).show();

            final PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My tag");
            wl.acquire();
            Thread timer = new Thread(){
                public void run(){
                    try {
                        sleep(5000);
                    } catch (InterruptedException e) {
                        // TODO: handle exception
                    }finally{
                        wl.release();
                    }
                }
            };
            timer.start();
        }
    }
}

What does this app do?

  1. Wakes up my mobile but few seconds before the message tone.
  2. Open a blank black screen with "WakeMeUp" as title.

Now, what I want is:

  1. Wakes up the mobile but almost at same time with the message tone.
  2. I dont want that blank screen to open when the activity is called.

Solution

    1. The delay might be due to your own code. Try removing time consuming code from your broadcast receiver and see if delay is reduced.

    2. If you don't want blank screen, simply don't use activity. If something is to be run in background, use service.