• 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)

  • 相关阅读:
    中台之交付
    mysql之事务
    中台之中台的设计
    0318 guava并发工具
    0312 java接口测试三棱军刺rest-assured
    0309 软件基本原理1
    0308 软件系统的非功能需求
    PELT(Per-Entity Load Tracking)
    CPU亲和度
    硬件相关知识随手笔记
  • 原文地址:https://www.cnblogs.com/ct732003684/p/2956177.html
Copyright © 2020-2023  润新知