In your main activity, you have to write something like

    BroadcastReceiver mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                    Log.v(TAG, "onReceive()");
            }
    };

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter
(Intent.ACTION_SCREEN_ON);
        registerReceiver(mReceiver, filter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mReceiver);
    }

Peli
www.openintents.org

On Jul 20, 5:33 pm, jiaoni <[email protected]> wrote:
> I tried this in my activity, but still cannot receive event:
>
> MessageReceiver receiver = new MessageReceiver();
> IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
> registerReceiver(receiver, filter);
>
> I feel frustrated and don't know why...
>
> On Jul 20, 9:58 pm, JP <[email protected]> wrote:
>
> > I've double checked my code and found I'm doing this slightly
> > differently, although the method above should also work.
>
> > Strike "Register" broadcast receiver in the manifest - this seems to
> > be meerly a declaration of the receiver. No intent filters; I check
> > the specific event types _ON/_OFF in the receiver's onReceive()
> > method.
> > The key: I register the BroadcastReceiver manually, using
> > Context.registerReceiver(BroadcastReceiver receiver, IntentFilter
> > filter)
>
> > On Jul 19, 8:22 pm, jiaoni <[email protected]> wrote:
>
> > > Thanks, JP. I have added following lines in AndroidManifest.xml, but
> > > still cannot receive any message in my BroadcastReceiver's onReceive()
> > > method. Is there anything I did wrong, or may I need some permission
> > > to receiveSCREEN_ON/SCREEN_OFF message? Please help.
>
> > > <receiver android:name=".MessageReceiver" android:process=":remote">
> > >         <intent-filter>
> > >                         <action 
> > > android:name="android.intent.action.SCREEN_ON"></action>
> > >         <action android:name="android.intent.action.SCREEN_OFF"></action>
> > >                         </intent-filter>
> > > </receiver>
>
> > > On Jul 18, 7:02 am, JP <[email protected]> wrote:
>
> > > > 1. You need to create a subclass of BroadcastReceiver, override
> > > > onReceive(Context context, Intent intent).
> > > > In onReceive(), extract action from intent and catch ACTION_SCREEN_ON
> > > > and ACTION_SCREEN_OFF
> > > > 2. Next you need to register the BroadcastReceiver in
> > > > AndroidManifest.xml
>
> > > > On Jul 17, 12:02 am, jiaoni <[email protected]> wrote:
>
> > > > > How can I catchSCREEN_ON/SCREEN_OFF broadcasts?
>
> > > > > I have tried for hours, but failed. Please help.
>
> > > > > Thanks a lot.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to