我尝试使用intent创建Caller应用程序,在android studio中ide代码是无错误的,但当它在实时设备上运行时&在EditText&呼叫按钮被按下,应用程序突然退出。。我找不到是什么导致了这个应用程序的行为,也找不到为什么没有打电话。。我该如何让它工作?
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)
所以我终于修复了它……多亏了"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);
}
这请求电话的运行时权限&当被授予时,它将直接拨打电话。
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.