2016-05-20 10 views
0

Permission Dispatcherを使用してMarshmallow権限を正常に処理しています。 $ {className} PermissionDispatcherの "this"にアクセスできない静的クラスで使用する方法はありますか?$ {method} WitchCheck(this)?"Permissions Dispatcher"を使用してMarshmallow権限を追加する

の作業例:

@RuntimePermissions 
public class ContactsMapFragment extends mFragment implements OnMapReadyCallback { 

    @Override 
    public void onMapReady(@NonNull GoogleMap googleMap) { 
     if (DeviceUtils.isMarshmallow()) 
      ContactsMapFragmentPermissionsDispatcher.setMapLocationWithCheck(this, googleMap); 
     else 
      googleMap.setMyLocationEnabled(true); 
    } 

    @NeedsPermission(Manifest.permission.ACCESS_FINE_LOCATION) 
    protected void setMapLocation(@NonNull GoogleMap gmap) { 
     gmap.setMyLocationEnabled(true); 
    } 
} 

しかし、この他のスニペットでは、私は静的クラスを持っているので、このが存在しません:

@RuntimePermissions 
public class PhoneUtils 
{ 
    @NeedsPermission(Manifest.permission.CALL_PHONE) 
    public static void makeCall(String szNumber) 
    { 
     Intent intent = new Intent(
       Intent.ACTION_CALL, // place a phone call directly 
       Uri.parse("tel:" + szNumber) 
     ); 

     mActivity activity = Muffin.getInstance().getActivity(); 
     if (activity == null) 
      return; 

     if (DeviceUtils.isMarshmallow()) 
      PhoneHelperPermissionDispatcher.startCallWithCheck(this, activity, intent); 
     else 
      activity.startActivity(intent); 
    } 

    protected void startCall(mActivity activity, Intent intent) 
    { 
     activity.startActivity(intent); 
    } 
} 
+0

はあなたの関連するコードを少し提供してくださいできますか? – Opiatefuchs

+0

質問を編集しました – HelLViS69

+0

これを試してみてくださいstackoverflow.com/a/41221852/5488468 –

答えて

0
のみの活動とフラグメントが許可されているので、Androidのフレームワークだけで活動にrequestPermission方法を提供して静的クラスは、注釈を付けることができない

およびフラグメント

0

を私が許可ディスパッチャライブラリを使用しますが、私していません静的クラスのメソッドにActivityまたはContextを渡して、それを使用できると思います。

または

あなたはグローバルコンテキストを維持するために、Applicationクラスを使用することができます

。例えば

public class MyDemoApplication extends Application { 

private static Context context; 

public void onCreate() { 
    super.onCreate(); 
    MyDemoApplication .context = getApplicationContext(); 
} 

public static Context getAppContext() { 
    return MyDemoApplication.context; 
} 

}

あなたは任意のクラスファイルから静的アプリケーションのコンテキストを取得するためにMyDemoApplication.getAppContext()を呼び出す必要があります。

注:プロジェクトでアプリケーションクラスを拡張する場合は、マニフェストファイルのapplicationタグにその名前を指定することを忘れないでください。

<?xml version="1.0" encoding="utf-8"?> 

<activity android:name=".MainActivity" 
    android:label="@string/app_name"> 
    <intent-filter> 
    <action android:name="android.intent.action.MAIN"/> 
    <category android:name="android.intent.category.LAUNCHER"/> 
    </intent-filter> 
</activity> 

</application> 
</manifest> 

詳細は以下のリンクを参照してください:

https://developer.android.com/reference/android/app/Application.html

http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/

関連する問題