开发者

Implementing a BroadcastReceiver in the AndroidManifext.xml File

开发者 https://www.devze.com 2023-02-20 16:10 出处:网络
I am trying to use a BroadcastReceiver in Android (using the manifest) and just had a quick question.

I am trying to use a BroadcastReceiver in Android (using the manifest) and just had a quick question.

So far I have this...

<receiver android:name=".Listener"
  android:label="testRecv"
  android:enabled="true"
  android:exported="true"
  android:process=":recovery">
  <intent-filter>
    <action android:name="android.intent.action.SCREEN_ON"/>
  </intent-filter>
</receiver>

But when I install the apk on a test device or emulator, nothing is happening what I turn on my screen. Here is开发者_JAVA百科 the code for the Listener class...

public class Listener extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.SCREEN_ON")) {
            Toast t = Toast.makeText(context, "hey", 10);
            t.show();
        }
    }
}

I am never seeing the toast get fired off (as a test).

Is there something I am missing? I never used Context.registerReceiver() because I declared the in the manifest, so I though I didn't need to.

The most confusing part about this whole thing is the and the documentation isn't that helpful for it. Can anybody just help me understand this stuff?


See CommonsWare's reply in Android - how to receive broadcast intents ACTION_SCREEN_ON/OFF?


if (intent.getAction().equals("android.intent.action.SCREEN_ON")) {
            Toast t = Toast.makeText(context, "hey", 10);
            t.show();
        }

You don't need the if statement

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号