Alt*_*win 2 android android-activity
我想以两种方式调用函数startActivity:
首先(它的工作原理):
public class HelloWorld extends Activity
{
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == 1){
startActivity(new Intent(Intent.ACTION_DIAL,Uri.parse("tel:660718109")));
}
else {
return super.onOptionsItemSelected(item);
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
第二: 在HelloActivity.java中
public class HelloWorld extends Activity {
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == 1){
IntentsUtils.tryOneOfThese(this);
}
else {
return super.onOptionsItemSelected(item);
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
在IntentsUtils.java中
public class IntentsUtils
{
public static void tryOneOfThese(Activity activity)
{
IntentsUtils.call(activity);
}
public static void call(Activity activity)
{
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:5555555555"));
Log.v("MyLogs", "It's works!");
activity.startActivity(intent);
}
}
Run Code Online (Sandbox Code Playgroud)
第二种方法不起作用 - 当我点击菜单中的位置时,我在应用程序中出错.我知道IntentsUtils中的函数"call"有效,因为分析日志记录在"LogCat"中
这是来自LogCat的日志:android.permission.CALL_PHONE
android.permission.CALL_PHONE> 10-27 16:10:56.702:
WARN/ActivityManager(52):权限拒绝:从ProcessRecord {43dbf4b8 363:com.androidbook启动Intent {act = android.intent.action.CALL dat = tel:5555555555 flg = 0x10000000 cmp = com.android.phone/.OutgoingCallBroadcaster}/10025}(pid = 363,uid = 10025)需要android.permission.CALL_PHONE
10-27 16:10:56.722:WARN/dalvikvm(363):threadid = 3:线程退出,未捕获异常(group = 0x4001b188)
10-27 16:10:56.722:ERROR/AndroidRuntime(363):未捕获的处理程序:由于未捕获的异常导致线程主要退出
10-27 16:10:56.752:ERROR/AndroidRuntime(363):java.lang.SecurityException:权限拒绝:启动Intent {act = android.intent.action.CALL dat = tel:5555555555 flg = 0x10000000 cmp = com.android ProcessRecord中的.phone/.OutgoingCallBroadcaster} {43dbf4b8 363:com.androidbook/10025}(pid = 363,uid = 10025)需要android.permission.CALL_PHONEandroid.permission.CALL_PHONE
迈克尔,提前谢谢
问题是你的行动.
ACTION_CALL仅限于特定应用程序(我认为仅限系统应用程序).获取logcat消息的原因是您在尝试启动活动之前记录它.由于系统不允许您的应用直接拨打电话号码,因此会发生权限错误.您可以拨打它,但用户必须按通话.
这是一个安全问题,如果他们让任何应用程序开始拨打电话号码.
来自Android SDK Documetation:
注意:对哪些应用程序可以发起呼叫会有限制; 大多数应用程序应使用ACTION_DIAL.
使用logcat查看上次编辑后编辑.我可以100%表示你的问题是使用ACTION_CALL.
长话短说,你没有使用ACTION_CALL的权限,你必须使用ACTION_DIAL.
| 归档时间: |
|
| 查看次数: |
4109 次 |
| 最近记录: |