0

在我的服务中,值每 1 秒更新一次。正在运行的活动运行一次,因此我无法使用新结果和更新来更新文本视图或函数。尝试在活动中使用计时器:

 Timer timer = new Timer();
    timer.scheduleAtFixedRate( 
               new java.util.TimerTask() {
                   @Override
                   public void run() {
                       if(Serve.Connecting){
                             tv = (TextView)findViewById(R.id.textView1);
                                tv.setText("Connecting");
                                ProgressBar PB =  (ProgressBar) findViewById(R.id.progressBar1);
                                PB.setProgress(25);
                        }
                        if(Serve.Connected){
                             tv = (TextView)findViewById(R.id.textView1);
                                tv.setText("Connected");
                                ProgressBar PB =  (ProgressBar) findViewById(R.id.progressBar1);
                                PB.setProgress(50);
                        }
                        if(Serve.Connected && Serve.loading.FALSE){
                             tv = (TextView)findViewById(R.id.textView1);
                                tv.setText("Loading");
                             ProgressBar PB =  (ProgressBar) findViewById(R.id.progressBar1);
                            PB.setProgress(75);
                        }
                        if(Serve.Connected && Serve.loading){
                             tv = (TextView)findViewById(R.id.textView1);
                                tv.setText("Done");
                                ProgressBar PB =  (ProgressBar) findViewById(R.id.progressBar1);
                                PB.setProgress(100);
                                Intent intent = new Intent(Loadingpage.this, Setup.class);
                                 startActivity(intent);
                                 finish();
                                 frstscr=false;

                        } 



           }},
           200, 500
           );

但它没有用,所以我尝试使用该处理程序示例:

private Handler handler = new Handler();
runnable.run();

    private Runnable runnable = new Runnable() 
{

public void run() 
{
     //
     // Do the stuff
     //

     handler.postDelayed(this, 1000);
}};

但没用,有什么想法吗??我什至使用了 while(frstscrn){fun },其中 frstscrn 是布尔值,用于标识活动正在运行

4

2 回答 2

0

您的活动将如下所示..

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class GenerateBroadcastActivity extends Activity 
{
    Button bt ;

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

        bt = (Button) findViewById(R.id.bt1);
        bt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startService(new Intent(getApplicationContext(),MyNewService.class));
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(receiver, new IntentFilter("BROADCAST_UNIQUE_NAME"));
    }

    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

             //take out data from intent
              int progressValue = intent.getIntExtra("progressValue",0);

             // just for instance , you can set the progress in your progress bar //defined globally in your app 
              progressBar.setProgress(progressValue);  

            Toast.makeText(context, "Broadcast coming with Progress value" + progressValue, Toast.LENGTH_LONG).show();          
        }
    };
}

像这样写你的服务类......

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.util.Log;

public class MyNewService extends IntentService 
{

    public MyNewService()
    {
      super("Service Name");    
    }

    @Override
    protected void onHandleIntent(Intent intent) 
    {
       //Do some background task . 
          int progressValue = 20 ;

       //Send broadcast like this
        Intent broadcast = new Intent();
       // name should be same as specified in your activity class onResume method
        broadcast.setAction("BROADCAST_UNIQUE_NAME");
       //send extra data with your broadcast   
       broadcast.putExtra("progressValue",progressValue);
       //send it now
       sendBroadcast(broadcast); 
    }
}

此广播将由您的广播接收器接收,您将在屏幕上收到一条消息。有关基础知识,请参阅此链接:http ://www.edureka.in/blog/android-tutorials-broadcast-receivers/

于 2013-05-12T12:03:34.520 回答
0

在你的活动中有一个意图并通过代码注册它。

从您的服务中,通过广播意图发送更新的值,然后onReceive()将值更新到 UI 组件

于 2013-05-12T11:36:00.177 回答