1

I am new in the android studio. I am working on my college project and i made this sample app to make a call because i want this function in my app. but there seems to be a problem. It says me to ask the permission to call from the user first and i m unknown how to do it. can anybody help me on this ?

This is the java code. MainActivity.java

package example.call;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button button;

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

        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("197"));
                startActivity(callIntent);
            }
        });
    }
}

This is what I get in the error:

enter image description here

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • Look at this https://developer.android.com/training/permissions/requesting.html and this http://stackoverflow.com/questions/33666071/android-marshmallow-request-permission?answertab=active#tab-top. Android 6.0 requires run time permissions – ᴛʜᴇᴘᴀᴛᴇʟ Nov 29 '16 at 16:40
  • Simple way : Just hover on that error and on the left side of the line, android studio would suggest you a way to handle that error. – CopsOnRoad Nov 29 '16 at 16:46

3 Answers3

1

Make sure that you have given below permission in Android Manifest file.

Also check out this https://stackoverflow.com/questions/7882955/android-permission-call-phone-for-tablet for more information.

Community
  • 1
  • 1
Srushti
  • 131
  • 7
0
     Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:197"));
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    startActivity(callIntent);

Add this permission check(handled from studio itself). Also add "tel:" before the phone number. Otherwise it may result in an error I guess

0

Add the following line in the permissions in your AndroidManifest file inside your app.

<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>

In Android you need permission from the user before being able to do such things as calling, accessing Internet, accessing the contacts...

gcorso
  • 154
  • 8