Advertisement

header ads

Android Studio Tutorials - Runtime Application permissions in Android version 6.0



In the last blog post I talked about how to check wifi network details without connecting to any wifi network. If you haven't read it yet here is it. How to get wifi info of nearby networks without connecting.



But I told you there, that you deploy that program to an Android Device that run on Android version 6.0 marshmallow. If you deploy that program to an android device runs on marshmallow. The app will probably crash without any errors.

That's because of a permission issue. Android devices which runs on Android version 6 or versions after that requires runtime permissions rather that permissions before running the applications.

So you can create the same application in the last blog post as below.

You can modify the code of the MainActivity.java as below


package org.techmagister.wifichecker;


import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    WifiManager wifi;
    WifiScanReceiver wifiReciever;
    TextView info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        info = (TextView)findViewById(R.id.Info);
        wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
        wifiReciever = new WifiScanReceiver();

        getPermission();
    }

    protected void onPause() {
        unregisterReceiver(wifiReciever);
        super.onPause();
    }

    protected void onResume() {
        registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        super.onResume();
    }
    private void getPermission() {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION , Manifest.permission.ACCESS_FINE_LOCATION},0x12345);
        }else{
            wifi.startScan();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == 0x12345) {
            for (int grantResult : grantResults) {
                if (grantResult != PackageManager.PERMISSION_GRANTED) {
                    wifi .startScan() ;
                    return;
                }
            }
            getPermission();
        }
    }

    private class WifiScanReceiver extends BroadcastReceiver {
        public void onReceive(Context c, Intent intent) {

            ScanResult result = wifi.getScanResults().get(0);
            String ssid = result.SSID;
            String bssid = result.BSSID;
            int rssi = result.level;
            String rssiString = String.valueOf(rssi);

            info.setText("Network Id: " + ssid + "\nMac Address: " + bssid + "\nSignal Strength: " + rssiString);

        }
    }

}


Run this code deploy the application into your android marshmallow device and Test it yourself. If you have any questions regarding this post. feel free to ask them in comment section down below. 

Post a Comment

3 Comments

  1. Our Dot Net Training in Chennai does not just deliver theoretical knowledge but also offers world class lab facilities...Learn More Details.......... Android Training in Chennai
    Selenium Training in Chennai

    ReplyDelete
  2. An Android WiFi switch makes all of this superfluous, as the widget or application will do the work for you.visit this site

    ReplyDelete

  3. Amazing & Great informative blog,it gives very useful practical information to developer like me. Besides that Wisen has established as Best Java Online Training from India . or learn thru Online Training mode Hibernate Online Training . Nowadays Hibernate ORM has tons of job opportunities on various vertical industry. or learn thru JavaScript Online Training from India. Nowadays even JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete