zoukankan      html  css  js  c++  java
  • service broadcast更新activity界面

    package com.ct.dynamicui;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.graphics.Color;
    import android.view.Menu;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    
        public static String TIME_CHANGED_ACTION = "com.ct.dynamicui.TIME_CHANGED_ACTION"; 
        public static TextView time = null;
        private Intent timeService  = null;
        UITimeReceiver receiver ;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            initView();
            registerBroadcastReceiver();   
            startTimeService();  
        }
        
        
        public static TextView getTime() {
            return time;
        }
    
    
        public static void setTime(TextView time) {
            MainActivity.time = time;
        }
    
    
        private void initView(){
            time = (TextView)findViewById(R.id.time);
            time.setTextColor(Color.RED);  
            time.setTextSize(15);  
        }
        
        private void startTimeService(){
            timeService = new Intent(this,MyService.class);
            startService(timeService);
        }
        
        private void registerBroadcastReceiver(){
            receiver = new UITimeReceiver();
            IntentFilter filter = new IntentFilter(TIME_CHANGED_ACTION);
            registerReceiver(receiver, filter);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    
        @Override
        protected void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            unregisterReceiver(receiver);
            stopService(timeService);
        }
        
        
    
    }
    package com.ct.dynamicui;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Timer;
    import java.util.TimerTask;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.IBinder;
    
    public class MyService extends Service{
    
        private String TAG = "TimeSerMyServicevice";  
        private Timer timer = null;  
        private SimpleDateFormat sdf = null;  
        private Intent timeIntent = null;  
        private Bundle bundle = null;  
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public void onCreate() {
            // TODO Auto-generated method stub
            super.onCreate();
            init();
            timer.schedule(new TimerTask() {
                
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    sendTimeChangedBroadcast();
                }
            }, 1000,1000);
        }
        
        private void init(){
            timer = new Timer();
            sdf = new SimpleDateFormat("yyyy年MM月dd日 "+"hh:mm:ss");
            timeIntent = new Intent();
            bundle = new Bundle();
        }
        
        private String getTime(){  
            return sdf.format(new Date());  
        }  
        private void sendTimeChangedBroadcast(){
            bundle.putString("time", getTime());
            timeIntent.setAction(MainActivity.TIME_CHANGED_ACTION);
            timeIntent.putExtras(bundle);
            sendBroadcast(timeIntent);
        }
    
        @Override
        public void onStart(Intent intent, int startId) {
            // TODO Auto-generated method stub
            super.onStart(intent, startId);
        }
    
        
    }
    package com.ct.dynamicui;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    
    public class UITimeReceiver extends BroadcastReceiver{
    
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            String action = intent.getAction();
            if(action.equals(MainActivity.TIME_CHANGED_ACTION)){
                Bundle bundle = intent.getExtras();
                String stringTime = intent.getStringExtra("time");
                MainActivity.getTime().setText(stringTime);
            }
        }
    
    }

     (在F:\java\DynamicUI)

  • 相关阅读:
    大话设计模式——UML图
    IdentityServer3零星笔记
    Angular路由
    基于jquery的静态页面翻页
    00_python安装与配置(mac)
    OracleParameter.UdtTypeName的值必须是全大写!
    VS2012调用64位IIS Express
    MVC中使用Ueditor
    优秀博客站点
    jqGrid中的formatter
  • 原文地址:https://www.cnblogs.com/ct732003684/p/2956177.html
Copyright © 2011-2022 走看看