14_Android中Service的使用,关于广播接收者的说明



服务:长期后台运行的没有界面的组件

 

android应用:什么地方需要用到服务?

 

天气预报:后台的连接服务器的逻辑,每隔一段时间获取最新的天气信息

股票显示:后台的连接服务器的逻辑,每隔一段时间获取最新的股票信息

mp3播放器: 后台长期的播放音乐。

 

 

new Thread(){}.start(); 子线程没有界面,也是长期后台运行的。

 

 

android系统进程管理是按照一定的规则的:

1.应用程序一旦被打开 通常情况下关闭(清空任务栈)后进程不会停止。方面下一次快速启动。

带来内存不足的问题。

2.Android系统有一套 内存清理机制。 按照优先级去回收系统的内存。

 

 

进程分为5个等级的优先级:(从高到低)

 

1.Foreground process 前台进程  用户正在玩的应用程序对应的进程

 

2.Visible process 可视进程 用户仍然可以看到这个进程的界面。

 

3.Service process服务进程  应用程序有一个服务组件在后台运行。

 

4.Background process 后台进程  应用程序没有服务在运行 并且最小化 activity onstop

 

5.Empty process 空进程 没有任何运行的activity 任务栈空了

 

 

长期后台运行的组件,不要在activity开启子线程。

应该是创建服务,在服务里面开启子线程。

 

服务的目的:

1.长期后台运行。

2.提高进程的优先级,系统不容易回收掉进程,即便回收了,内存充足的时候,把进程重新创建。


  1. 案例场景:使用一个按钮开启服务,在控制台打印服务启动状况。程序界面如下:

2 Android清单文件如下:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.itheima.testservice"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="19" />

 

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name="com.itheima.testservice.MainActivity"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

        <service android:name="com.itheima.testservice.MyService"></service>

    </application>

 

</manifest>

3 布局文件如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >

 

    <Button

        android:onClick="click"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:layout_centerVertical="true"

        android:text="开启服务"/>

   

</RelativeLayout>

4 MainActivity的代码如下:

package com.itheima.testservice;

 

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

 

public class MainActivity extends Activity {

 

         @Override

         protected void onCreate(Bundle savedInstanceState) {

                   super.onCreate(savedInstanceState);

                   setContentView(R.layout.activity_main);

         }

        

         public void click(View view) {

                    Intent intent = new Intent(this,MyService.class);

                    startService(intent);

         }

}

5 MyService的代码如下:

package com.itheima.testservice;

 

import android.app.Service;

import android.content.Intent;

import android.os.IBinder;

 

public class MyService extends Service {

 

         @Override

         public IBinder onBind(Intent intent) {

                   return null;

         }

 

         //oncreate ondestory onstart onstop onresume onpause

         @Override

         public void onCreate() {

                   System.out.println("服务创建了");

                   super.onCreate();

         }

        

         @Override

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

                   System.out.println("服务器");

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

         }

        

         @Override

         public void onDestroy() {

                   System.out.println("服务器销毁了");

                   super.onDestroy();

         }

}






 


6.关于接受者的说明


四大组件:

Activity

Content provider 内容提供者

Broadcast receiver 广播接受者

Service  服务

 

 

电台:   发送广播

收音机: 接受广播

 

 

android系统下的广播:

电池电量低。

电池充电完毕

短信到来了

程序安装卸载

sd卡卸载 安装

 

 

1.写一个类继承广播接受者

2.在清单文件配置关心的动作

3.一旦广播事件发生了,就会执行广播接受者的onreceive方法

 

 

短信到来的广播接受者 4.4


 




 



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涂作权的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值