ActivityCompat.requestPermissions does not show prompt

前端 未结 4 1641
囚心锁ツ
囚心锁ツ 2021-01-05 02:28

I\'m attempting to request ACCESS_FINE_LOCATION permissions in order to get the user\'s current location.

My logging indicates that my app does not curr

4条回答
  •  甜味超标
    2021-01-05 03:04

    I've faced the same problem on a project which use TabHost. Basis on @Robin solution, I use the EventBus library for send a message from child activity to the TabActity.

    EventBus : https://github.com/greenrobot/EventBus

    Create an event object :

    public class MessageEvent {
        private String message;
        public MessageEvent(String message){
            this.message = message;
        }
    
        public String getMessage(){
            return this.message;
        }
    }
    

    In your main Activity :

    private EventBus eventBus = EventBus.getDefault();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        eventBus.register(this);
    }
    @Override
    protected void onDestroy() {
        eventBus.unregister(this);
        super.onDestroy();
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        if (event.getMessage().equals("contacts")){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(android.Manifest.permission.WRITE_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(MainPage.this,new String[]{android.Manifest.permission.WRITE_CONTACTS}, 100 );
            }
        }
    };
    

    Set a different message for the permission your want to request. In your child activity you can than post the adequate message :

    EventBus.getDefault().post(new MessageEvent("contacts"));
    

    Be aware of onRequestPermissionsResult callback and the request code ;)! It will only work in the main activity.

提交回复
热议问题