Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

broadcastreceiver - Android power button press not receiving by receiver

I am trying to receive power button key press, But i unable to receive it.

Receiver.

public class PowerSendMessage extends BroadcastReceiver
{
public PowerSendMessage(Activity activity) 
    {
        this.activity=activity;
    }
static int countPowerOff = 0;
private Activity activity = null;
@Override
public void onReceive(Context context, Intent intent) 
{
    Log.d("Power Button", "Click");

    if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
    {
        countPowerOff++;
    }
    else if(intent.getAction().equals(Intent.ACTION_SCREEN_ON))
    {
        if(countPowerOff == 2)
        {
            Log.d("Power Click", "2 Times");
        }
    }
}

Manifest Entry

<receiver android:name=".PowerSendMessage">

        <intent-filter>
            <action android:name="android.intent.action.SCREEN_OFF"></action>
            <action android:name="android.intent.action.SCREEN_ON"></action>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED"></action>
            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"></action>
            <action android:name="android.intent.action.ACTION_SHUTDOWN"></action>
        </intent-filter>
    </receiver>

Where to put below code (means in which file)

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    PowerSendMessage mReceiver = new PowerSendMessage(this);
    registerReceiver(mReceiver, filter);

Any permission require for this procedure ?

I think I missing something but what that I dont Know please help me.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try this

MyReceiver.java

public class MyReceiver extends BroadcastReceiver 
{
    private static int countPowerOff = 0;

    public MyReceiver ()
    {

    }

    @Override
    public void onReceive(Context context, Intent intent)
    {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
        {    
            Log.e("In on receive", "In Method:  ACTION_SCREEN_OFF");
            countPowerOff++;
        }
        else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
        {
            Log.e("In on receive", "In Method:  ACTION_SCREEN_ON");
        }
        else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT))
        {
            Log.e("In on receive", "In Method:  ACTION_USER_PRESENT");
            if (countPowerOff > 2)
            {
                countPowerOff=0;
                Toast.makeText(context, "MAIN ACTIVITY IS BEING CALLED ", Toast.LENGTH_LONG).show();
                Intent i = new Intent(context, MainActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
                context.startActivity(i);
            }
        }
    }
}

MainActivity.java

public class MainActivity extends Activity 
{
    private MyReceiver myReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_USER_PRESENT);
        myReceiver = new MyReceiver();
        registerReceiver(myReceiver, filter);
    }

    @Override
    protected void onDestroy() 
    {
        if (myReceiver != null)
        {
            unregisterReceiver(myReceiver);
            myReceiver = null;
        }           
        super.onDestroy();
    }
}

There is no need to register the receiver in the manifest, if you have already done so in the Activity.

Also, it is better to start your activity from ACTION_USER_PRESENT, as this means that the device has been unlocked after being locked by power button press. But, if you want to start the activity from ACTION_SCREEN_ON, you can do that as well, that will also work, but the activity will be visible only after the user has unlocked the device.

EDIT

if you want receiver which receive broadcast event from outside app you can use service for that

First, unlike other broad casted intents, for Intent.ACTION_SCREEN_OFF and Intent.ACTION_SCREEN_ON you CANNOT declare them in your Android Manifest! so You need to make a service which will keep on running like this

public static class UpdateService extends Service {

        @Override
        public void onCreate() {
            super.onCreate();
            // register receiver that handles screen on and screen off logic
            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            BroadcastReceiver mReceiver = new Receiver();
            registerReceiver(mReceiver, filter);
        }

        @Override
        public void onStart(Intent intent, int startId) {
            boolean screenOn = intent.getBooleanExtra("screen_state", false);
            if (!screenOn) {
                // your code
            } else {
                // your code
            }
        }
}

and your receiver can be something

public class Receiver extends BroadcastReceiver {

    private boolean screenOff;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            screenOff = true;
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            screenOff = false;
        }
        Intent i = new Intent(context, UpdateService.class);
        i.putExtra("screen_state", screenOff);
        context.startService(i);
    }

}

You need to add <service android:name=". UpdateService"/>

and run this service from activity via

context.startService(new Intent(this, UpdateService.Class));

Here is complete example and fit for your requirement

http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...