从服务获取数据到活动 - 帮助!

时间:2011-04-21 20:45:06

标签: android service android-intent

我现在正在使用该服务。我只是无法从服务中获取信息到主要活动。

我添加了一个函数来尝试从服务中检索数据,该服务在主活动中的按钮单击启动服务后启动:

startService(passvars);
                //Init. variable for getDisplayInf() function to update the display with counter strings
                Running = 1;
                getDisplayInf();

getDisplayInf()ex:

public void getDisplayInf() {
        //Intent stIntent = new Intent(MainActivity.this,MainActivity.class);
        //Intent passvars = new Intent(MainActivity.this,Service.class);
        Bundle getvars = getIntent().getExtras();
        String Remaining;
        String CountDown;
        do {
            Remaining = getvars.getString("Remaining");
            CountDown = getvars.getString("CountDown");
            mRemaining.setText(Remaining);
            mCountDown.setText(CountDown);
            Running = getvars.getInt("Running");
        }
        while (Running == 1);
    };

计时器将在服务内部完成时将Running变量设置为0。 一旦我点击了这个新功能的按钮就会崩溃应用程序,所以我假设它与我在getDisplayInf()代码中使用的Bundle / intent有关。

在服务中,我正在柜台的onTick上执行此操作:

            @Override
        public void onTick(long millisUntilFinished) {
            Running = 1;
            Remaining = "Time Remaining:  ";
            CountDown = formatTime(millisUntilFinished);
            ticker();
        }

自动收报机代码:

public void ticker () {
         Intent passvars = new Intent(Service.this,MainActivity.class);
        //this is for the activity to keep looping to grab the countdown values while running this timer

        //now send it to the activity
        passvars.putExtra("Running", Running);
        //String Values to be passed to the activity

        //now send it to the activity
        passvars.putExtra("mRemaining", Remaining);


        //now send it to the activity
        passvars.putExtra("mCountDown", CountDown);
     };

我是否以正确的方式解决这个问题?有没有更简单的方法?一些帮助将非常感谢!!!

0 个答案:

没有答案
相关问题