我正在列出手机中的所有联系人,并直接从我的Android应用程序中调用viber开启viber应用程序。我在网上找到了一些例子,但是所有年龄都太大,可能是几年之前没有一个对我有用
Uri uri = getUriFromPhoneNumber(phoneNumber, context);
if (uri != null) {
Intent localIntent4 = new Intent("com.viber.voip.action.CALL");
localIntent4.setType("vnd.android.cursor.item/vnd.com.viber.voip.call");
localIntent4.setData(Uri.parse("tel:" + uri));
startActivity(localIntent4);
}else {
Toast.makeText(context, "Number is not in Viber Contacts List", Toast.LENGTH_LONG).show();
}
我在androidmanifest.xml文件中包含了read_contacts权限。 但是这总是引发安全例外。我建议的一个解决方案是包括
activity android:name="com.ujjwalmainali.univhub.TestActivity"
android:launchMode="singleInstance"
android:exported="true"
android:screenOrientation="portrait">
机器人:导出="真"但这对我没有用。这里有什么问题。我需要什么样的许可才能打电话。
这是异常的logcat
java.lang.SecurityException: Permission Denial: starting Intent { act=com.viber.voip.action.CALL dat=tel:xxxxxxxxxxxxx.xxxxxxx.xxxxxxxxxxxxxxxxx cmp=com.viber.voip/.phone.PhoneFragmentActivity } from ProcessRecord{271db4b9 3911:com.ujjwalmainali.univhub/u0a219} (pid=3911, uid=10219) not exported from uid 10204
at android.os.Parcel.readException(Parcel.java:1549)
at android.os.Parcel.readException(Parcel.java:1502)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2463)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1619)
at android.app.Activity.startActivityForResult(Activity.java:3761)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:77)
at android.app.Activity.startActivityForResult(Activity.java:3722)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at android.app.Activity.startActivity(Activity.java:4032)
at android.app.Activity.startActivity(Activity.java:4000)
at com.ujjwalmainali.univhub.TestActivity.callToViberContact(TestActivity.java:57)
at com.ujjwalmainali.univhub.TestActivity$1.onClick(TestActivity.java:33)
at android.view.View.performClick(View.java:4785)
at android.view.View$PerformClick.run(View.java:19869)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
答案 0 :(得分:1)
将此标记添加到您的清单中:
<uses-permission android:name="android.permission.CALL_PHONE" />
和runTimePermission:
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activityCompat, new String[]{Manifest.permission.CALL_PHONE}, 123);
return;
}
context.startActivity(localIntent4);
。 。 。
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
for (int grantResult : grantResults) {
if (grantResult == PackageManager.PERMISSION_DENIED) {
return;
}
}
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(localIntent4 );
}
localIntent4是您正在使用的意图。
编辑:
您还需要将此标记设置为您的意图
localIntent4.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);