Search code examples
javaandroidnotificationmanager

From the notificationbar, to call the working Class


I'm developing an online radio application. The application works in the background. When I click the NotificationManager, radioclass starts working again. I want to call the radioclass that are running. How can I do?

player = MediaPlayer.create(this, Uri.parse("http://...../playlist.m3u8"));
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
            player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer player) {

                }
            });
            player.start();



final int notificationID = 1234;
        String msg = "mesajjj";
        Log.d(TAG, "Preparing to update notification...: " + msg);

        NotificationManager mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);



        Intent intent = new Intent(this, RadioClass.class);

//Here RadioClass running again. But RadioClass already running. I should call RadioClass that is running.

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


    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.logotam)
                .setContentTitle("Test FM")
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setContentText(msg);

        mBuilder.setContentIntent(pendingIntent);
        mNotificationManager.notify(notificationID, mBuilder.build());

Solution

  • If you want to go back to the same activity you are currently running, you can do it like this:

    Add the following tag on your AndroidManifest.xml:

    <activity
    ........
        android:launchMode="singleTop" >
    ........
    </activity>
    

    Modify your PendingIntent like this:

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

    With this modifications, you ensure that your activity will only have one instance at any given time. On your activity class, you can also override the onNewIntent method:

        @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    
    }
    

    This method will handle all of the additional calls to your activity.