简体   繁体   中英

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.

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;
        }
    }

This happens when you test on Custom Built ROMs for Android. This is the most common problem faced on Cyanogenmod and Lineage OS.

String contact_number="123456789";
Intent callIntent = new Intent(Intent.ACTION_CALL);
intent.setPackage("com.android.phone");
callIntent.setData(Uri.parse("tel:" + contact_number));
startActivity(callIntent);

Give the user an option to select the telephony app, there might be some different telephony app on the device which you are testing, like hangouts , truecaller or any other.

The above code states the package name, where you can define your phone app's package name by seeing it in your phone's application manager.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM