zoukankan      html  css  js  c++  java
  • Service(三)

    Activity类:

    public class ServiceUI extends Activity {

            

            private mServiceUIReceiver mReceiver = null;

            private TextView tv_msg = null;

            private ProgressDialog mProgressDialog = null;

            private Intent intent = null;

            

        /** Called when the activity is first created. */

        @Override

        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.main);

            

            tv_msg = (TextView)findViewById(R.id.tv_msg);

            

            //注册BroadcastReceiver

            mReceiver = new mServiceUIReceiver();

            IntentFilter mFilter = new IntentFilter(ServiceUIService.SERVICEUI_SERVICE);

            registerReceiver(mReceiver, mFilter);

            

            //开启服务

            intent = new Intent();

            intent.setClass(this, ServiceUIService.class);

            startService(intent);

            

            //初始化ProgressDialog

            mProgressDialog = new ProgressDialog(this);

            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

            mProgressDialog.setMessage("正在启动服务...");

            mProgressDialog.setCancelable(false);

        }

        

        @Override

        protected void onDestroy() {

                unregisterReceiver(mReceiver);

                stopService(intent);            

                super.onDestroy();

        }

        

        private class mServiceUIReceiver extends BroadcastReceiver{

                //广播接受数据

                    @Override

                    public void onReceive(Context context, Intent intent) {

                            

                            Bundle bundle = intent.getExtras();

                            int pd_value = bundle.getInt("ProgressDialogValue");

                            String pd_data=bundle.getString("data");

                            if(pd_value==0){

                                    //显示ProgressDialog

                                    mProgressDialog.show();

                            }

                            if(pd_value==100){

                                    //销毁ProgressDialog

                                    mProgressDialog.dismiss();

                                    tv_msg.setTextSize(25f);

                                    tv_msg.setText(pd_data);

                                    

                            }

                    }

        }

    }

    Manifest代码清单:

    <service android:name=".ServiceUIService" android:enabled="true" />

  • 相关阅读:
    驱动下的异常处理
    头文件 .h 与源文件 .ccp 的区别
    驱动程序进阶篇
    驱动中链表的使用
    内存操作相关内核 API 的使用
    链表的概念、建立、删除与插入
    编写简单的 NT 式驱动程序的加载与卸载工具
    驱动程序入门篇
    c++ 指针的简单用法
    CTL_CODE 宏 详解
  • 原文地址:https://www.cnblogs.com/itfenqing/p/4429519.html
Copyright © 2011-2022 走看看