Android Service简介(系列1)

Android Service是Android重要的组件,在

Android Service简介(系列1)

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实现一个加法


1.定义一个Key供调用

package com.example.service;public class Constans {public static final String KEY="my_key";}
2.发送端
package com.example.service;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity  implements OnClickListener{EditText et1=null;EditText et2=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);et1=(EditText) findViewById(R.id.et1);et2=(EditText) findViewById(R.id.et2);Button button1=(Button) findViewById(R.id.button1);button1.setOnClickListener(this);}@Overridepublic void onClick(View v) { int[] data=new int[2]; data[0]=Integer.parseInt(et1.getText().toString()); data[1]=Integer.parseInt(et2.getText().toString()); Intent intent=new Intent(MainActivity.this,MyAppservice.class); intent.putExtra(Constans.KEY, data); startService(intent);}public void onDestroy(){super.onDestroy(); Intent intent=new Intent(MainActivity.this,MyAppservice.class);stopService(intent);}

3.接收端

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 MyAppservice extends Service{@Overridepublic void onCreate(){}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {int[] data=intent.getIntArrayExtra(Constans.KEY);int num=data[0]+data[1];Toast.makeText(this, num+"", 0).show();return super.onStartCommand(intent, flags, startId);}@Overridepublic IBinder onBind(Intent intent) {return null;}public void onDestroy(){super.onDestroy();Log.d("没问题", "就是他");}}




onCreate仅仅在Sepackage 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 MyAppservice extends Service{








@Override
public void onCreate(){

}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {

int[] data=intent.getIntArrayExtra(Constans.KEY);

int num=data[0]+data[1];

Toast.makeText(this, num+"", 0).show();

return super.onStartCommand(intent, flags, startId);
}




@Override
public IBinder onBind(Intent intent) {

return null;
}
public void onDestroy(){
super.onDestroy();

Log.d("没问题", "就是他");
}
}rvice第一次被startService时候初始化操作一次,随后不管再怎么startService,都不会再onCreate了。
耗时的、后台的、不需要用户交互的操作放在onStartCommand里面处理。需要强调一点,Android的Service并不是一个单独的进程、线程空间,是和Android主线程共享进程空间,这就意味,不要在onStartCommand方法里面阻塞主线程,否则将造成ANR!如果在onStartCommand里面有耗时操作,那么务必将onStartCommand里面的耗时操作代码块放到线程里面做。(注意!次说仅仅针对Service,IntentService和Service机制不同,不存在此问题,但有其自身特点,后面文章再说。)
Service的onDestroy只会被调用一次,那就是Service被stopService或者stopSelf时候。

更多相关文章

  1. SpringBoot 2.0 中 HikariCP 数据库连接池原理解析
  2. 当android里一堆button,用数组来循环建立并且操作每个button
  3. android 去锯齿
  4. Android数据库ContentProvider封装原理
  5. adb命令基本操作
  6. Android(安卓)JNI 之 JNIEnv 解析
  7. android_UIThread 主线程 AsynTask Handler View.post
  8. android service 广播 更新 activity
  9. Android的handler和callback机制

随机推荐

  1. android 使用http协议上传文件
  2. Android实现点击缩略图放大效果
  3. 阅读《Android 从入门到精通》(10)——单项
  4. openssl NDK 交叉编译
  5. QtAndroid详解(1):QAndroidJniObject
  6. 关于Android的Accessibility--自定义View
  7. Android Gradle 配置补充
  8. android Gridview中文API
  9. Android BatteryManager类
  10. Android 放大镜