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" />

posted @ 2011-08-29 23:05  rorshach  阅读(102)  评论(0编辑  收藏  举报