2

I have (strange!) another question for all of you!

I have this code :

package com.radio.radiostar;

import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

import java.io.IOException;



 public class MainActivity extends Activity implements OnClickListener {

private final static String RADIO_STATION_URL = "http://178.32.137.180:8665/stream";

private ProgressBar playSeekBar;

private Button buttonPlay;

private Button buttonStopPlay;

private MediaPlayer player;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initializeUIElements();

    initializeMediaPlayer();
}


private void initializeUIElements() {

    playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
    playSeekBar.setMax(100);
    playSeekBar.setVisibility(View.INVISIBLE);

    buttonPlay = (Button) findViewById(R.id.buttonPlay);
    buttonPlay.setOnClickListener(this);

    buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
    buttonStopPlay.setEnabled(false);
    buttonStopPlay.setOnClickListener(this);

}

@SuppressWarnings("unused")
public void onClick(View v) {
    if (v == buttonPlay) {
        startPlaying();
        Context context = getApplicationContext();
        CharSequence text = "In Connessione...";
        int duration = android.widget.Toast.LENGTH_LONG;
        android.widget.Toast toast = android.widget.Toast.makeText(context, text, duration);
        toast.show();
        Object nm;
        nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        Intent intent = new Intent(this,MainActivity.class);
        PendingIntent pendingIntent = null;
        NotificationCompat.Builder mBuilder =

                new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Radio Star")
                .setContentText("In Diretta")
                .setContentIntent(pendingIntent); //Required on Gingerbread and below
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, new Intent(), Intent.FLAG_ACTIVITY_NEW_TASK);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify( 0, mBuilder.build());
    } else if (v == buttonStopPlay) {
        stopPlaying();
        Intent svc=new Intent(this, BackgroundSoundService.class);
        startService(svc);
    }
}

private void startPlaying() {
    if (player.isPlaying()) {
        buttonPlay.setVisibility(View.INVISIBLE);
    }
    buttonStopPlay.setEnabled(true);
    buttonPlay.setEnabled(false);
    buttonPlay.setVisibility(View.INVISIBLE);
    playSeekBar.setVisibility(View.VISIBLE);


    player.prepareAsync();

    player.setOnPreparedListener(new OnPreparedListener() {

        public void onPrepared(MediaPlayer mp) {
            player.start();
        }

    });

}

private void stopPlaying() {
    if (player.isPlaying()) {
        player.stop();
        player.release();
        initializeMediaPlayer();
        NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
        notificationManager.cancel(0);
    }

    buttonPlay.setEnabled(true);
    buttonStopPlay.setEnabled(false);
    playSeekBar.setVisibility(View.INVISIBLE);
    buttonPlay.setVisibility(View.VISIBLE);

}

private void initializeMediaPlayer() {
    player = new MediaPlayer();
    try {
        player.setDataSource(RADIO_STATION_URL);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

        public void onBufferingUpdate(MediaPlayer mp, int percent) {
            playSeekBar.setSecondaryProgress(percent);
            Log.i("Buffering", "" + percent);
        }
    });
}

@Override
public void onBackPressed()
{
   moveTaskToBack(false);


}


};

On the notification part, i have the problem that this notification don't open the app. Any suggestion? I was trying a lot of possibility, but nothing... where i'm wrong? Help me please.

Fabio Pinna
  • 75
  • 3
  • 9

1 Answers1

5

Your pendingIntent is null.

Replace this:

PendingIntent pendingIntent = null;

with this:

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);

EDIT

Code for creating notification:

Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("Radio Star")
        .setContentText("In Diretta")
        .setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify( 0, mBuilder.build());
Boban S.
  • 1,662
  • 13
  • 16
  • Mmmh, i have tried but nothing :/ notification don't open my app... But one time, using some code, was working.. other ideas @Boban S. ? – Fabio Pinna Oct 30 '14 at 16:17
  • I have no idea at the moment. I will check and come back to you if I have something in mind. But it is very strange to me that this is not working... – Boban S. Oct 30 '14 at 16:45
  • I know :/ look maybe better the part of void onclick :/ I have PendingIntent pendingIntent = null; and PendingIntent pIntent = PendingIntent.getActivity(this, 0, new Intent(), Intent.FLAG_ACTIVITY_NEW_TASK); after... but also, if i delete this secon string, and paste your, nothing change :/ – Fabio Pinna Oct 30 '14 at 17:40
  • As I understood you, you are adding my line after the builder. You need to add it before. Look at the edited answer. – Boban S. Oct 31 '14 at 08:35
  • Nice, now it's opened my app (touching on the notification) but! it's opening with some seconds of retard :/ And it open a new activity, because if i press play, then i press home and i tap on notification, the app opened it's with the play showed (and must be tha pause button ) Any other help? – Fabio Pinna Oct 31 '14 at 14:06
  • What is the problem now? Why but? – Boban S. Oct 31 '14 at 14:07
  • I Edited my answer after "but" :) – Fabio Pinna Oct 31 '14 at 14:43
  • here you can find about starting mode of activity: http://stackoverflow.com/questions/12043671/notification-click-activity-already-open this can help you with your new problem, i helped you solve this one – Boban S. Oct 31 '14 at 16:12
  • Done :) I think that I ll post a new question xD – Fabio Pinna Nov 03 '14 at 12:33