Java Android应用程序停止拨打电话
这里是主要活动Java Android应用程序停止拨打电话,java,android,android-activity,Java,Android,Android Activity,这里是主要活动 import android.content.Intent; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Te
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText number;
Button makecall;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number = findViewById(R.id.number);
makecall = findViewById(R.id.makecall);
call();
}
private void call() {
makecall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent;
intent = new Intent(Intent.ACTION_DIAL);
intent = intent.setData(Uri.parse("number:"+number.getText().toString()));
startActivity(intent);
}
});
}
}
我已将此权限请求添加到清单文件
<uses-permission android:name="android.permission.CALL_PHONE"/>
这是我的XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.bimstajyer1.calisma10.MainActivity">
<EditText
android:id="@+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Number"
android:inputType="number"
android:maxLength="11" />
<Button
android:id="@+id/makecall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Make a call" />
</LinearLayout>
当我按下呼叫按钮打电话时,我发现这个错误
致命异常:主
流程:com.example.bimstajyer1.calisma10,PID:25198
android.content.ActivityNotFoundException:未找到要处理的活动
Intent{act=android.Intent.action.DIAL dat=number:}
错误行为:startActivity(intent) 请调查一下
private void call() {
makecall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent;
intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"+number.getText().toString()));
startActivity(intent);
}
});
}
}));
}
现在跳过所有崩溃 你为什么打电话;方法,在onCreate()中设置click listener in按钮,验证您的号码,然后调用call()方法。
private void call() {
makecall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PackageManager pm = getPackageManager();
if (pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
if (!number.contains("tel:"))
number = "tel:" + number;
Uri link = Uri.parse(number);
Intent callIntent = new Intent(Intent.ACTION_DIAL, link);
startActivity(callIntent);
}else{
Toast.makeText(this,"No call facility available in device!",Toast.LENGTH_SHORT).show();
}