繁体   English   中英

使用隐式意图时,如何解决我代码中的ActivityNotFoundException?

[英]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 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>

在这里,我正在创建一个应用程序来拨打电话。 问题是,当我启动应用程序时,它会请求权限,但是在我提供权限后,单击按钮时它会显示ActivityNotFoundException说“找不到用于处理Intent的活动”。 如何解决这个问题。

另外,我的startActivity(i)下划线为红色,表示“呼叫需要权限,可能会被用户拒绝”,但是程序正在编译并且执行良好,但是当程序在按钮上单击时崩溃,然后在android显示器中显示这条线引起的异常。

通过以下方式更改代码

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

当您在Android的“自定义内置ROM”上进行测试时,就会发生这种情况。 这是Cyanogenmod和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);

给用户一个选择电话应用程序的选项,您正在测试的设备上可能有一些不同的电话应用程序,例如环聊truecaller或其他任何电话应用程序。

上面的代码说明了程序包名称,您可以在其中通过在电话的应用程序管理器中看到它来定义电话应用程序的程序包名称。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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