2016-10-19 13 views
3

Androidから電話をかけようとしていますが、実行時のアクセス許可も設定しています。電話をかけるかどうかを尋ねます。Android版Marshmallow以降のAndroidから電話をかけるにはどうすればよいですか?

これは私がそれを実装する方法です:

private static final int REQUEST_PHONE_CALL = 1; 
    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "+918511812660")); 

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { 
      ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE},REQUEST_PHONE_CALL); 
     } 
     else 
     { 
      startActivity(intent); 
     } 
    } 
    else 
    { 
     startActivity(intent); 
    } 


@Override 
public void onRequestPermissionsResult(int requestCode, 
             String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case REQUEST_PHONE_CALL: { 
      if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       startActivity(intent); 
      } 
      else 
      { 

      } 
      return; 
     } 
    } 
} 

これは私がlogcatで取得するものである:

java.lang.RuntimeException: Failure delivering result ResultInfo{[email protected]:requestPermissions:, request=1, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.devpost.airway/com.devpost.airway.activities.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.toString()' on a null object reference 
                    at android.app.ActivityThread.deliverResults(ActivityThread.java:3733) 
                    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3776) 
                    at android.app.ActivityThread.-wrap16(ActivityThread.java) 
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412) 
                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                    at android.os.Looper.loop(Looper.java:148) 
                    at android.app.ActivityThread.main(ActivityThread.java:5461) 
                    at java.lang.reflect.Method.invoke(Native Method) 
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.toString()' on a null object reference 
                    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1485) 
                    at android.app.Activity.startActivityForResult(Activity.java:3930) 
                    at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48) 
                    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75) 
                    at android.app.Activity.startActivityForResult(Activity.java:3890) 
                    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:856) 
                    at android.app.Activity.startActivity(Activity.java:4213) 
                    at android.app.Activity.startActivity(Activity.java:4181) 
                    at com.devpost.airway.activities.MainActivity.onRequestPermissionsResult(MainActivity.java:140) 
                    at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:6582) 
                    at android.app.Activity.dispatchActivityResult(Activity.java:6460) 
                    at android.app.ActivityThread.deliverResults(ActivityThread.java:3729) 
                    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3776)  
                    at android.app.ActivityThread.-wrap16(ActivityThread.java)  
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)  
                    at android.os.Handler.dispatchMessage(Handler.java:102)  
                    at android.os.Looper.loop(Looper.java:148)  
                    at android.app.ActivityThread.main(ActivityThread.java:5461)  
                    at java.lang.reflect.Method.invoke(Native Method)  
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  
しかし、私は、アプリのクラッシュを許可押すと、

これはおそらく原因ですか?

+0

onRequestPermissionsResultで作成する必要がありますか? – vidulaJ

+0

これはあなたの答えを確認することができます。 http://stackoverflow.com/a/39646644/6812027 –

答えて

8

にコードの下に書く

public static boolean hasPermissions(Context context, String... permissions) { 
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) { 
    for (String permission : permissions) { 
     if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) { 
      return false; 
     } 
    } 
} 
return true; 
} 

新しいメソッドを追加OKですが、startActivityへの電話はonRequestPermissionsResult()からクラッシュしています。 startActivityに正しく設定されているIntentが正しく設定されていますか?私はそれがコードのその部分に設定されているのを見ることはできません。

ということにも注意してくださいContextCompat.checkSelfPermissionがあなたに代わってチェックSDKのバージョンを扱うので、あなたは、ラッピングSDKバージョンチェックコードなしで、それ自体で

if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { 
    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE},REQUEST_PHONE_CALL); 
} 
else 
{ 
    startActivity(intent); 
} 

を使用することができるはずです。

+1

ありがとうございます、意図は意図が適切に設定されていないために発生しました、私はそれが問題を解決するのを助けたとして答えをupvotingです。 – OBX

+0

Hy @OBXインテントが正しく設定されていないセクションを説明できますか?私はいくつかの問題があります。ありがとうございました – fanjavaid

0

スタックトレースがあなたの権限の流れが動作していることを示していると思われるグローバル

int PERMISSION_ALL = 1; 
String[] PERMISSIONS = {Manifest.permission.READ_CONTACTS, Manifest.permission.CALL_PHONE}; 

でこれを追加してのonCreate

if(!hasPermissions(this, PERMISSIONS)){ 
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL); 
} 
0

onRequestPermissionsResultを下に変更するには、基本的に最初にintentを作成してから許可を得て呼び出す必要があります。それはあなたが間違っているところです。

@Override 
public void onRequestPermissionsResult(int requestCode, 
             String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case REQUEST_PHONE_CALL : { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "+918511812660")); 

       if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) { 
        startActivity(intent); 
       } 
      } 
     } 
    } 
} 
3

はあなたのIntent活動や断片インサイド


@Override 
public void onRequestPermissionsResult(int requestCode, 
             String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case REQUEST_PHONE_CALL: { 
      if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "+918511812660")); 
       startActivity(intent); 
      } 
      else 
      { 

      } 
      return; 
     } 
    } 
}
関連する問題