Example usage for android.content IntentFilter IntentFilter

List of usage examples for android.content IntentFilter IntentFilter

Introduction

In this page you can find the example usage for android.content IntentFilter IntentFilter.

Prototype

public IntentFilter(Parcel source) 

Source Link

Usage

From source file:Main.java

public static boolean isPowerConnected(Context context) {
    Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

    int status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    boolean isCharging = ((status == BatteryManager.BATTERY_STATUS_CHARGING)
            || (status == BatteryManager.BATTERY_STATUS_FULL));

    return isCharging;
}

From source file:Main.java

public static float getBatteryLevel(Context context) {
    Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

    // Error checking that probably isn't needed, just in case.
    if (level == -1 || scale == -1) {
        return 0.50f;
    }/*from www .j  a  va 2s. c  o  m*/

    return ((float) level / (float) scale);
}

From source file:Main.java

/**
 * @return true is device is plugged at this moment, false otherwise.
 * @see #isPlugged(android.content.Intent)
 *//*from   w  w  w. j  av a 2 s  .  c o  m*/
public static boolean isPlugged(@NonNull Context context) {
    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    return isPlugged(context.getApplicationContext().registerReceiver(null, intentFilter));
}

From source file:com.Context.HeadsetWatcher.java

@Override
public PluginResult execute(String action, JSONArray data, String callbackId) {
    this.callback = callbackId;
    headsetReceiver = new HeadsetBroadcastReceiver(this);
    PluginResult result = new PluginResult(Status.NO_RESULT);
    this.cordova.getActivity().registerReceiver(headsetReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
    result.setKeepCallback(true);//from   w  w  w. j av a2s  .co  m
    return result;
}

From source file:Main.java

/**
 * Rudimentary methods wrapping the use of a LocalBroadcastManager to simplify the process
 * of notifying other classes when a particular fragment is notified that a permission is
 * granted.//from w ww .j a v a  2  s  . c om
 *
 * To be notified when a permission has been granted, create a new broadcast receiver
 * and register it using {@link #registerPermissionReceiver(Context, BroadcastReceiver, String)}
 *
 * E.g.
 *
 * final BroadcastReceiver receiver = new BroadcastReceiver() {
 *     @Override
 *     public void onReceive(Context context, Intent intent) {
 *         refreshContactsView();
 *     }
 * }
 *
 * PermissionsUtil.registerPermissionReceiver(getActivity(), receiver, READ_CONTACTS);
 *
 * If you register to listen for multiple permissions, you can identify which permission was
 * granted by inspecting {@link Intent#getAction()}.
 *
 * In the fragment that requests for the permission, be sure to call
 * {@link #notifyPermissionGranted(Context, String)} when the permission is granted so that
 * any interested listeners are notified of the change.
 */
public static void registerPermissionReceiver(Context context, BroadcastReceiver receiver, String permission) {
    final IntentFilter filter = new IntentFilter(permission);
    LocalBroadcastManager.getInstance(context).registerReceiver(receiver, filter);
}

From source file:com.hybris.mobile.app.commerce.activity.ApplicationActivity.java

@Override
protected void onResume() {
    super.onResume();

    // Register a broadcast for resuming the activity
    LocalBroadcastManager.getInstance(this).registerReceiver(this.mResumeContextBroadcastReceiver,
            new IntentFilter(getString(R.string.intent_action_resume)));
}

From source file:cm.aptoide.pt.services.MainService.java

@Override
public IBinder onBind(Intent intent) {
    registerReceiver(receiver, new IntentFilter("complete"));
    return new LocalBinder();
}

From source file:io.v.positioning.BluetoothPositionActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bluetoothposition);
    Button scanButton = (Button) findViewById(R.id.button_scan);
    scanButton.setOnClickListener(new View.OnClickListener() {
        @Override//from ww  w .j a v  a  2s . com
        public void onClick(View v) {
            doDiscovery();
            v.setVisibility(View.GONE);
        }
    });
    mRecordedDevices = new ArrayAdapter<String>(this, R.layout.device_name);
    ListView recordedDevicesListView = (ListView) findViewById(R.id.recorded_devices);
    recordedDevicesListView.setAdapter(mRecordedDevices);
    this.registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
    this.registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED));
    mBtAdapter = BluetoothAdapter.getDefaultAdapter();
}

From source file:com.messagesight.mqtthelper.PayloadViewer.java

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.payload);//from www.  j a  v a  2  s.co  m
    LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("payload"));
    expListView = (ExpandableListView) findViewById(R.id.expandableListView);

    Intent intent = getIntent();
    String jsonString = "";
    jsonString = intent.getStringExtra("json");

    // System.out.println("JSON Present: "+jsonString);
    headers = MqttHandler.getInstance().topicsReceived;
    listChildren = MqttHandler.getInstance().payload;
    payloadAdapter = new PayloadAdapter(this, headers, listChildren);
    expListView.setAdapter(payloadAdapter); //set adapter

    // populate the listView for JSONObject Payloads
    if (jsonString != null) {

        try {
            JSONObject json = new JSONObject(jsonString);

            tempHeaders = new ArrayList<String>();
            tempListChildren = new HashMap<String, List<String>>();
            // headers.clear();
            // listChildren.clear();

            Iterator<?> keys = json.keys();

            while (keys.hasNext()) {
                String key = (String) keys.next();

                tempHeaders.add(key);

                List<String> val = new ArrayList<String>();
                val.add(json.get(key).toString());
                tempListChildren.put(key, val);
            }

            payloadAdapter = new PayloadAdapter(this, tempHeaders, tempListChildren);
            expListView.setAdapter(payloadAdapter);
            isJsonView = true;

        } catch (JSONException e) {

        }
    }

    // Listview on child click listener
    expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition,
                long id) {
            // TODO Auto-generated method stub
            List<String> tempHeadersOnClick;
            HashMap<String, List<String>> tempListChildrenOnClick;
            if (isJsonView) {
                tempHeadersOnClick = tempHeaders;
                tempListChildrenOnClick = tempListChildren;
            } else {
                tempHeadersOnClick = headers;
                tempListChildrenOnClick = listChildren;

            }

            JSONObject json = null;
            try {
                json = new JSONObject(
                        tempListChildrenOnClick.get(tempHeadersOnClick.get(groupPosition)).get(childPosition));
            } catch (JSONException e) {

            }

            if (json != null) {
                Intent intent = new Intent(getApplicationContext(), PayloadViewer.class);
                intent.putExtra("json", json.toString());
                startActivity(intent);
            }
            return false;
        }
    });

}

From source file:com.manning.androidhacks.hack021.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);//from   www  .  java2s. c  o m

    mTextView = (TextView) findViewById(R.id.text_view);

    mProgressDialog = ProgressDialog.show(this, "Loading", "Please wait");
    mProgressDialog.show();

    mReceiver = new MyServiceReceiver();
    mIntentFilter = new IntentFilter(MyService.ACTION);

    startService(new Intent(this, MyService.class));
}