1

I have an app that I would like to pull new data whenever the phone is turned on either by pressing the power button or the home button.

Is there a method in the Android API that detects this?

The basic use case would be:

  • User turns on device
  • App widget detects phone is turned on, generates new data to display on widget.
cYn
  • 3,291
  • 6
  • 25
  • 43

4 Answers4

1

If your app isn't writing to the lockscreen, then you can use ACTION_USER_PRESENT. It sends an intent after the user makes the lockscreen go away.

http://developer.android.com/reference/android/content/Intent.html#ACTION_USER_PRESENT

itsmichaelwang
  • 2,282
  • 4
  • 16
  • 25
1

When an android phone is completed turned on (booted), an intent is broadcated:

android.intent.action.BOOT_COMPLETED

You can handle this intent in your app and do the work appropriately.

Sushil
  • 8,250
  • 3
  • 39
  • 71
1

When an android phone is completed turned on (booted), an intent is broadcated:

Try this BootReceiver class

public class BootReceiver extends BroadcastReceiver { 

@Override
public void onReceive(Context aContext, Intent aIntent) { 
  //  if (BOOT_ACTION_NAME.equals(intent.getAction())) {

        // ToDo: Whatever I need at immediately after bootstrap
        Toast.makeText(aContext, "This message phone is turned on!", Toast.LENGTH_LONG).show();
        Log.e("..BOOT_ACTION_NAME...", "This message phone is turned on!");
  //  }
}}

add to below class in manifest.xml in application tag

 <receiver
        android:name="com.example.testing_demo.BootReceiver"
        android:enabled="true" 
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
            <action android:name="android.intent.action.SCREEN_ON" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
Dev 9
  • 263
  • 2
  • 5
0

hey what you can do is go for background services and broadcast receivers, whatever feasible to you for yours app widget need,and there you can handle the power button key presses as follows

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if(keyCode == KeyEvent.KEYCODE_POWER)
    {
//handle the code to achieve yours desired logic
    }
    return super.onKeyDown(keyCode, event);
}

visit also as it will help you activate an application when a power button is clicked

Community
  • 1
  • 1
Jitesh Upadhyay
  • 5,244
  • 2
  • 25
  • 43