-5

While making a call from the app, it is not working. I have added the permission in android manifest, but it is still not working. Please see my code and help me soon.

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:999999999"));

        if (ActivityCompat.checkSelfPermission(menu.this,
                Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            return;
        }

        startActivity(callIntent);
    }
});
Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
  • Welcome to Stack Overflow! To give you a great answer, it might help us if you have a glance at [ask] if you haven't already. It might be also useful if you could provide a [mcve]. – Mat Jan 27 '17 at 12:38
  • refer http://stackoverflow.com/questions/1556987/how-to-make-a-phone-call-in-android-and-come-back-to-my-activity-when-the-call-i for different approach – Manohar Jan 27 '17 at 12:43
  • Check whether your call permissions tag is before application tag in manifest file – Karthik CP Jan 27 '17 at 13:12

1 Answers1

0

This code should work:

    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "Your Phone_number"));
    startActivity(intent);

Permission in Manifest:

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

but i highly recommend using ACTION_DIAL instead. It opens up the dialer screen with the number entered instead. This gives the user more flexibility. Also you don't need to have the CALL_PHONE permission with this one.

Here is an update:

With CALL_PHONE permission

Main Class

public class MainActivity extends AppCompatActivity {

private final int CALL_PHONE = 1;
private Button dialBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    dialBtn = (Button) findViewById(R.id.dial_button);
    //In android 6 we need to ask for permissions:
    if (ActivityCompat.checkSelfPermission(getApplicationContext(),
            Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, CALL_PHONE);
        } else {
            Toast.makeText(getApplicationContext(), "We need permissions to dial.", Toast.LENGTH_LONG).show();
        }
    } else {
        setupView();
    }
}

private void setupView() {
    dialBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:999999999"));
            // We have to implement this part because ... yeah permissions in android.....
            if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getApplicationContext(), "We need permissions to dial.", Toast.LENGTH_LONG).show();
                return;
            }
            startActivity(callIntent);
        }
    });
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case CALL_PHONE:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                setupView();
            } else {
                Toast.makeText(getApplicationContext(), "We need permissions to dial.", Toast.LENGTH_LONG).show();
            }
            break;
    }
}

My manifest:

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

Without the permission check and only using ACTION_DIAL

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button dialBtn = (Button) findViewById(R.id.dial_button);
    dialBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent callIntent = new Intent(Intent.ACTION_DIAL);
            callIntent.setData(Uri.parse("tel:999999999"));
            startActivity(callIntent);
        }
    });
}
}

The last one is so much easier. Android permission checks are a pain in the ass.

I just made my own class. It's easier for the next time to also provide the error Stack trace. But for what I can see in your code is that your using menu.this I don't know for sure because u didn't provide enough information but I think this causes the error.

I hope I helped u out.

Bram Jongebloet
  • 306
  • 1
  • 2
  • 10