日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

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

發布時間:2024/9/27 Android 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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.提高進程的優先級,系統不容易回收掉進程,即便回收了,內存充足的時候,把進程重新創建。


  • 案例場景:使用一個按鈕開啟服務,在控制臺打印服務啟動狀況。程序界面如下:

  • 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


    ?




    ?

     

    總結

    以上是生活随笔為你收集整理的14_Android中Service的使用,关于广播接收者的说明的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。