0

How would I get my app to listen for when DayDream stops. When the system stops dreaming it sends the ACTION_DREAMING_STOPPED string out. I have added a BroadcastReceiver in my OnResume and onCreate and neither are used when DayDream stops. So where should I put my listener? I do apologize if I am calling something by its wrong name, I haven't worked with DayDream before.

@Override
protected void onResume() {
    mDreamingBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_DREAMING_STOPPED)) {
                // Resume the fragment as soon as the dreaming has
                // stopped
                Intent intent1 = new Intent(MainActivity.this, MainWelcome.class);
                startActivity(intent1);
            }
        }
    };
    super.onResume();
}
Aaron
  • 4,380
  • 19
  • 85
  • 141

1 Answers1

1

The BroadcastReceiver can be created in your onCreate.

Ensure you register the receiver with: registerReceiver(receiver, filter) and that you've got the intent-filter inside your AndroidManifest.xml.

Sample:

MainActivity.java

public class MainActivity extends Activity 
{
    private static final String TAG = MainActivity.class.toString();

    private BroadcastReceiver receiver;
    private IntentFilter filter;

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

        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent)
            {
                Log.d(TAG, TAG + " received broacast intent: " + intent);
                if (intent.getAction().equals(Intent.ACTION_DREAMING_STOPPED)) {
                    Log.d(TAG, "received dream stopped");
                }
            }
        };

        filter = new IntentFilter("android.intent.action.DREAMING_STOPPED");
        super.registerReceiver(receiver, filter);
    }
}

AndroidManifest.xml

<activity
    android:name="com.daydreamtester.MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <action android:name="android.intent.action.DREAMING_STOPPED" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
Justin Jasmann
  • 2,363
  • 2
  • 14
  • 18
  • What is new PlaceholderFragment()? – Aaron Mar 14 '14 at 20:29
  • Thanks, I didn't know how to register receivers. Can you explain the if statement. I don't understand what you are doing in there – Aaron Mar 14 '14 at 20:35
  • Oh sorry, that just comes from the default Android app template (copy/pasted). You don't need it for this particular example - I'll edit. – Justin Jasmann Mar 14 '14 at 23:21