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?
Related
I am trying to start two services on reboot of the device using only one Broadcastreceiver. But only one service is called.
Here is my receiver:
public class FirstReciever extends BroadcastReceiver{
private static final String TAG8 = "Mytag";
#Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Log.i(TAG8, "Restarting App after boot");
Intent myServiceInent = new Intent(context, FirstReciever.class);
context.startService(myServiceInent);
Intent myServiceInent1 = new Intent(context, DbService.class);
context.startService(myServiceInent1);
}
}
}
To start an Activity use:
startActivity(intent);
To Start a Service use:
startService(intent);
first of all look at this.
Intent myServiceInent = new Intent(context, FirstReciever.class);
context.startService(myServiceInent);
you have given the reference of your FirstReciever.class, that should be a service.
and if that doesn't work than start a single service and start another service from first one...
I want to upload images (using asyncTask),but if my internet is down,i want to send a broadcast intent and upload images automatically when internet is connected next time. I have successfully uploaded images when internet is active, I saw many examples but none of them helped me.
i have a class UploadImages which extends AsyncTask
I made broadcast receiver class like this
public class MyBroadcastReceiver extends BroadcastReceiver {
ConnectivityManager connec;
#Override
public void onReceive(Context context, Intent intent) {
connec = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connec != null && (connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) ||(connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED))
{
Intent intent1 = new Intent(context, UploadIntentService.class);
intent1.putExtra("", "index.html");
context.startService(intent);
}
}
}
Here is my UploadIntentService class
public UploadIntentService() {
super("uploadIntentService");
}
#Override
protected void onHandleIntent(Intent intent) {
new UploadImageToBucket().execute("");
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.alg.imgSave.MyBroadcastReceiver");
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
broadcastIntent.putExtra("RESPONSE_STRING", "1");
broadcastIntent.putExtra("RESPONSE_MESSAGE", "Uploaded");
sendBroadcast(broadcastIntent);
}
But this is not working
Please reference:
Broadcast receiver for checking internet connection in android app
The idea here would be that if your internet goes down, send the broadcast intent which this receiver is registered for, and once your internet connection changes this receiver will be launched and then you can make your request to upload the image.
I am newbie to android and I am implementing background service im my app, i am starting app on BOOT_COMPLETED process & i am calling the activity to start the app and app come on foreground. But i want to keep the app in background (Minimized) and i want to do slient login in app.
i am using below code for app start on Boot_Complete.
public class bootUpLocation extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
// here we start the service
Toast.makeText(context, "broadcast receiver start for location",
Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(context, LoginActivity.class);
myIntent.addCategory(Intent.CATEGORY_HOME);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
}
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
public class myReceiver extends BroadcastReceiver {
public static boolean wasScreenOn = true;
#Override
public void onReceive(final Context context, Intent recievedIntent) {
if (recievedIntent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
wasScreenOn = false;
Intent intent = new Intent(context,
myActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
context.startActivity(intent
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
} else if (recievedIntent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
wasScreenOn = true;
}
}
}
The activity display after when action screen on is call.
myActivity.class
disableKeyguard using KeyguardManager and start service. In service register the Receiver. Now when user off the device then call ACTION_SCREEN_OFF and when tap home button then call ACTION_SCREEN_ON but the problem is occur when i press home buttom many times.
add one line in manifest in receiver tag
receiver android:name=".MyBoardCast" android:enabled="true"