android之服务

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>

 

posted @ 2016-07-12 16:37  被罚站的树  阅读(192)  评论(0)    收藏  举报