如何在android中显示TextView对通知的更改?

时间:2018-04-15 12:42:11

标签: android notifications textview

我的应用中有一个TextView,想要在通知中显示其值。如下所示,Textview显示媒体播放的时间。当pauseBTN被点击时,通知会有争议地显示时间。

但通知只显示一个最小化按钮点击的数字。我怎么能改变它?

我的Activity类代码:

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;

    }


}

和我的服务代码:

 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;
    }



}

2 个答案:

答案 0 :(得分:0)

我认为您正在寻找更新通知的方法吗?

您可以保留构建者创建的通知实例,并且您可以更改通知内容,例如,如果您使用相同的通知ID通知通知管理器,那么您可以更改textView的更新文本,以便在状态栏中更新通知

查看链接: Update text of notification, not entire notification

//示例

此代码在活动的onCreate上触发通知,并在textView中显示设备的小时:最小:秒并每1秒更新一次通知

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();
    }
}

答案 1 :(得分:0)

我找到了答案。无需创建Service类。所以,我将Notification放在start()方法中。

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());

}