使用运行服务会导致活动

时间:2013-05-12 11:31:29

标签: java android service android-activity

在我的服务中,值每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是boolean,用于标识活动正在运行

2 个答案:

答案 0 :(得分:0)

在您的活动中拥有一个Intent并通过代码进行注册。

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

答案 1 :(得分: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/