Andriod Service 并在内部求和并显示 用广播机制返回求和的值

来源:互联网 发布:ubuntu tweak tools 编辑:程序博客网 时间:2024/05/17 03:35

Android Service简介Android Service是Android重要的组件,在开发中会有用到和涉及。本文先给出一个最简单的Android Service例子。

(第1步)写一个类继承自Service,假设这个类的名字叫做MyAppService.java,重点是完成两个方法:Service的onCreate和onStartCommand方法。onCreate仅仅在Service第一次被startService时候初始化操作一次,随后不管再怎么startService,都不会再onCreate了。

耗时的、后台的、不需要用户交互的操作放在onStartCommand里面处理。需要强调一点,Android的Service并不是一个单独的进程、线程空间,是和Android主线程共享进程空间,这就意味,不要在onStartCommand方法里面阻塞主线程,否则将造成ANR!如果在onStartCommand里面有耗时操作,那么务必将onStartCommand里面的耗时操作代码块放到线程里面做。(注意!次说仅仅针对Service,IntentService和Service机制不同,不存在此问题,但有其自身特点,后面文章再说。)

Service的onDestroy只会被调用一次,那就是Service被stopService或者stopSelf时候。此例用Service实现一个加法


首先要继承Service

package com.example.service;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;import android.widget.Toast;public class Mysevice extends Service{private String KEY="my_key";private String action="com.xh";@Overridepublic void onCreate() {super.onCreate();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {int [] data= intent.getIntArrayExtra(KEY);int num=data[0]+data[1];Toast.makeText(getApplicationContext(), num+"",Toast.LENGTH_LONG).show();Intent in=new Intent();in.putExtra(KEY, num+"");in.setAction(action);this.sendBroadcast(in);return super.onStartCommand(intent, flags, startId);}@Overridepublic IBinder onBind(Intent arg0) {// TODO Auto-generated method stubreturn null;}}
接下来传递到anctivity中 接收

package com.example.service;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity {EditText text1;EditText text2;TextView text;private String KEY="my_key";private String action="com.xh";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        text1=(EditText) findViewById(R.id.text1);        text2=(EditText) findViewById(R.id.text2);                text=(TextView) findViewById(R.id.text);                Button  button=(Button) findViewById(R.id.button);        button.setOnClickListener(new OnClickListener()         {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stub Intent intent=new Intent(MainActivity.this,Mysevice.class); int []data=new int[2]; data[0]=Integer.parseInt(text1.getText().toString()); data[1]=Integer.parseInt(text2.getText().toString()); intent.putExtra(KEY,data); MainActivity.this.startService(intent); }});                myBroadcastReceiver receiver=new myBroadcastReceiver();        IntentFilter intent1=new IntentFilter();        intent1.addAction(action);        this.registerReceiver(receiver, intent1);    }                    @Overrideprotected void onDestroy() {super.onDestroy();}      private class  myBroadcastReceiver  extends BroadcastReceiver   {@Overridepublic void onReceive(Context context, Intent intent) {String num=intent.getStringExtra(KEY);text.setText(num);}      }   }



0 0