Android Open Source - ramp_up_android Broadcasts Activity






From Project

Back to project page ramp_up_android.

License

The source code is released under:

Apache License

If you think the Android project ramp_up_android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.macadamian;
//w  w w .  j a v  a 2 s .c o  m
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.TextView;

public class BroadcastsActivity extends Activity {
    private BroadcastReceiver _receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String act = intent.getAction();
                if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(act)) {
                    show_network_info(intent);
                } else if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(act)) {
                    show_ringer_mode(intent);
                } else if (Intent.ACTION_TIME_TICK.equals(act)) {
                    show_tick(intent);
                }
            }
        };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.broadcasts);
        register();
    }

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

    private void register() {
        IntentFilter filt = new IntentFilter();
        filt.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        filt.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
        filt.addAction(Intent.ACTION_TIME_TICK);
        registerReceiver(_receiver, filt);
    }

    private void show_network_info(Intent i) {
        NetworkInfo ni = (NetworkInfo) i.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        switch (ni.getState()) {
            case DISCONNECTED:
                show("Network DISCONNECTED");
                break;
            case DISCONNECTING:
                show("Network DISCONNECTING");
                break;
            case CONNECTING:
                show("Network CONNECTING");
                break;
            case CONNECTED:
                show("Network CONNECTED");
                break;
        }
    }

    private void show_ringer_mode(Intent i) {
        int mode = i.getIntExtra(AudioManager.EXTRA_RINGER_MODE, AudioManager.RINGER_MODE_NORMAL);
        switch (mode) {
            case AudioManager.RINGER_MODE_NORMAL:
                show("Ringer mode: NORMAL");
                break;
            case AudioManager.RINGER_MODE_SILENT:
                show("Ringer mode: SILENT");
                break;
            case AudioManager.RINGER_MODE_VIBRATE:
                show("Ringer mode: VIBRATE");
                break;
        }
    }

    private void show_tick(Intent i) {
        show("Time has changed");
    }

    private void show(String m) {
        ((TextView) findViewById(R.id.broadcasts_output_messages)).append(m +"\n");
    }
}




Java Source Code List

com.macadamian.ActionBarExampleActivity.java
com.macadamian.ActionsActivity.java
com.macadamian.BasicUserInterfaceActivity.java
com.macadamian.BroadcastsActivity.java
com.macadamian.CursorAdapterActivity.java
com.macadamian.DataExamplesFragment.java
com.macadamian.ExamplePreferenceFragment.java
com.macadamian.ExamplesSelectionActivity.java
com.macadamian.ExecutionControlExamplesFragment.java
com.macadamian.FragmentsActivity.java
com.macadamian.InAppBillingActivity.java
com.macadamian.IntentsActivity.java
com.macadamian.IntentsStartedActivity.java
com.macadamian.SimpleListAdapterActivity.java
com.macadamian.SystemExamplesFragment.java
com.macadamian.UserInterfaceExamplesFragment.java
com.macadamian.ViewPagerActivity.java
com.macadamian.billing.Base64DecoderException.java
com.macadamian.billing.Base64.java
com.macadamian.billing.IabException.java
com.macadamian.billing.IabHelper.java
com.macadamian.billing.IabResult.java
com.macadamian.billing.Inventory.java
com.macadamian.billing.Purchase.java
com.macadamian.billing.Security.java
com.macadamian.billing.SkuDetails.java