Intent action for new network added (Android) - android

WiFiManager has an addNetwork(wifiConfiguration) method which allows to programmatically add a new network. This is also performed behind the scenes by the system whenever the user/native-manager tries to connect to a new access point and I want to listen to this event.
I tried:
IntentFilter myStateChanged = new IntentFilter();
myStateChanged.addAction( WifiManager.NETWORK_IDS_CHANGED_ACTION );
ctx.registerReceiver(myStateChgRcvr, myStateChanged);
But in my broadcast receiver what do I do with the intent object to get the desired info? I want to ascertain that this is indeed a case of a new network profile added and I want to get that network's info:
private BroadcastReceiver myStateChgRcvr = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent i) {
i.getParcelableExtra(...)); //???
}
};
Many thanks,
Spitzer

But in my broadcast receiver what do I do with the intent object to get the desired info?
Nothing. There are no documented Intent extras on that Intent.
I want to ascertain that this is indeed a case of a new network profile added and I want to get that network's info:
Call getConfiguredNetworks() on WifiManager and see if anything has changed, I guess.

Related

Get view of activity and fragments from service

I have on my app a service who get my location (gps). This service get information like latitude, longitude, etc, and I want wrote these information in layouts fragment and activity (TextView).
But how can I get view reference of theses fragments since my service ?
You can do one thing to achieve this. send broadcast to the activity. And on that activity where you want to update view register broadcast there. Once you get the location stuff then send broadcast. Hope that helps you. Write this code in service when you get location stuff and pass that stuff via intent as mentioned in below code
Intent broadcastIntent = new Intent(your action here);
broadcastIntent.putExtra(BaseBroadCastReceiver.BROADCAST_KEY_AUDIO_INDEX, audioIndex);
broadcastIntent.putExtra(BaseBroadCastReceiver.BROADCAST_KEY_AUDIO_LIST, mSongList.size());
sendBroadcast(broadcastIntent);
And in your activity write below code
private void registerMyReceiver() {
IntentFilter filter = new IntentFilter(your action here you passed in service);
registerReceiver(playNewAudio, filter);
}
NOTE: your action can be any string but string must be same on both side
When activity's oncreate method is called, call registerMyReceiver() method
and in onDestroy method unregister is else you may get RUNTIME exception.
Below will be your code for actual broad cast receiver
private BroadcastReceiver myBroadcast= new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//do your stuff here... get extra which you passed
// from service and set that value in views
}
};

To check whether the wifi scan is complete

I'm writing a code for getting the information of all the access points in the range continuously.
Here is my code:
myRunnable = new Runnable() {
#Override public void run() {
while(){
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.startScan();
List<ScanResult>results= wifi.getScanResults();
try{
//data Logging
}
}
}
};
myThread = new Thread(myRunnable);
myThread.start();
As the scanning and logging of data continuous repeatedly, i want to check whether the scan is complete before logging data.
Is there any flag or function which checks for wifi.startScan() is complete or not , before logging the data.
Could you please help me out with a code.
i want to check whether the scan is complete before logging data. Is
there any flag or function which checks for wifi.startScan() is
complete or not , before logging the data. Could you please help me
out with a code.
Yes, there is mechanizm designated for this purpose. To reach your goal you need to implement BroadcastReceiver that will listen for WiFi scans.
All what you need is to create BroadcastReceiver with appropriate IntentFilter. So you need this action for filter:
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION
This action means that an access point scan has completed, and results are available. And then just create BroadcastReceiver (statically or dynamically, it's doesn't matter).
If you don't know how to start, look at this tutorial:
Android BroadcastReceiver Tutorial
To build up on the answers above, I just wanted to add the related code to give an example.
1. Registering for the event
// don't forget to set permission to manifest
wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
context.registerReceiver(this,new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
//check if WIFI is enabled and whether scanning launched
if(!wifiManager.isWifiEnabled() || !wifiManager.startScan())
{
throw new Exception("WIFI is not enabled");
}
2. Receiving the result
To receive, the object registered (in this case this) needs to extend BroadcastReceiver and implement the following method:
public void onReceive(Context context, Intent intent) {
List<ScanResult> result = wifiManager.getScanResults();
}
You need to implement a BroadcastReceiver listening for the scan results returned from WifiManager.startScan(). onReceive() allows you to access the scan results directly. It takes about 1 second for the scan to complete and trigger onReceive()...

Listening for Bluetooth Status

I'm writing a code that listens for the Bluetooth device to become disconnected, then does something. How would I go about doing this? I'm not exactly sure what I want to put after it yet, figured I'd get this sorted out first. Hopefully I wasn't completely wrong with this code, as I'm new to developing. This is what I have so far:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (BluetoothAdapter.ACTION_ACL_DISCONNECTED.equals(action))
{ // This will be the followup action, once I figure out what I want it to be.
First, use the BluetoothChat example to start with as coding with Bluetooth is pretty complex and it's nice to have working code. What you want to look at is handleMessage() in BluetoothChat.java, and you'll need to save off the state. When the state changes from BluetoothChatService.STATE_CONNECTED to BluetoothChatService.STATE_NONE, the device has become disconnected.

Programmatic Intent Filter for NFC

EDIT: I found the solution, see below
My first post on StackOverFlow. However I have been reading about this problem for a while without a solution that works.
What I would like to do is register the following Intent: android.nfc.action.TAG_DISCOVERED
I am doing the following in my Code:
IntentFilter filter = new IntentFilter();
filter.addAction("android.nfc.action.TAG_DISCOVERED");
filter.addCategory("android.intent.category.DEFAULT");
Log.d(TAG, "Created the new filter");
reciever = new NFCBroadcastReciever(this);
Log.d(TAG, "Created the new Broadcast Reciever");
this.registerReceiver(reciever, filter);
Log.d(TAG, "Registered new reciever");
The BroadCastReciever is defined as follows:
public class NFCBroadcastReciever extends BroadcastReceiver {
private Screen screen;
public static String TAG = "NFCBroadcastReciever";
NFCBroadcastReciever(Screen _screen){
screen = _screen;
}
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG, "Action recieved: "+action);
if(action != null && NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)){
paymentScreen.onNewIntent(intent);
}
}
}
However I get an exception that the intent being fired from a tag read has no corresponding Activity. I would like to be able to only start listening for NFC events at a certain point in my application.
Thanks in advance for your help.
I found the solution to the problem actually, the key to getting NFC events to occur only on a specific activity while it is active and not when other activities are running. The sample in the Android SDK explains it: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/nfc/ForegroundDispatch.html
I found the solution to the problem actually, the key to getting NFC events to occur only on a specific activity while it is active and not when other activities are running. The sample in the Android SDK explains it: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/nfc/ForegroundDispatch.html
Is your intention to start an activity when the broadcast is received? It doesn't seem to me that paymentScreen.onNewIntent(intent); is going to accomplish that. Instead, you will likely need to build an intent that you can use with startActivity() and you'll likely want to include the relevant data from your broadcast receiver's intent into your activity intent in the form of extras.

BroadcastReceiver on new app installs

I want to receive a notification when a new application is installed.
IntentFilter newAppFilter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
newAppFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
newAppFilter.addAction(Intent.ACTION_PACKAGE_INSTALL);
newAppFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
getApplicationContext().registerReceiver(newAppReceiver, newAppFilter);
public static BroadcastReceiver newAppReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Log.e("Broadcast","Received");
}
};
But I am not able to get any log. Anyone can help me?
Try to add data scheme to your IntentFilter.
newAppFilter.addDataScheme("package");
Reference: IntentFilter.addDataScheme() documentation
If no schemes are included, then an
Intent will match only if it includes
no data.
If anyone runs across this, the intent documentation now says:
ACTION_PACKAGE_INSTALL - This constant is deprecated. This constant has never been used.

Categories

Resources