register as music player

后端 未结 4 976
星月不相逢
星月不相逢 2020-12-09 20:26

I\'d like to see my app in the list of player (\"continue action using...\") that pops up when I try to open an audio file (ie. from file browser or gmail attachment). Here

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-09 21:05

    The CommonsWare answer regarding Manifest is good and it works.

    "But when I select song and select my app from list, it opens my app successfully but not playing that selected song. what to do for that?? "

    If your app is not in background, in your activity OnCreate you can do

     Uri data = getIntent().getData()
     if (data!=null){/*use this Uri like you want*/}
    

    In case your app is in background the Intent you have to intercept will pass through the onNewIntent() method that you have to ovveride.

    Here is a complete code example:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*...*/
        interceptExternalIntentAndPlaySongFromUri(getIntent());
    }
    
    @Override
    protected void onNewIntent(Intent intent) {
        interceptExternalIntentAndPlaySongFromUri(intent);
    }
    
    private boolean interceptExternalIntentAndPlaySongFromUri(Intent intent){
        Uri data = intent.getData();
        if (data != null && intent.getAction() != null && 
    intent.getAction().equals(Intent.ACTION_VIEW)){      
            String scheme = data.getScheme();
            String filename = "";
            if ("file".equals(scheme)) {
                filename = data.getPath();
            } else {
                filename = data.toString();
            }
            player.setDataSource(filename);
            setIntent(new Intent());        //this is to remove the last intent 
    //without the above line, last request is reloaded when the user open another app 
    //and return in this
            return true;        //has intercepted an external intent. so you can load this uri.
        }
        return false;           //has not intent to intercept
    }
    

提交回复
热议问题