Search code examples
androiddaydream

Listening for ACTION_DREAMING_STOPPED


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();
}

Solution

  • 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>