簡體   English   中英

使用正在運行的服務會導致活動

[英]Using Running service results in activity

在我的服務中,值每1秒更新一次。 正在運行的活動運行一次,因此我無法使用新結果和更新來更新textviews或函數。試圖在活動中使用timer:

 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是布爾值,用於標識活動正在運行

在您的活動中有一個意圖,並通過代碼進行注冊。

從您的服務中,通過廣播意圖發送更新的值,然后從onReceive()將值更新到UI組件

您的活動將如下所示..

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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM