0

I have a TextView in my app and want to show its value in Notification. as you see below Textview shows times of media playing. when pauseBTN clicked, notification shows the times contentiously.

but notification just show one number that minimize button clicked. how can I change it?

my Activity class code:

public class CountActivity extends AppCompatActivity  implements View.OnClickListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_count);

    loop_num_txt= Step2Fragment.times;
    loop_num=Integer.parseInt(loop_num_txt);

    mediaPlayer=new MediaPlayer();
    try {
        mediaPlayer.setDataSource(audioPath);
        mediaPlayer.prepare();
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(this,"فایل صوتی ضبط شده در دسترس نیست.",Toast.LENGTH_LONG).show();
    }

    pause= (Button) findViewById(R.id.pauseBTN); pause.setOnClickListener(this);
    play= (Button) findViewById(R.id.playBTN); play.setOnClickListener(this);
    stop= (Button) findViewById(R.id.stopBTN); stop.setOnClickListener(this);
    minimize= (Button) findViewById(R.id.minimizeBTN); minimize.setOnClickListener(this);

    start(loop_num);
}//oncreate

public void start(final int i){
    show_time = i;
    mediaPlayer.start();

    int duration = mediaPlayer.getDuration();
    animationDrawable.addFrame(getResources().getDrawable(R.drawable.t1),duration);

    counter.setText(Integer.toString(show_time));


    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            show_time--;
            if (show_time !=0){
                start(show_time);
            }else {
                vibrate(1000);
                counter.setText("0");
                imageView.setBackgroundResource(R.drawable.t2);
            }
        }
    });

}


@Override
public void onClick(View view) {
    resume_txt = counter.getText().toString();
    int resume_int=Integer.parseInt(resume_txt);

    switch (view.getId()){
        case R.id.pauseBTN:
            mediaPlayer.pause();
            animationDrawable.stop();
            play.setVisibility(View.VISIBLE); pause.setVisibility(View.GONE);
            break;

        case R.id.playBTN:
            start(resume_int);
            play.setVisibility(View.GONE); pause.setVisibility(View.VISIBLE);
            break;

        case R.id.stopBTN:
            animationDrawable.stop();
            mediaPlayer.stop();
            counter.setText("0");
            finish();
            startActivity(new Intent(CountActivity.this,MainActivity.class));
            break;

        case R.id.minimizeBTN:
            Intent intent= new Intent(CountActivity.this,NotifyService.class);
            //intent.putExtra("times",resume_txt);
            startService(intent);
            moveTaskToBack(true);
            break;

    }


}

and my service code:

 public class NotifyService extends Service {
    public NotificationCompat.Builder builder;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Intent notificationIntent_Restore = new Intent(this,CountActivity.class);
        PendingIntent restoreIntent = PendingIntent.getActivity(this,
                0, notificationIntent_Restore, PendingIntent.FLAG_UPDATE_CURRENT);

         builder= (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(false)
                 .setContentText(CountActivity.resume_txt)
                .setContentIntent(restoreIntent);

        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(0, builder.build());
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }



}
iamkdblue
  • 3,448
  • 2
  • 25
  • 43

2 Answers2

1

i think you are looking for way to update your notification ?

you can keep instance of your notification which builders create and than you can change your notification content for example update text of the textView if you notify notification Manager with the same notification id that gonna update your notification in status bar

see the link : Update text of notification, not entire notification

// Example

this code fires notification at the onCreate of the activity and show 's the Hour:Min:Sec of the device inside the textView and updates notification every 1 second

import android.app.Notification;
import android.app.NotificationManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RemoteViews;

import java.util.Date;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_view);
        remoteViews.setTextViewText(R.id.layout_notification_Text, "00:00:00");

        final Notification notification = new Notification.Builder(getApplicationContext())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentText("Hello")
                .setContent(remoteViews)
                .getNotification();

        final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(1, notification);

        new Thread(new Runnable() {
            @Override
            public void run() {

                while (true) {

                    Date date = new Date(System.currentTimeMillis());
                    int h = date.getHours();
                    int m = date.getMinutes();
                    int s=date.getSeconds();

                    remoteViews.setTextViewText(R.id.layout_notification_Text, h + ":" + m+":"+s);

                    notificationManager.notify(1,notification);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();
    }
}
Amir Hossein Mirzaei
  • 2,325
  • 1
  • 9
  • 17
0

I find my answer. there is no need to create Service class. so, I put Notification in start() method.

public void start(final int i){
    show_time = i;
    mediaPlayer.start();

    int duration = mediaPlayer.getDuration();
    animationDrawable.addFrame(getResources().getDrawable(R.drawable.t1),duration);
    animationDrawable.addFrame(getResources().getDrawable(R.drawable.t2),200);
    imageView.setBackgroundDrawable(animationDrawable);
    animationDrawable.start();

    counter.setText(Integer.toString(show_time));
    setupNotification(show_time);

    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            show_time--;
            if (show_time !=0){
                start(show_time);
            }else {
                vibrate(1000);
                counter.setText("0");
                imageView.setBackgroundResource(R.drawable.t2);
            }
        }
    });

}

private void setupNotification(int j){
    String note= Integer.toString(j);

    Intent notificationIntent_Restore = new Intent(this,CountActivity.class);
    notificationIntent_Restore.putExtra("TIMES",note);
    PendingIntent restoreIntent = PendingIntent.getActivity(this,
            0, notificationIntent_Restore, PendingIntent.FLAG_UPDATE_CURRENT);

    builder= (NotificationCompat.Builder) new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource( getResources(), R.mipmap.ic_launcher))
            .setContentTitle("وقت ندارم!")
            .setAutoCancel(false)
            .setContentText(note)
            .setContentIntent(restoreIntent);



     manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(0, builder.build());

}