Does system broadcasts any intents during factory reset and system updates? - android

how to find factory reset is called in android

Old topic but maybe will help someone.
There is not any official event which states that Factory Data Reset will be performed. However for Android <= 22 (Lollipop) there is unofficial (hidden in the code) broadcast intent android.intent.action.MASTER_CLEAR which is fired when External Storage will be formatted. If you click factory reset for Android Settings then MASTER_CLEAR intent is fired, in the result you can state that there will be factory reset.
Sample code:
// Class Member
private BroadcastReceiver receiver;
.
.
.
// In some method, e.g. onCreate()
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.ACTION_SHUTDOWN");
filter.addAction("android.intent.action.MASTER_CLEAR");
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(
Intent.ACTION_SHUTDOWN)) {
Log.d(TAG, "SHUTDOWN")
}
if (intent.getAction().equals("android.intent.action.MASTER_CLEAR")) {
Log.d(TAG, "FACTORY DATA RESET")
}
};
registerReceiver(receiver, filter);
If there will be factory reset then you will see MASTER_CLEAR action followed by SHUTDOWN action. I tested on Nexus 5 and Androids 4.4.4, 5.0, 6.0, 6.0.1 and works.

there is no event to track factory reset

Related

Issue handling lost Bluetooth connections using BroadcastReceiver when connected to two devices

I have the following code to handle lost Bluetooth connections.
public class BluetoothReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
//call method to cancel connection thread
}
}
}
However, I have another app that uses Bluetooth connected to a different device running in the background. If I lose connection to that device, I also lose the connection to the device within this app.
I was wondering, is there any way to prevent this?
As described here you are able to get the device instance from the intent with intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) If the device is NOT the one you are interested in, just DO NOT call:
//call method to cancel connection thread

Broadcast action for WIFI change

In my application I have to get notified whenever the device connects or disconnects from a WIFI network. For this I have to use a BroadcastReceiver but after reading through different articles and questions here on SO I'm a bit confused which Broadcast action I should use for this. In my opinion I have three choices:
SUPPLICANT_CONNECTION_CHANGE_ACTION
NETWORK_STATE_CHANGED_ACTION
CONNECTIVITY_ACTION
To reduce resources I really only want to get notified whenever the device is CONNECTED to a WIFI network (and it has received an IP address) or when the device has DISCONNECTED from one. I do not care about the other states like CONNECTING etc.
So what do you think is the best Broadcast action I should use for this? And do I have to manully filter the events (because I receieve more then CONNECTED and DISCONNECTED) in onReceive?
EDIT: As I pointed out in a comment below I think SUPPLICANT_CONNECTION_CHANGE_ACTION would be the best choice for me but it is never fired or received by my application. Others have the same problem with this broadcast but a real solution for this is never proposed (in fact other broadcasts are used). Any ideas for this?
You can go for WifiManager.NETWORK_STATE_CHANGED_ACTION works.
Register receiver with WifiManager.NETWORK_STATE_CHANGED_ACTION Action, either in Manifest or Fragment or Activity, which ever suited for you.
Override receiver :
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if(action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
boolean connected = info.isConnected();
if (connected)
//call your method
}
}
Please try
#Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction("android.net.wifi.STATE_CHANGE");
registerReceiver(networkChangeReceiver, filter);
}
#Override
protected void onDestroy() {
unregisterReceiver(networkChangeReceiver);
super.onDestroy();
}
and
BroadcastReceiver networkChangeReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (!AppUtils.hasNetworkConnection(context)) {
showSnackBarToast(getNetworkErrorMessage());
}
}
};
I am using this and it is working for me. Hope it will help you out.

Way to ensure delivery of Action HEADSET_PLUG for ZTE T815 Android phone

I have registered a BroadcastReceiver to receive ACTION_HEADSET_PLUG which works fine for most devices, ie it is called whenever the headset is plugged or unplugged. But on others eg the ZTE T815, the Intent is never sent/received when the headset is plugged/unplugged.
For reference here is the code for the receiver registration:
private final BroadcastReceiver headsetPlugReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Received intent=" + intent);
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_HEADSET_PLUG)) {
// do stuff
}
}
};
public void onCreate(Bundle savedState) {
super.onCeate(savedState);
// ...
registerReceiver(headsetPlugReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
}
Further info:
The Intent is dispatched but only after the HEADSET_HOOK command is fired on the headset.
And when the Intent is dispatched
final int microphone = intent.getIntExtra("microphone", 0);
always returns 0 (ie no microphone).
So
Is there some config/code that can force the delivery of this
Intent?
How do I get the Intent to correctly report whether a
microphone exists or not?
It turns out the ZTE T815 has an OMTP TRRS config for its audio socket instead of CTIA/AHJ like every other modern Android device.
See http://en.wikipedia.org/wiki/Phone_connector_%28audio%29
A sad state of affairs, especially when trying to use audio feed inpout across products.

As I can register an intent to listen when the NFC switch to on / off?

I am making a widget that lets go NFC settings and activate it, now I just need to know how to register an intent in the NFC onReceive whether a change on / off.
Any help would be highly appreciated.
#Override
public void onReceive(Context context, Intent intent)
{
manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
adapter = manager.getDefaultAdapter();
//OR
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()))
{
actualizar(context);
}
//OR
if (intent.getAction().equals("ADAPTER_STATE_CHANGE"))
{
actualizar(context);
}
}
You can detect changes to NFC settings once your app is created and resumed. See this example.
There isn't such a broadcast event. Try reading the logs, this would work but is very unreliable and fragmented. Alternatively you can check the state of things using NfcAdapter.isEnabled() call at specific points in your app.

Android bluetooth ACTION_DISCOVERY_FINISHED not working

I have written my first Android app and everything is working really well, except...in the routine, below, the ACTION_DISCOVERY_FINISHED never seems to get called (or broadcast or received or whatever). No matter what the block of code in that "else if" is not working.
I have only tested on my Motorola Atrix, so I am wondering if that is the issue. Since I am testing bluetooth functionality, I don't think I can use the Android emulator for effective testing.
Thoughts?
private BluetoothAdapter mBtAdapter;
mBtAdapter.startDiscovery();
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
//do something
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
//do something else
}
}
}
2 possibles solutions:
Instead of creating an annonymous receiver, subclass BroadcastReceiver with just the same implementation, then declare it in your project manifest (Remember to declare that your receiver receives these actions you want).
Dynamically register it from your activity/service, this way:
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(mReceiver, filter);
I'm not sure if you have to unregister it when registering it from an activity/service (I know you have to when registering from app's context) so check it out.
You need to add the line
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
to your manifest

Categories

Resources