• service+activity


    <span style="font-size:18px;">package com.gufengxiachen.broadcast;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    
    
    
    public class LocalService extends Service{
    	private int progress;
    	@Override
    	public IBinder onBind(Intent intent) {
    		// TODO Auto-generated method stub
    		return null;
    	}
    	
    	@Override
    	public void onStart(Intent intent, int startId) {
    		// TODO Auto-generated method stub
    		super.onStart(intent, startId);
    		//得到Activity中传递过来的数据
    		progress = intent.getIntExtra("progress", 0);
    		//创建一个线程,每隔一秒向Activity发送一个广播,并将progress的新值发送出去
    		new Thread(){
    			public void run() {
    				while(progress<100){
    					progress = progress+1;
    					Intent intent = new Intent();
    					intent.putExtra("serviceProgress", progress);
    					//设置发送广播的类型,可以随便写一个
    					intent.setAction("com.gufengxiachen.service");
    					sendBroadcast(intent);
    					try {
    						sleep(1000);
    					} catch (Exception e) {
    						e.printStackTrace();
    					}
    				}
    			}
    		}.start();
    	}
    	
    
    }
    </span>
    

      

    <span style="font-size:18px;">package com.gufengxiachen.broadcast;
    
    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.ProgressBar;
    import android.widget.SeekBar;
    import android.widget.SeekBar.OnSeekBarChangeListener;
    
    public class BroadcastReceiverTestActivity extends Activity {
        
    	private ProgressBar progressBar;
    	private Button button;
    	private int progress = 0;
    	private MyBroadcastReceiver myBroadcastReceiver = new MyBroadcastReceiver();
    	
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            progressBar = (ProgressBar)findViewById(R.id.progressBar);
            button = (Button)findViewById(R.id.button);
            progressBar.setMax(100);
            button.setOnClickListener(new OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    				// TODO Auto-generated method stub
    				//在这里启动服务
    				Intent intent  = new Intent();
    		    	intent.putExtra("progress", progress);
    		    	intent.setClass(BroadcastReceiverTestActivity.this, LocalService.class);
    		    	startService(intent);
    			}
    		});
           
        }
        
        @Override
        protected void onResume() {
        	// TODO Auto-generated method stub
        	super.onResume();
        	//注册广播接收器
        	IntentFilter filter = new IntentFilter();
        	//设置接收广播的类型,这里要和Service里设置的类型匹配,还可以在AndroidManifest.xml文件中注册
        	filter.addAction("com.gufengxiachen.service");
        	this.registerReceiver(myBroadcastReceiver, filter);
    
        }
        
        @Override
        protected void onStop() {
        	// TODO Auto-generated method stub
        	super.onStop();
        	this.unregisterReceiver(myBroadcastReceiver);
        }
        //定义一个广播接收器
        class MyBroadcastReceiver extends BroadcastReceiver{
        	@Override
        	public void onReceive(Context context, Intent intent) {
        		// TODO Auto-generated method stub
        		//接收到Service发送的广播信息,得到数据,更新UI
        		progress = intent.getIntExtra("serviceProgress", 0);
        		progressBar.setProgress(progress);
        	}
        }
    }</span>
    http://blog.csdn.net/wustrive_2008/article/details/7188062
  • 相关阅读:
    No.5 Verilog 建模方式
    No.1 Verilog HDL简介
    [好文推荐]能大大提升工作效率和时间效率的9个重要习惯
    org.apache.commons.net.ftp.FTPClient上传文件大小改变的解决方法
    利用myeclipse建立webservice服务端和客户端
    FtpClient.storeFile返回false解决方法
    jquery表单formSerialize方法乱码问题解决
    Apache FTPClient下载地址
    Myeclipse发生java heap space错误
    UtraEdit的下载地址,无需注册码的
  • 原文地址:https://www.cnblogs.com/leiqun123/p/3339969.html
Copyright © 2020-2023  润新知