0
The error appears when I click on the CALL PHONE button.
Mainactivity class
package com.edu.chamada;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.content.Intent;
import android.net.Uri;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity {
Button btnChamar;
Button btnTelefone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnChamar = (Button) findViewById(R.id.btnChamar);
btnTelefone = (Button) findViewById(R.id.btnTelefone);
btnChamar.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("http://www.google.com");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
finish();
}
});
btnTelefone.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("34621026");
Intent it = new Intent(Intent.ACTION_CALL);
iniciarTelefone(it);
}
});
}
public void iniciarTelefone(Intent it){
startActivity(it);
finish();
}
//Criar um menu da aplicação
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == R.id.acaoTelefonar){
Log.i("Saida do Menu", "Você escolheu: "+ id);
}
return super.onOptionsItemSelected(item);
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.edu.chamada.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnChamar"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="37dp"
android:layout_marginTop="21dp"
android:text="@string/btnChamar"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnTelefone"
android:layout_marginTop="24dp"
android:layout_alignLeft="@+id/btnChamar"
android:layout_below="@+id/btnChamar"
android:text="@string/btnTelefone"/>
</RelativeLayout>
ERROR
05-31 14:28:39.017 2103-2103/com.mantovani.projetotelefone E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mantovani.projetotelefone, PID: 2103
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.CALL }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1632)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
at android.app.Activity.startActivityForResult(Activity.java:3424)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.app.Activity.startActivityForResult(Activity.java:3385)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at android.app.Activity.startActivity(Activity.java:3627)
at android.app.Activity.startActivity(Activity.java:3595)
at com.mantovani.projetotelefone.MainActivity.iniciarTelefone(MainActivity.java:54)
at com.mantovani.projetotelefone.MainActivity$2.onClick(MainActivity.java:49)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
[ 05-31 14:28:39.027 1567: 1688 D/ ]
HostConnection::get() New Host Connection established 0xb8f012a0, tid 1688
what is the difference 'Intent.ACTION_DIAL' of 'Intent.ACTION_CALL'
– Vale
the error was only in the syntax where had to put the "tel:" worth.
– Vale
You weren’t passing Uri with your phone to Enter in your code (with the setData) or checking if the device has ways to make a call (for example, if it’s a tablet, your app will crash). ACTION_CALL makes the call directly without the user’s intervention (but requires that proper permissions are given) the ACTION_DIAL only shows the dialer, but with the number already set for the user to confirm and dial, and this does not require special permissions in the application.
– Márcio Oliveira