听取ACTION_DREAMING_STOPPED

时间:2014-03-14 17:40:39

标签: android daydream

当DayDream停止时,我如何让我的应用程序听取。当系统停止做梦时,它会发送ACTION_DREAMING_STOPPED字符串。我在OnResume和onCreate中添加了一个BroadcastReceiver,并且在DayDream停止时都没有使用它。那么我应该把听众放在哪里?如果我用错误的名字打电话,我很抱歉,我以前没有和DayDream合作过。

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

1 个答案:

答案 0 :(得分:1)

可以在BroadcastReceiver中创建onCreate

确保您使用registerReceiver(receiver, filter)注册接收者,并且intent-filter内有AndroidManifest.xml

样品:

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>