I need to handle the connectivity change broadcast in my app. Every thing is great except that when it comes for the broadcast the application crashes. I am using the following code in my Broadcast :
#Override
public void onReceive(Context context, Intent intent) {
Log.i("NET", "Broadcast started");
Intent startServiceIntent = new Intent(context, NewsService.class);
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
if(noConnectivity) {
context.stopService(startServiceIntent);
Toast.makeText(context, "Connection is terminated!", Toast.LENGTH_LONG).show();
Log.i("NET", "Stopped");
}
else {
context.startService(startServiceIntent);
Toast.makeText(context, "Connection is ok!", Toast.LENGTH_LONG).show();
}
}
This code is supposed to stop a service when no internet connection is found and to start it whenever it finds a connection.
Lastly and after 2 days of searching I found the problem, it is about the package the broadcast is placed in I found it here
Related
I'm currently trying to get broadcast receivers running in the background of my android application, which I've been told to use an event service for. At present my broadcast receivers work fine if you're in the activity within which they're registered
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
#Override
public void onReceive(Context arg0, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BATTERY_LOW)) {
Toast.makeText(arg0, "Battery's dying!!", Toast.LENGTH_LONG).show();
Log.e("LOW", "LOW");
intent = null;
}else if(intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)) {
Toast.makeText(arg0, "Battery's discharging!!", Toast.LENGTH_LONG).show();
Log.e("discharge", "discharge");
intent = null;
}else if(intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
Toast.makeText(arg0, "Battery's charging!!", Toast.LENGTH_LONG).show();
Log.e("charge", "charge");
intent = null;
}else if(intent.getAction().equals(Intent.ACTION_BATTERY_OKAY)) {
Toast.makeText(arg0, "Battery's okay!!", Toast.LENGTH_LONG).show();
Log.e("OKAY", "OKAY");
intent = null;
}
}
};
in OnCreate:
registerReceiver(this.mBatInfoReceiver,
new IntentFilter(Intent.ACTION_BATTERY_LOW));
registerReceiver(this.mBatInfoReceiver,
new IntentFilter(Intent.ACTION_BATTERY_OKAY));
registerReceiver(this.mBatInfoReceiver,
new IntentFilter(Intent.ACTION_POWER_DISCONNECTED));
registerReceiver(this.mBatInfoReceiver,
new IntentFilter(Intent.ACTION_POWER_CONNECTED));
Despite the issues that arise with leaking intentfilters by making them in oncreate, my current issue that that when I change activity these no longer run, I've read from the following: http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html#startingservices_alarmmanager
That by placing these into an IntentService and starting this service, they'll run consistently, however this involves registering the receivers in the manifest, which gives me issues in that my application is planning on allowing the user to listen out for specific events IE: these cannot be created dynamically.
Is there a way to dynamically create broadcast receivers within a class, which runs in the background of the application and is triggered when the broadcast occurs?
I have a bluetooth device.
If:
They have already paired and connected to the device
it becomes unplugged
then it becomes plugged back in
I would like to auto-connect to it. That's why I'm listening to ACTION_FOUND.
My code is fairly simple. The ACTION_BOND_STATE_CHANGED and ACTION_ACL_DISCONNECTED work just fine. I am completely unable to get the ACTION_FOUND to catch though. My "INTENT RECEIVED" log message never prints…
I feel like I am missing something simple. Thanks!
public void registerReceiver() {
if (BuildConfig.DEBUG) Log.e(TAG, "REGISTERING RECEIVER");
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
mainActivity.registerReceiver(receiver, filter);
}
private final BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BuildConfig.DEBUG) Log.e(TAG, "INTENT RECEIVED: " + String.valueOf(action));
if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// … some stuff …
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
autoConnectDevice();
} else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
disconnectDevice();
}
}
};
public void unregisterReceiver() {
if (BuildConfig.DEBUG) Log.e(TAG, "UNREGISTERING RECEIVER");
mainActivity.unregisterReceiver(receiver);
}
I believe the ACTION_FOUND event only fires when you are running device discovery with the intent to pair your device to another. It would not fire when an already paired device comes back into range. Unfortunately, there does not appear to be an event for the case that you want.
Your best bet will be to start an AsycTask or Thread when the connection is lost and try to reconnect until successful or until you hit an arbitrary time limit and give up.
I have written the following code for detecting the network status from within the BroadcastReceiver. I start a service when the network is available and stop the service when the network is not available.
I have the following class level variable.
private boolean IsNetworkAlreadyConnected = false;
Within onCreate method of the main class I start the service if the internet is available.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (InternetConnectivity.isConnected(MainActivity.this)) {
IsNetworkAlreadyConnected = true;
Intent timerIntent = new Intent(getBaseContext(), InActivityTimer.class);
startService(timerIntent);
}
}
and below is the code for my BroadcastReceiver in the same class,
public class mConnectivityCheckReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("android.net.conn.CONNECTIVITY_CHANGE")) {
try {
boolean networkAvailable = InternetConnectivity.isConnected(context);
if (networkAvailable) {
if (!IsNetworkAlreadyConnected) {
Intent timerIntent = new Intent(getBaseContext(), InActivityTimer.class);
startService(timerIntent);
IsNetworkAlreadyConnected = true;
}
else {
Log.d("KC_HomeActivity", "Network was already connected. No need to start service again.");
}
}
else {
Log.d("KC_HomeActivity", "Network Disconnected. Service Stopped.");
IsNetworkAlreadyConnected = false;
Intent timerIntent = new Intent(getBaseContext(), InActivityTimer.class);
stopService(timerIntent);
}
}
catch (Exception e) {
}
}
}
};
When both Mobile data and Wifi are turned on then the service is started from onCreate method and it is not started again in the BroadcastReceiver but when I turn off the Wifi the Android changes the network mode to Mobile Data but for few seconds there is no internet connectivity and the service is stopped and then started again. I don't want to do this. If there is no connectivity only then the service should be stopped. If the network is shifting from Wifi to Mobile Data then the service should not be stopped.
Note: To check the internet connectivity I am using,
NetworkInfo info = InternetConnectivity.getNetworkInfo(context);
return (info != null && info.isConnectedOrConnecting());
Network connections aren't that precise. You should make it relax a bit, or you'll pull your hair out.
I would implement a smoothing function from the broadcasts. When you get a connectivity change notification, set a timeout for like 15 seconds. At that time, check your status and either start, stop, or do nothing. If another broadcast comes in, clear the first and reset for another 15 seconds. That will give the device time to reconnect.
I wrote an app, which shows a dialog when charge lvl is 15% or low.
Problem is that on one forum I received the message that it does not work on Google Nexus S (I9023). MIUI 2.3.7c.
code for receiver is (it is registered in manifest):
if (intent.getAction().equals(Intent.ACTION_BATTERY_LOW))
{
try
{
context.startService(myIntent);
}
catch(Exception ex){Toast.makeText(context, "Problem with Starting a Service", Toast.LENGTH_LONG).show();}
}
In service I register receiver with filter Intent.ACTION_BATTERY_CHANGED.
code for service in is:
...
if (batteryLevel == mLowLevelAlarm)
{
if (prefView.getBoolean(PREF_LOW_CHARGE_ON, true))
{
myIntent = new Intent(MyService.this, BatteryLow.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(myIntent);
}
}
...
Can anyone help to solve this problem?
thx
I implemented a broadcast receiver to "block" my app if the internet connection is lost.
By block I mean that the app has to open a "No internet connection" activity.
this is my receiver code:
public class ConnectivityReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
Log.d("** Debug **","noConnectivity " + noConnectivity);
if(noConnectivity){
//SHOW NO INTERNET CONNECTION ACTIVITY
}
}
}
Is it possibile to start NoInternetConnection.class when noConnectivity == true??
Thanks!
SOLUTION:
Intent i = new Intent(context, NoInternetConnection.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
You should just have to call startActivity:
context.startActivity(new Intent(NoInternetConnection.class));
You will need to make sure the "NoInternetConnection" activity is registered in your manifest file:
<activity android:name=".NoInternetConnection"></activity>
What issues are you having specifically?