Search code examples
androidandroid-intentandroid-permissionsphone-call

How to resolve ActivityNotFoundException in my code when using implicit intents?


MainActivity.java

public class MainActivity extends AppCompatActivity {


    EditText edit;
    Button btn;
    boolean b = false;

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

        edit = (EditText) findViewById(R.id.phone);
        btn = (Button) findViewById(R.id.call);

        permissionRequest();


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (b == false)
                {

                    permissionRequest();
                }

                else
                    {

                        if (edit.getText().toString().equals(" "))
                    {
                        Toast.makeText(MainActivity.this, "Enter the number first", Toast.LENGTH_LONG).show();
                    }
                    else
                        {
                        Intent i = new Intent(Intent.ACTION_CALL);
                        i.setData(Uri.parse(edit.getText().toString()));
                        startActivity(i);   //This line is coming in red
                    }
                }
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    b = true;
                } else {

                    b = false;
                }
        }


    }

    void permissionRequest() {

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 1);
        }
    }
}

Manifest file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ankit.dialer">
<uses-permission android:name="android.permission.CALL_PHONE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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>

</manifest>

Here I am creating an app to make a call. The problem is that when I start the app then it asks for permission but after I provide the permission the on click of button it shows ActivityNotFoundException saying that "No Activity found to handle Intent". How to resolve this.

Also my startActivity(i) is underlined as red saying "call requires permission which may be rejected by user" but program is compiling and executig well but when program is crashing on button click then in android monitor it is showing that exception caused by this line.


Solution

  • Change your code in the following manner

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) 
        {
            Intent i = new Intent(Intent.ACTION_CALL);
            i.setData(Uri.parse("tel:" + edit.getText().toString()));
            startActivity(i);   //This line is coming in red          
    
            if (ActivityCompat.checkSelfPermission(this, 
            Manifest.permission.CALL_PHONE) != 
            PackageManager.PERMISSION_GRANTED) 
            {
    
                /**********************
                 * 
                 *     WRITE UR CODE INSIDE THIS BLOCK TO HANDLE SITUATION WHEN
                     THE APP IS NOT GRANTED " ACTION_CALL" PERMISSION
                 * /
    
                return;
            }
        }