意图未将数据传递到通知

如何解决意图未将数据传递到通知

我遇到了一个问题,我的额外意图没有传递给我的通知服务;

所以在我的活动中我有一个计时器, 然后在我的通知中,它显示了具有相同时间的天文钟(我正在为公司构建时间日志应用程序)

现在我要通过计时器的底盘和几个布尔值, 这样我就可以检测到天文钟何时暂停并在通知中显示正确的时间

这是我的通知类的代码

public class TimerBackgroundService extends Service {

    String pausedstring;
    long pauseOffset;
    boolean Paused;
    boolean Restarted;
    long TimeDifference;
    @Override
    public void onCreate() {
        super.onCreate();


    }
    @Override
    public int onStartCommand(Intent intent,int flags,int startId) {
      
intent.getLongExtra("Offset",pauseOffset);
intent.getBooleanExtra("Paused",Paused);
intent.getLongExtra("TimeDiffrence",TimeDifference);
intent.getBooleanExtra("Restarted",Restarted);
        Intent notificationIntent = new Intent(this,IntTimerActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        RemoteViews collapsedView = new RemoteViews(getPackageName(),R.layout.notificationtimer);

        PendingIntent pendingIntent = PendingIntent.getActivity(this,notificationIntent,0);
        collapsedView.setImageViewResource(R.id.notifyimage,R.drawable.sp_icon);
        pausedstring = "Timer is Running";
        collapsedView.setTextViewText(R.id.text_view_collapsed_1,pausedstring);
if(Paused) {
    collapsedView.setChronometer(R.id.NotifyChrono,SystemClock.elapsedRealtime(),null,false);
    pausedstring = "Timer is Paused";
    collapsedView.setTextViewText(R.id.text_view_collapsed_1,pausedstring);
}
if(Restarted){
    collapsedView.setChronometer(R.id.NotifyChrono,TimeDifference + SystemClock.elapsedRealtime(),true);
}




        Notification notification = new NotificationCompat.Builder(this,CHANNEL_ID)
//                .setContentTitle("SmartApp")
//               .setContentText("Timer is Running")

         .setSmallIcon(R.drawable.sp_icon)
              .setContentIntent(pendingIntent)
                 .setWhen(System.currentTimeMillis())
               .setShowWhen(true)
                .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
//                .setContentTitle("SmartPractice SmartApp")
//
//                .setContentText(pausedstring)

                .setContent(collapsedView)
//                .setCustomContentView(collapsedView)
//                .setCustomBigContentView(notificationLayoutExpanded)
                .build();
       startForeground(1,notification);
        //do heavy work on a background thread
        //stopSelf();
        return START_NOT_STICKY;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }

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

这是“活动”中我的计时器的代码

      chrono.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener(){
            @Override
            public void onChronometerTick(Chronometer chronometer) {
                long time = SystemClock.elapsedRealtime() - chronometer.getBase();
                h   = (int)(time /3600000);
                m = (int)(time - h*3600000)/60000;
                int s= (int)(time - h*3600000- m*60000)/1000 ;
                String t = (h < 10 ? "0"+h: h)+":"+(m < 10 ? "0"+m: m)+":"+ (s < 10 ? "0"+s: s);
                chronometer.setText(t);
            }
        });
        chrono.setBase(SystemClock.elapsedRealtime());
        chrono.setText("00:00:00");

        Intent serviceIntent = new Intent(this,TimerBackgroundService.class);
        notificationManager = NotificationManagerCompat.from(this);

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startTimerService();
                chrono.setBase(SystemClock.elapsedRealtime() - pauseOffset);

                chrono.start();
}


        });


        pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pauseOffset=SystemClock.elapsedRealtime() - chrono.getBase();

pauseTimerService();
    chrono.stop();



            }
        });

        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

stopTimerService();
                chrono.stop();
                String tyd =(String) chrono.getText();
                pauseOffset = 0;
                String[] split = tyd.split(":");
                String  Hours =  split[0];
                String  Minutes =  split[1];
                String Seconds =  split[2];
                hoursTextView.setText(Hours);
                

                int sec = Integer.parseInt(Seconds);
                int min = Integer.parseInt(Minutes);

                int one = 1;
                int sum = min + one;
                if(sec >= 30) {
                    MinutesText.setText(String.valueOf(sum));
                }
                else {
                    MinutesText.setText(Minutes);
                }
                chrono.setBase(SystemClock.elapsedRealtime());




            }
        });

    }

    public void startTimerService(){
        Intent serviceIntent = new Intent(this,TimerBackgroundService.class);
        ContextCompat.startForegroundService(this,serviceIntent);
        if(Paused){
            boolean Restarted = true;
            serviceIntent.putExtra("Restarted",Restarted);
        }


    }

    public void pauseTimerService(){
        Intent serviceIntent = new Intent(this,TimerBackgroundService.class);
        long timeDifference = 0;
        Paused = true;
        timeDifference  = chrono.getBase() - SystemClock.elapsedRealtime();
        serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
serviceIntent.putExtra("Offset",pauseOffset);
serviceIntent.putExtra("TimeDiffrence",timeDifference);
serviceIntent.putExtra("Paused",Paused);
        ContextCompat.startForegroundService(this,serviceIntent);

    }
    public void stopTimerService(){
        Intent serviceIntent = new Intent(this,TimerBackgroundService.class);
        stopService(serviceIntent);

    }

然后为您提供方便,这是我传递意图的地方

   public void pauseTimerService(){
            Intent serviceIntent = new Intent(this,TimerBackgroundService.class);
            long timeDifference = 0;
            Paused = true;
            timeDifference  = chrono.getBase() - SystemClock.elapsedRealtime();
            serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
    serviceIntent.putExtra("Offset",pauseOffset);
    serviceIntent.putExtra("TimeDiffrence",timeDifference);
    serviceIntent.putExtra("Paused",Paused);
            ContextCompat.startForegroundService(this,serviceIntent);
    
        }

以及我的意图

public int onStartCommand(Intent intent,int startId) {
          
    intent.getLongExtra("Offset",pauseOffset);
    intent.getBooleanExtra("Paused",Paused);
    intent.getLongExtra("TimeDiffrence",TimeDifference);
    intent.getBooleanExtra("Restarted",Restarted);
            Intent notificationIntent = new Intent(this,IntTimerActivity.class);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            RemoteViews collapsedView = new RemoteViews(getPackageName(),R.layout.notificationtimer);
    
            PendingIntent pendingIntent = PendingIntent.getActivity(this,0);
            collapsedView.setImageViewResource(R.id.notifyimage,R.drawable.sp_icon);
            pausedstring = "Timer is Running";
            collapsedView.setTextViewText(R.id.text_view_collapsed_1,pausedstring);
    if(Paused) {
        collapsedView.setChronometer(R.id.NotifyChrono,false);
        pausedstring = "Timer is Paused";
        collapsedView.setTextViewText(R.id.text_view_collapsed_1,pausedstring);
    }
    if(Restarted){
        collapsedView.setChronometer(R.id.NotifyChrono,true);
    }

至于我已经尝试过的,就是检查我的意图键是否正确,并在意图addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);中添加标志

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com(将#修改为@)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?