android中的进程优先级


  • 前台进程

    • 拥有一个正在与用户交互的Activity(onResume方法被调用)
    • 与一个前台Activity绑定的服务
    • 服务调用了startForeground
    • onCreate(), onStart(),onDestroy()方法正在被调用的服务
    • onReceiver方法被调用的广播接受者
  • 可见进程:拥有一个可见但没有焦点的Activity(onPause方法被调用)
  • 服务进程:启动了一个service并调用了startService方法
  • 后台进程:拥有一个不可见的Activity(onStop方法被调用)的进程
  • 空进程:没有拥有任何活动的应用组件的进程

前台进程优先级最高,空进程优先级最低。

Service


因为服务进程的优先级相对比较高,所以我们需要将一些需要长期运行的程序(音乐播放、文件下载)写在服务中,这样即使我们Activity关闭了,我们的应用也能正常运行。

生命周期


  • 服务对象被创建时会调用onCreate(),采用单例模式
  • 服务开始运行时会调用onStartCommand方法,多次开启同一个服务该方法会被调用多次,而onCreate方法只被调用一次。
  • 服务对象被销毁的时候onDestroy方法会被调用

显示启动


Service和Activity一样,有显示启动和隐式启动之分。显示启动就是加载类文件。

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="开启服务"
android:onClick="click"/>
<Button
android:id="@+id/stop"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关闭服务"
android:onClick="click1"/>
</LinearLayout>

Activity

package xidian.dy.com.chujia;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast; public class MainActivity extends AppCompatActivity {
Intent service;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); } public void click(View v){
Toast.makeText(this,"开启服务",Toast.LENGTH_SHORT).show();
service = new Intent(this, MyService.class);
startService(service);
} public void click1(View v){
if(service != null)
stopService(service);
}
}

service(该服务中并没有运行具体的程序)

package xidian.dy.com.chujia;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable; /**
* Created by dy on 2016/7/12.
*/
public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}

 清单文件(在清单文件中注册service组件)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xidian.dy.com.chujia">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="主界面">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService" />
</application>
</manifest>

最新文章

  1. 分布式数据库中的Paxos 算法
  2. HDOJ 1690
  3. Java多线程之join
  4. Android IOS WebRTC 音视频开发总结(十五)-- 培训课程大纲
  5. phonegap/cordova 启动页面
  6. script是什么
  7. 在ubuntu上面配置nginx实现反向代理
  8. 天坑 之 java web servlet+jsp项目 配置后 404 (MyEclipse转eclipse)
  9. Js特效总结
  10. 【转】UNIX时间戳与.net日期类的转换
  11. 大数据之Flume
  12. call_user_func 和 call_user_func_array用法
  13. 在同一台电脑上部署多个tomcat服务器
  14. 【转载】 A* 寻路算法 (个人认为最详细,最通俗易懂的一个版本)
  15. 【UVA1401】Remember the Word Trie+dp
  16. app后端设计-- 数据库分表
  17. form提交方式Get与Post详解
  18. 关于删除MySQL Logs的一点记录
  19. Xiaocms验证码绕过分析
  20. scrollbar样式

热门文章

  1. Java api 入门教程 之 JAVA的文件操作
  2. Activity中使用Intent实现页面跳转与参数的传递(转)
  3. CentOS 6.3下rsync服务器的安装与配置
  4. [转]在EntityFramework6中执行SQL语句
  5. Machine Learning Algorithms Study Notes(1)--Introduction
  6. HTML 学习笔记 CSS样式(文本)
  7. APIO2013 tasksauthor
  8. Meet python: little notes 4 - high-level characteristics
  9. P3369 【模板】普通平衡树(Treap/SBT)
  10. HTML5添加 video 视频标签后仍然无法播放的解决方法 IIS添加MIEI类型