Thomas k
Thomas k

Reputation: 129

How should I add requests for the permission in marshmallow?

I am trying to create an One time password access android application. I have created a local host server using wamp and I am trying to access that thing with the mobile application I have designed. Unfortunately, I am getting the error as following in my log cat:

03-07 10:42:35.097 31217-31217/com.example.user.myapplication V/ActivityThread: updateVisibility : ActivityRecord{1ecab2fb token=android.os.BinderProxy@23c38b5b {com.example.user.myapplication/com.example.user.myapplication.activity.SmsActivity}} show : true
03-07 10:42:36.607 31217-31217/com.example.user.myapplication I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@23c38b5b time:52633449
03-07 10:42:37.397 31217-31217/com.example.user.myapplication D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
03-07 10:42:37.537 31217-32544/com.example.user.myapplication E/SmsActivity: Posting params: {[email protected], name=Thomas, mobile=9002378900}
03-07 10:42:37.537 31217-32544/com.example.user.myapplication I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
03-07 10:42:37.537 31217-32544/com.example.user.myapplication I/System.out: KnoxVpnUidStorageknoxVpnSupported API value returned is false
03-07 10:42:37.547 31217-31217/com.example.user.myapplication E/SmsActivity: Error: java.net.SocketException: socket failed: EACCES (Permission denied)
03-07 10:42:37.617 31217-31217/com.example.user.myapplication D/SRIB_DCS: log_dcs ThreadedRenderer::initialize entered! 

I have included all of the permissions, here's my AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.user.myapplication">
<application
    android:name=".app.MyApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:supportsRtl="true"
    android:theme="@style/MyMaterialTheme">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />

    <activity
        android:name=".activity.SmsActivity"
        android:label="@string/title_activity_sms">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".activity.MainActivity"
        android:label="@string/app_name"
        android:windowSoftInputMode="adjustResize">

    </activity>

    <!-- SMS Receiver -->
    <receiver android:name=".receiver.SmsReceiver">
        <intent-filter android:priority="99999">
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>


    <!-- Intent service -->
    <service
        android:name=".service.HttpService"
        android:exported="false" />

</application>

Here's my permission.java file, which was written for raising the request after above error:

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Permission extends AppCompatActivity implements View.OnClickListener{

    private Context context;
    private Activity activity;
    private static final int PERMISSION_REQUEST_CODE = 1;
    private View view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.request_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        context = getApplicationContext();
        activity = this;
        Button check_permission = (Button)findViewById(R.id.check_permission);
        Button request_permission = (Button)findViewById(R.id.request_permission);
        check_permission.setOnClickListener(this);
        request_permission.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {

        view = v;

        int id = v.getId();
        switch (id){
            case R.id.check_permission:
                if (checkPermission()) {

                    Snackbar.make(view,"Permission already granted.",Snackbar.LENGTH_LONG).show();

                } else {

                    Snackbar.make(view,"Please request permission.",Snackbar.LENGTH_LONG).show();
                }
                break;
            case R.id.request_permission:
                if (!checkPermission()) {

                    requestPermission();

                } else {

                    Snackbar.make(view,"Permission already granted.",Snackbar.LENGTH_LONG).show();

                }
                break;
        }
    }

    private boolean checkPermission(){
        int result = ContextCompat.checkSelfPermission(context, Manifest.permission.RECEIVE_SMS);
        if (result == PackageManager.PERMISSION_GRANTED){

            return true;

        } else {

            return false;

        }
    }

    private void requestPermission(){

        if (ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.RECEIVE_SMS)){

            Toast.makeText(context,"SMS permission allows us to receive sms. Please allow in App Settings for additional functionality.",Toast.LENGTH_LONG).show();

        } else {

            ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.RECEIVE_SMS},PERMISSION_REQUEST_CODE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    Snackbar.make(view,"Permission Granted, Now you can access SMS.",Snackbar.LENGTH_LONG).show();

                } else {

                    Snackbar.make(view,"Permission Denied, You cannot access SMS.",Snackbar.LENGTH_LONG).show();

                }
                break;
        }

} }

When I run the following code, i get no error in log cat and app stops with PACKAGE INSTALLER HAS STOPPED..I am bit confused at the moment and I understand I am stuck with something silly. I would be glad if anyone could help me with the issue.

Upvotes: 2

Views: 484

Answers (1)

opt05
opt05

Reputation: 852

Basically the only permission you need to ask for is the SMS permission (Internet is not a dangerous permissions and you don't have to request it from the user). Google recommends you request for permissions before you are going to use them and not all at once (like when launching an app for the first time). Google has also provided great detail on how best to implement and when in their developer documentation that would be hard to sum up here. Take a look at the developer documents here for code details, and the Material Design documents on how best to implement based on your app's situation.

Let me know if you need more details or any other questions about permissions.

Upvotes: 1

Related Questions