3

Is it technically feasible to do the following in Android on pressing the phone start button. Lets say the user's phone is off. When the user presses the on button, then the following happens:

  1. Send SMS
  2. Send Email
  3. Make Call

Currently, I tried to find the KeyCode of the phone's START BUTTON but unable to get any response in the log cat.

import android.view.KeyEvent;

        @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {


            Log.e("KeyCode", "KeyCode:" + keyCode);
            Log.e("KeyEvent", "KeyEvent" + event);

            return true;

    }

Looking forward to a credible answer with precise technical details. Thanks

Muhammad Maqsoodur Rehman
  • 33,681
  • 34
  • 84
  • 124

7 Answers7

6

Register the Broadcast Receiver in the AndroidManifest.xml. This receiver broadcast when screen went ON. This execute either your application activity is on screen or not.

AndroidManifest.xml

<receiver android:name=".MyBroadCastReciever">
    <intent-filter>
        <action android:name="android.intent.action.SCREEN_ON"/>
    </intent-filter>
</receiver>

MyBroadCastReciever.java

public class MyBroadCastReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            Log.i("Check","Screen went ON");
            Toast.makeText(context, "screen ON",Toast.LENGTH_LONG).show();

            // Here you can write the logic of send SMS, Email, Make a call

        }
    }
}

Update

Logic to make the call

You can do this simply. It make the call directly.

Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "Your Phone_number")); 
startActivity(callIntent);

and add this permission in AndroidManifest.xml

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

Logic to send the SMS

You can do this simply. This send the SMS

public void sendSMS() {
    String phoneNumber = "0123456789";
    String message = "Hello World!";

    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(phoneNumber, null, message, null, null);
}

and add this permission in AndroidManifest.xml

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

Logic to send the Email

If you want to send the Email here without any user interaction on id then either design the PHP web service and call the web service in the android application.

If you want to send the mail from the configured email then you can use the Intent.

Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{"youremail@yahoo.com"});          
email.putExtra(Intent.EXTRA_SUBJECT, "subject");
email.putExtra(Intent.EXTRA_TEXT, "message");
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Choose an Email client :"));

Note : If you want to perform this action in Activity then register the broadcast receiver in the Activity and unregister when activity destroy.

Ajay S
  • 48,003
  • 27
  • 91
  • 111
3
 @Override
public boolean onKeyDown(int keyCode, KeyEvent event) 
     {
    if (keyCode == KeyEvent.KEYCODE_BACK)
          {


     // do watever you want here

    }

    return super.onKeyDown(keyCode, event);
}
Shani Goriwal
  • 2,111
  • 1
  • 18
  • 30
Poison
  • 33
  • 7
  • You are right. There are 2 scenarios here: 1. I just press the start button (the phone gets locked with offscreen). 2. I keep my finger while pressing the on/off button (the phone asks the user to turn it off or restart it). I want to catch the keycode in both scenarios. – Muhammad Maqsoodur Rehman Jan 10 '14 at 11:04
  • so what is the problem with this? you are not able to give conditions..let me know... – Poison Jan 10 '14 at 11:12
  • Yes exactly...that's the problem. I am not able to give conditions in order to make the app sense whether 1. I have just pressed the on/off button or 2. I have pressed it longer to turn it off or restart it. – Muhammad Maqsoodur Rehman Jan 10 '14 at 11:20
1

You are saying

Lets say the user's phone is off.

So I asume the phone is completly off and you want to do those things on boot. I would create a receiver StartMyServiceAtBootReceiver.

public class StartMyServiceAtBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
            //Do whatever you want to do on boot.
        }
    }
}

And place this in your minifest

    receiver
        android:name="yourpackage.StartMyServiceAtBootReceiver"
        android:enabled="true"
        android:exported="true"
        android:label="StartMyServiceAtBootReceiver" >
        <intent-filter android:priority="999" >
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
Robin Dijkhof
  • 18,665
  • 11
  • 65
  • 116
1

You can never get StartButton events in your application's

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
}

Because android system don't dispatch this event for us. But if your intension is to do some task on completion of Phone's booting, you have to listen to <action android:name="android.intent.action.BOOT_COMPLETED" /> Broadcast & registation have to be done through AndroidManifest.xml.

CR Sardar
  • 921
  • 2
  • 17
  • 32
0

These two posts have all the information you need:

Listen to volume buttons in background service?

Detect on/off Key Press Android

Basically your application needs to be running to detect key events.

KeyEvent.KEYCODE_POWER is the key for on/off button

Community
  • 1
  • 1
Niko
  • 8,093
  • 5
  • 49
  • 85
0

i think you can do in this way

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

        super.onKeyDown(keyCode, event);

        if (keyCode == KeyEvent.KEYCODE_POWER) {
              // Your code here
              return true;
        }

        return false;
    }

visit also http://developer.android.com/reference/android/view/KeyEvent.html

Jitesh Upadhyay
  • 5,244
  • 2
  • 25
  • 43
0

Please look at this answer by Dave Webb:Broadcast reciever on shutdown

He writes:

You can use the ACTION_SHUTDOWN Intent which is broadcast when the phone is about to shutdown. The documentation says:

Apps will not normally need to handle this, since the foreground activity will be paused as well.

In other words, if you respond to all the lifecycle events for your Activity appropriately, there's no need to use this unless you really want to do something specific related to shutdown.

The ACTION_SHUTDOWN Intent was introduced in API Level 4, in other words it'll only be sent on phones running Android 1.6 or later.

You'll trap the Broadcast with a BroadcastReceiver. It will look something like this:

public class ShutdownReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        //Insert code here
    }

}

You'll also need an entry in your Manifest like the following:

<receiver android:name=".ShutdownReceiver">   
    <intent-filter>
        <action android:name="android.intent.action.ACTION_SHUTDOWN" />
    </intent-filter> 
</receiver>

Depending on what you're doing, another option would be to use the ACTION_BOOT_COMPLETED Intent which is sent when the phone is restarted.

I hope it will help!

Community
  • 1
  • 1
molokoka
  • 1,290
  • 12
  • 16