2017-12-11 21 views
0

私はXamarinでAndroidアプリを作っています。これはzxingを使用しています。Xamarinについてカメラの許可

ユーザがボタンをクリックすると、QrScanページとダイアログボックスが表示され、カメラの許可を求めるダイアログが表示されます。

ユーザーが許可するかどうかを確認するダイアログボックスを表示するには、クリックするたびに権限を許可してください。

ユーザーが[拒否]をクリックすると、アプリケーションを再起動する前に権限ダイアログが表示されなくなりました。

ご存知ですか?

これは私のソースです。

アンドロイド--- MainActivity.cs

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity 
{ 
    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 
     global::Xamarin.Forms.Forms.Init(this, bundle); 
     ZXing.Net.Mobile.Forms.Android.Platform.Init(); 
     LoadApplication(new App { OSVersion = "Android Version " + "2.0" }); 
    } 

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults) 
    { 
     // If this is not be, occur unexpected exception when user click deny 
     if(grantResults[0] == Permission.Denied) 
     { 
      return; 
     } 
     global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults); 
    } 
} 

そして、これがクリックされたボタンと呼ばれるPCLプロジェクトで私の実行QrScan方法です。

public async void ImgQrScan_Clicked(object sender, EventArgs e) 
    { 
     this.TappedEvent?.Invoke(sender, e); 
     CustomScanViewMaker(); 
     await Navigation.PushModalAsync(oCustomQrScanPage); 

     zxingPage.IsScanning = true; 
     string sScanResult = ""; 
     zxingPage.OnScanResult += (result) => 
     { 
      sScanResult = result.Text; 
      zxingPage.IsScanning = false; 
      Device.BeginInvokeOnMainThread(async() => 
      { 
       this.OnClicked?.Invoke(sender, new QrScannerClickEventArgs(sScanResult)); 

       await Navigation.PopModalAsync(); 
      }); 
     }; 

     this.OnClicked?.Invoke(sender, new QrScannerClickEventArgs(sScanResult)); 
    } 

ありがとうございます。

+0

あなたは、この問題を解決しましたか? –

答えて

0

ユーザーが許可するかどうかを確認するダイアログボックスを表示するには、クリックする度にボタンをクリックします。 documentが言ったように

あなた

は、この機能を実装するために shouldShowRequestPermissionRationaleメソッドを使用して試みることができる:

は、ユーザーが説明を必要とする可能性がある状況を見つけやすくするために、Androidはutiltity方法を提供し、shouldShowRequestPermissionRationale()。 このメソッドは、アプリケーションが以前にこの権限を要求していて、ユーザーがその要求を拒否した場合にtrueを返します。その使用方法については

、あなたは公式文書Requesting Permissions at Run Timeを参照してください可能性があり、C#で、それはこのようなものです:

// Here, this is the current activity 
if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.Camera) != Permission.Granted) 
{ 
    // Should we show an explanation? 
    if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.Camera)) 
    { 
     // Provide an additional rationale to the user if the permission was not granted 
     // and the user would benefit from additional context for the use of the permission. 
     // For example if the user has previously denied the permission. 

     // Show an explanation to the user *asynchronously* -- don't block 
     // this thread waiting for the user's response! After the user 
     // sees the explanation, try again to request the permission. 

     Log.Info(TAG, "Displaying camera permission rationale to provide additional context."); 
    } 
    else 
    { 
     // No explanation needed, we can request the permission. 
     ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.Camera }, REQUEST_CAMERA); 

     // REQUEST_CAMERA is an app-defined int constant. The callback method gets the 
     // result of the request. 
    } 
} 
else 
{ 
    System.Diagnostics.Debug.WriteLine("Permission Granted!!!"); 
} 
+0

返信ありがとうございました。私はすでにそれを試みたが、それは私の問題を解決するものではない。依頼の依頼ダイアログは、アプリを再起動することで一度だけ表示されます。 – user5949771

+0

@ user5949771あなたのプロジェクトでこれらのコードを使用しようとしましたか?コードを投稿してください。問題を特定するのに役立つコードを表示する必要があると思われます。 –

+0

はい、試しました。申し訳ありませんが、今は仕事で忙しいですが、アップデートするとすぐに12月にサンプルソースを作成する(会社のソースを使用できません)。ありがとうございました。 – user5949771

関連する問題