I want call the function startActivity in two ways:
First (It works):
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);
}
开发者_StackOverflow社区return true;
}
Second: in 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;
}
in 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);
}
}
The second way doesn't work - I have a error in aplication when I clicked on position in menu. I know function "call" in IntentsUtils work because analise logs in "LogCat"
This is my logs from LogCat: android.permission.CALL_PHONE
android.permission.CALL_PHONE> 10-27 16:10:56.702:
WARN/ActivityManager(52): Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:5555555555 flg=0x10000000 cmp=com.android.phone/.OutgoingCallBroadcaster } from ProcessRecord{43dbf4b8 363:com.androidbook/10025} (pid=363, uid=10025) requires android.permission.CALL_PHONE
10-27 16:10:56.722: WARN/dalvikvm(363): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
10-27 16:10:56.722: ERROR/AndroidRuntime(363): Uncaught handler: thread main exiting due to uncaught exception
10-27 16:10:56.752: ERROR/AndroidRuntime(363): java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:5555555555 flg=0x10000000 cmp=com.android.phone/.OutgoingCallBroadcaster } from ProcessRecord{43dbf4b8 363:com.androidbook/10025} (pid=363, uid=10025) requires android.permission.CALL_PHONEandroid.permission.CALL_PHONE
Thanks in advance, Michael
The problem is your action.
ACTION_CALL is restricted to specific apps (I think system apps only). The reason you get your logcat message is because you log it before you try to start the activity. A permission error is going to occur because the system will not allow your app to directly call a phone number. You can dial it, but the user has to press call.
This is a security issue, if they let any application start making phone calls to numbers.
From the Android SDK Documetation:
Note: there will be restrictions on which applications can initiate a call; most applications should use the ACTION_DIAL.
edit
after looking at your last edit, with the logcat. I can say 100% that your issue is with using ACTION_CALL.
Long story short, you do not have permission to use ACTION_CALL, you have to use ACTION_DIAL.
Try setting the FLAG_ACTIVITY_NEW_TASK as given in
http://developer.android.com/reference/android/content/Context.html#startActivity(android.content.Intent)
you may have to set like this:
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
I havent tried this. But worth to give a try
精彩评论