App尝试拨打给定号码时突然退出

huangapple go评论73阅读模式
英文:

App Suddenly Exits When Tried to Call given number

问题

抱歉,代码部分无法直接翻译成文本,因为它包含了程序代码和特定的标记符号。如果您有任何关于这段代码的问题或需要帮助解决问题,请随时提问。我会尽力提供帮助。

英文:

I Tried to Create Caller app using intent, in android studio ide code is error free but when it is run on live device & put in the number in EditText & Call button is pressed app suddenly exits.. im unable to find what causes this app behaviour and why call is not getting placed..How do i make it work ?

MainActivity.java

    public class MainActivity extends AppCompatActivity {
 EditText txt_phn;
 Button btn_call;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txt_phn=(EditText)findViewById(R.id.txt_phn);
        btn_call=(Button)findViewById(R.id.btn_call);
        btn_call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                mck();
            }
        });


    }
    private void mck()
    {


        
        String number = "tel:" + txt_phn.getText().toString().trim();

        Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(number));

        try {
            startActivity(callIntent);
            finish();
            Log.i("Finished making a call", "");
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(this, "Call faild, please try again later.", Toast.LENGTH_SHORT).show();
        }


    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
   
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}
Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.caller">
    <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>
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/txt_phn"
        android:layout_width="379dp"
        android:layout_height="70dp"
        android:layout_marginTop="148dp"
        android:ems="10"
        android:hint="Please Enter Phone Number To Call"
        android:inputType="text"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="16dp" />

    <Button
        android:id="@+id/btn_call"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="360dp"
        android:text="Call"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
2020-08-10 20:04:47.337 23806-23806/com.example.caller E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.caller, PID: 23806
    java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxxxxxxxxx cmp=com.android.server.telecom/.components.UserCallActivity } from ProcessRecord{70113a3 23806:com.example.caller/u0a425} (pid=23806, uid=10425) with revoked permission android.permission.CALL_PHONE
        at android.os.Parcel.createException(Parcel.java:2071)
        at android.os.Parcel.readException(Parcel.java:2039)
        at android.os.Parcel.readException(Parcel.java:1987)
        at android.app.IActivityTaskManager$Stub$Proxy.startActivity(IActivityTaskManager.java:3851)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1714)
        at android.app.Activity.startActivityForResult(Activity.java:5210)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)
        at android.app.Activity.startActivityForResult(Activity.java:5168)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663)
        at android.app.Activity.startActivity(Activity.java:5539)
        at android.app.Activity.startActivity(Activity.java:5507)
        at com.example.caller.MainActivity.mck(MainActivity.java:49)
        at com.example.caller.MainActivity.access$000(MainActivity.java:19)
        at com.example.caller.MainActivity$1.onClick(MainActivity.java:33)
        at android.view.View.performClick(View.java:7260)
        at android.view.View.performClickInternal(View.java:7237)
        at android.view.View.access$3600(View.java:802)
        at android.view.View$PerformClick.run(View.java:27915)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:491)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:915)
     Caused by: android.os.RemoteException: Remote stack trace:
        at com.android.server.wm.ActivityStackSupervisor.checkStartAnyActivityPermission(ActivityStackSupervisor.java:1058)
        at com.android.server.wm.ActivityStarter.startActivity(ActivityStarter.java:762)
        at com.android.server.wm.ActivityStarter.startActivity(ActivityStarter.java:585)
        at com.android.server.wm.ActivityStarter.startActivityMayWait(ActivityStarter.java:1298)
        at com.android.server.wm.ActivityStarter.execute(ActivityStarter.java:516)

答案1

得分: 0

所以最终我找到了解决方法...感谢"David Wasser",因为一开始我没有看logcat,通过他的建议我查看了logcat并找到了运行时调用权限的错误,这是我没有在代码中添加的,同时也要感谢"vikas kumar"提供的提示。
现在是更改的部分。以下是我在MainActivity中所做的更改:-

if (ContextCompat.checkSelfPermission(getApplicationContext(), CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
    startActivity(callIntent);
} else {
    requestPermissions(new String[]{CALL_PHONE}, 1);
}

这会要求运行时权限以进行电话操作,当权限被授予时,它将直接拨打电话。

为什么需要这些更改
因为从Android 6.0以上版本开始有API更改和权限相关的更改
我遵循了旧版本的调用应用程序教程示例
英文:

So finally i got the fix for it..thanks to "David Wasser" as i didnt looked at logcat initially. through his suggetion i looked at logcat & found an error of runtime call permission which i didn't added in code , and this hint also given by "vikas kumar" thanks to him..
now the changes. following are the changes i made in mainactivity : -

  if (ContextCompat.checkSelfPermission(getApplicationContext(), CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
            startActivity(callIntent);
        } else {
            requestPermissions(new String[]{CALL_PHONE}, 1);
        }

this asks for runtime permission for telephony & when granted it will directly place a call.

Why This changes needed ?
Because from Android 6.0 Above there were api changes & also permission related changes.
& i followed older example of calling app tutorial.

huangapple
  • 本文由 发表于 2020年8月7日 21:11:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63302573.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定