Ask user to turn on Wi-Fi - android

I have an app which almost always needs to know the user location.
When I need to access a location, I do this:
final AlertDialog.Builder builder = new AlertDialog.Builder(
MapScreen.this);
builder.setTitle("MyAppName");
builder.setMessage("The location service is off. Do you want to turn it on?");
builder.setPositiveButton("Enable location",
new DialogInterface.OnClickListener() {
#Override
public void onClick(
final DialogInterface dialogInterface,
final int i) {
startActivity(new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
});
builder.setNegativeButton("Continue without location", null);
builder.create().show();
However, the GPS gives me some info that isn't always precise enough. Wi-Fi always gives me enough precission, so I want to ask the user to turn on the Wi-Fi the same way I ask them to turn on the location. I do not want to just turn it on, I want the user to be notified about it, and to manually enable it.
Is there any Intent to bring the WiFi menu to the user?

Following intent shows the wireless settings such as Wi-Fi, Bluetooth and Mobile networks:
startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
For the complete list of Settings: https://developer.android.com/reference/android/provider/Settings.html
For the documentation on the startActivity method:
https://developer.android.com/reference/android/app/Activity.html#startActivity(android.content.Intent)
(keep in mind startActivity is just throw and forget, if you want to capture the response of what the user did out there you could instead call startActivityForResult, probably not needed in this case)

You can try
Intent i = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
startActivity(i);

The previous answers were quotting the "Settings.ACTION_WIRELESS_SETTINGS", but it is too general, as it is going to open the settings of the general wireless connection while the question is about wifi only.
I'm using "Settings.ACTION_WIFI_SETTINGS" as follows
Intent turnWifiOn = new Intent(Settings.ACTION_WIFI_SETTINGS);
startActivity(turnWifiOn);
Note: It's not going to open just a dialog, but an activity instead.

Related

Background service getting killed on removing the app from task manager on ColorOS

I am working on an app where one of the service needs to be running always to perform some specific operation in the background. So i am restarting the service using a Broadcast Receiver, whenever it is being killed from the task manager. So for this i am taking Autostart/Battery Optimization permission from the user to get it restarted again.
This approach is working completely fine with almost every leading device manufacturers except on ColorOS and as long as the "Autostart/Battery Optimization permission" is turned on for my app, it is working completely fine on every other devices except that on ColorOS.
The reason for this being, i am not able to redirect user to the "AutoStart" or "Battery Optimization" settings page
I have tried to open the Autostart settings Activity from my app using this code:
Intent autostartIntent = new Intent();
autostartIntent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity"));
startActivity(autostartIntent);
Also i have tried to play with the power saving settings manually to check if in any case that is working. But nothing seems to be working anyways.
I would be looking a way to redirect user to the Autostart permission page or to the battery optimization settings page. Anyone who dealt with the similar kind of problem can suggest some solution or even workarounds for the same.
Got it working!!
I have redirected user to the app details page and there he/she need to turn on Auto Startup option. This will keep the service running on ColorOS. Below is the code to redirect user to the app details page and here the user need to turn the Auto Startup on.
if ("oppo".equalsIgnoreCase(Build.MANUFACTURER)) {
AlertDialog.Builder builder = new AlertDialog.Builder(PermissionsActivity.this);
builder.setTitle("Allow Auto-startup");
builder.setMessage("To access content on lock screen, enable ‘Allow Auto-startup’ for JiffyFeed");
builder.setPositiveButton("Allow",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.fromParts("package", getPackageName(), null));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
builder.setNegativeButton("Deny", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
// Do something
}
});
builder.show();
}
Additionally, i have used a workaround as well. I am using NotificationListenerService which keeps on running always, so i am restarting the service on receipt of a new notification, so every time a new notification comes up, it wakes up the service which i need to keep running always.

How to close an app after showing a dialog?

I have almost completed my app development and I have even allowed a way to show up a toast message if there is no internet connection. I need to know how to close an app after showing a dialog if there is no internet connection?
My app is a webview app. So when user turn on the app, the app must check for internet connection. and display a dialog box if no network and the app must close after showing dialog.
OR
I don't want users to see the "webpage not available" page, so if there is no internet connection, the app must close after showing a dialog box with one button 'close'. Or is there any way to redirect users to a text in directory which shows no internet connection?
I need those code only; all the other codes have been done and are working properly.
Just invoke finish() to destroy activity in the onClick() function of dialog. Like:
...
builder.setMessage('There is no connection!! Please close the activity!')
.setPositiveButton('close', new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
...
You can popup a dialog after checking the internet connectivity as follows
new AlertDialog.Builder(this)
.setTitle("Connection Error")
.setMessage("You are not connected to Internet.")
.setPositiveButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// finish the activity here or,
// redirect to another activity
}
})
.show();
You can check your internet connectivity before setting the view to the activity.
Like :
if(connnected){
setContentView(layoutId);
}else{
show a tosat with information no internet;
finish activity.
}
call finish() method on your activity once user clicks on dialogue

How can i detect the change in network state and reload my android app if the connectivity is available

I have an android app which runs when internet connection is active. When the connection is not available, a dialog box appears through which wifi can be activated. I have made the app to quit when connection is not available after initiating wifi connection. The question is, i have to reload the app and continue using the app when the internet connection is available after enabling the wifi rather than to exit the app.
Here is the code:
if (CheckConnection.checkInternetConnection(this)) {
//load a webview and show contents from web.
}
else
{
try {
AlertDialog alertDialog = new AlertDialog.Builder(Activity.this).create();
alertDialog.setTitle("Info");
alertDialog.setMessage("Internet Connection not available. Please cross check your internet connectivity and try again.");
alertDialog.setIcon(R.drawable.error);
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.setCancelable(false);
alertDialog.setButton2("WiFi Setting", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.android.settings", "com.android.settings.wifi.WifiSettings");
startActivity(intent);
System.exit(0);
}
});
alertDialog.show();
}
catch(Exception e)
{
}
Don't call System.exit(0).
First of all because it is not a recommended way of quitting your app and makes it look to the user as if it crashed.
Second, why are you exiting at all? The user will be navigated to the internet settings, switch them on and when they press back, your activity will already be there. You can recheck internet status onResume of your activity. This is the acceptable way of handling an unavailable internet connection in your application.

OnActivityResult called prematurely with Settings menu

In my Application, before going into the App, I have to check my wifi connection and take the user to the wifi settings if wifi is not enabled.
I dont want to use WifiManager.setWifiEnabled(); as I want to give the user the opportunity to set it.
I have referred the link, onActivityResult() called prematurely
and also, onActivityResult and the Settings Menu
But it is not working for me. The OnActivityResult() and onResume() is called almost at the same time I get into the Settings menu.
Here is my code,
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setMessage("You are not currently connected to any network. Please turn on the network connection to continue.")
alert.setPositiveButton("Settings", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int arg1)
{
Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
startActivityForResult(intent,SETTINGSCODE);
dialog.cancel();
}
});
alert.show();
In the onActivityResult(), I am checking the network state again which is called before changing the settings.
What can i do so that OnActivityResult()
will be called only after coming back from the settings menu?
Please help me with this issue..!!
This is how I solved this issue.
In reference to the answer posted for a similar question, I changed startActivityForResult() to startActivity() as the order of the action calls is like
startActivityForResult()
onActivityResult()
onCreate() (new activity)
setResult() or finish()
The control is taken to the settings page where the user can switch on/off the Wi-Fi and later come back to the app. :)

How to make an activity window stay always on top

I want to create an activity that stays always on top (like the modal windows or task manager in Windows) of the others activities.
How can I do this on Android? Thanks
You can use the following code in the overridden onStop method of your activity:
#Override
protected void onStop(){
super.onStop();
Intent intent = new Intent(this, ClassNameOfYourActivity.class);
startActivity(intent);
}
Beauty problem: your activity will pop-up again if any other activity trying to claim the focus. So it's not a modal window.
And it's dangerous! You wont be able to handle the Android GUI, you'll be able to control only your application GUI. For instance, switching debug mode on-off, killing application (only over ADB), reaching system settings, etc. will be impossible. If you switch off the ADB and combine it with the auto start mechanism then you'll be in trap.
So you won't be popular if you share it with Play :)
Depending on what exactly you are trying to do, you might be happy with windows that stay on top of other Activities.
Apps like Super Video client & Floating Notes are able to display floating windows above other activities. These questions might point you in the right direction:
Creating a system overlay window (always on top)
How to create always-top fullscreen overlay activity in Android
You can't. As this is defined in the Android system.
Follow the steps to achieve your requirement
Create an activity which is going to be the top activity
Add the following code in the manifest
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Use the following code to get overlay permission from user
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
new AlertDialog.Builder(this)
.setTitle("Permission Request")
.setMessage("This app needs your permission to overlay the system apps")
.setPositiveButton("Open settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
startActivityForResult(myIntent, 101);
}
})
.setNegativeButton(android.R.string.no, null)
.show();
}
}

Categories

Resources