Call class inside another in PendingIntent

0

I have this class that plays a song through MediaPlayer . This is working well. Now I want to implement playback controls in the notification area, but I'm having trouble calling the class I have in NotificationReturnSlot where I grab the corresponding action of each button.

I tried to put the inner class in another file but then I could not call the methods of the class AudioInterface . How can I do this? o How could I make it work?

I clarify that this is a Interface call from a WebView , which creates the Play controls and pauses in the notification bar.

public class AudioInterface {
        private NotificationManager nManager;
        private NotificationCompat.Builder nBuilder;
        private RemoteViews remoteView;

    MediaPlayer mPlayer;
    Context mContext;

    AudioInterface(Context c) {
        mContext = c;
    }

    //Play an audio file from the webpage
    @JavascriptInterface
    public void playAudio(String aud) { //String aud - file name passed
        mPlayer = new MediaPlayer();
        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        //String url = aud;
        String url = "http://mysite.com/file.mp3";

        try {
            mPlayer.setDataSource(url);
        } catch (IllegalArgumentException e) {
            Toast.makeText(mContext, "Wrong URI", Toast.LENGTH_LONG).show();
        } catch (SecurityException e) {
            Toast.makeText(mContext, "Wrong URI", Toast.LENGTH_LONG).show();
        } catch (IllegalStateException e) {
            Toast.makeText(mContext, "Wrong URI", Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            mPlayer.prepare();
        } catch (IllegalStateException e) {
            Toast.makeText(mContext, "Wrong URI", Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            Toast.makeText(mContext, "Wrong URI", Toast.LENGTH_LONG).show();
        }

        nBuilder = new NotificationCompat.Builder(mContext)
                .setContentTitle("Test")
                .setSmallIcon(R.drawable.splash_image)
                .setOngoing(true);

        remoteView = new RemoteViews(mContext.getPackageName(), R.layout.notificationview);

        //set the button listeners
        setListeners(remoteView);
        nBuilder.setContent(remoteView);

        nManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        nManager.notify(2, nBuilder.build());
    }

    public void play(){
        this.mPlayer.start();
    }

    public void pause(){
        this.mPlayer.pause();
    }

    public void setListeners(RemoteViews view){
        //listener 1
        Intent playMP = new Intent(mContext,NotificationReturnSlot.class);
        playMP.putExtra("DO", "play");
        PendingIntent btn1 = PendingIntent.getActivity(mContext, 0, playMP, 0);
        view.setOnClickPendingIntent(R.id.btn1, btn1);

        //listener 2
        Intent pauseMP = new Intent(mContext, NotificationReturnSlot.class);
        pauseMP.putExtra("DO", "pause");
        PendingIntent btn2 = PendingIntent.getActivity(mContext, 1, pauseMP, 0);
        view.setOnClickPendingIntent(R.id.btn2, btn2);
    }

    public void notificationCancel() {
        nManager.cancel(2);
    }

    public class NotificationReturnSlot extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);

            String action = (String) getIntent().getExtras().get("DO");

            if (action.equals("play")) {
                Log.i("NotificationReturnSlot", "play");

                Toast.makeText(this, "playing", Toast.LENGTH_SHORT).show();
                play();

            } else if (action.equals("pause")) {
                //Log.i("NotificationReturnSlot", "stopNotification");
                Log.i("NotificationReturnSlot", "pause");

                Toast.makeText(this, "paused", Toast.LENGTH_SHORT).show();
                pause();
            }
            //finish();
        }
    }
}
    
asked by user45089 28.05.2017 в 03:34
source

1 answer

0

I suffered the same problem, I solved it with a Broadcast , first you create the ACTION, you can see it in the package of your Mainactivity, intentfilters, the registers and you tell the pending that they send that to Broadcast

  public static String NEXT_ACTION="com.example.a7860k.reproductor.action.next";
  public static String PLAY_ACTION="com.example.a7860k.reproductor.action.play";
  private IntentFilter previus;
  private IntentFilter play;
  BroadcastReceiver receiver;

        Intent playMP = new Intent(PLAY_ACTION);
        playMP.putExtra("DO", "play");
        PendingIntent btn1 = PendingIntent.getBroadcast(mContext, 0, playMP, 0);
        view.setOnClickPendingIntent(R.id.btn1, btn1);

        //listener 2
        Intent pauseMP = new Intent(NEXT_ACTION);
        pauseMP.putExtra("DO", "pause");
        PendingIntent btn2 = PendingIntent.getBroadcast(mContext, 1, pauseMP, 0);
        view.setOnClickPendingIntent(R.id.btn2, btn2);

Then you create instances to the filters and broadcast

play=new IntentFilter();
next=new IntentFilter();    
play.addAction(PLAY_ACTION);
next.addAction(NEXT_ACTION);
receiver=new BroadcastReceiver() {
       @Override
      public void onReceive(Context context, Intent intent) {
                                if(intent.getAction().equalsPREV_ACTION)){
                                    actionPreviusSong();
                                }

                                if(intent.getAction().equals(PLAY_ACTION)){
                                   actionPlaySong();
                                }

                                if(intent.getAction().equals(NEXT_ACTION)){
                                   actionNextSong();
                                }
                            }
                        };

And finally the registrations

 registerReceiver(receiver,play);
 registerReceiver(receiver,previus);

This is an example that I extracted from my code obviously open discrepancy, try to accommodate yours to make it easier.

    
answered by 29.05.2017 / 06:16
source