美文网首页
Service实现轮询更新Ui

Service实现轮询更新Ui

作者: heyzhuyue | 来源:发表于2017-05-11 15:44 被阅读574次
    • Service定时轮询来更新前台的信息
         public class CityWeatherService extends Service  
        {  
                private static final int UPDATAWEATHER = 0X10;  
      
                private final int GOTOBROADCAST = 0X20;  
      
             public static final String BROADCASTACTION = "com.jone.broad";  
      
            Timer timer;  
      
      
             @Override  
             public IBinder onBind(Intent arg0)  
                {  
                  // TODO Auto-generated method stub  
                 return null;  
            }  
      
             @Override  
            public int onStartCommand(Intent intent, int flags, int startId)  
            {  
      
            // updateWeather();  
            timer = new Timer();  
            timer.schedule( new TimerTask()  
            {  
      
                @Override  
                public void run()  
                {  
                    // 定时更新  
                    String jsonString = getWeather();  
                    // 发送广播  
                    Intent intent = new Intent();  
                    intent.setAction( BROADCASTACTION );  
                    intent.putExtra( "jsonstr", jsonString );  
                    sendBroadcast( intent );  
      
                }  
            }, 0, 20 * 1000 );  
      
            return super.onStartCommand( intent, flags, startId );  
                }  
    
    • Activity内部定义一个广播内部内,用于接受Service发送的广播,通过Handler通知Ui更新界面.
            public class MainActivity extends Activity  
            {  
      
            @Override  
            protected void onCreate(Bundle savedInstanceState)  
             {  
                super.onCreate( savedInstanceState );  
                setContentView( R.layout.activity_main );  
                Intent intent = new Intent();  
                intent.setClass(this, CityWeatherService.class);  
                startService(intent);  
                broadcastMain = new BroadcastMain();  
                IntentFilter filter = new IntentFilter();  
                filter.addAction( CityWeatherService.BROADCASTACTION );  
                registerReceiver( broadcastMain, filter );  
              
            }  
      
            //接受Service发送的广播Receiver
            public class BroadcastMain extends BroadcastReceiver{  
            @Override  
            public void onReceive(Context context, Intent intent)  
            {  
                Message msg = handler.obtainMessage();  
                msg.what = 01;  
                handler.sendMessage( msg );  
                }  
            }
            
            //更新Ui的Handler
            Handler handler = new Handler()  
            {  
                public void handleMessage(android.os.Message msg)  
                    {  
                     switch (msg.what)  
                    {  
                     case 01:  
                        weather = manager.getWeather();  
                        cityTextView.setText( weather.getCityName() );  
                        break;  
      
                    default:  
                        break;  
                }  
                  
            };  
      
            };  
            @Override  
            protected void onDestroy()  
            {  
      
            Intent intent = new Intent();  
            intent.setClass(this, CityWeatherService.class);  
            stopService(intent);  
            super.onDestroy();  
                }  
            }  
    

    相关文章

      网友评论

          本文标题:Service实现轮询更新Ui

          本文链接:https://www.haomeiwen.com/subject/guiotxtx.html