[android] 代碼註冊廣播接收者&利用廣播調用服務的方法

來源:互聯網
上載者:User

標籤:

利用廣播調用服務裡面的方法,間接的方式調用服務內部的方法,與現實中差不多,請媒體曝光

 

主介面裡面

在介面建立的時候開啟一下服務普通的startService()方法

發送一條廣播出去

擷取Intent對象,通過new出來

調用Intent對象的setAction()方法,參數:一般就是包名

調用sendBroadcast(intebt)方法,發送廣播,參數:Intent對象

 

服務裡面

建立一個MyService類繼承系統的Service類

添加一個自訂的服務的方法callServiceMethod(),我們目的要調用的方法

 

建立一個內部類MyReceiver繼承系統的Receiver類

廣播接收者既能通過資訊清單檔註冊,也可以通過代碼的方式註冊,這是四大組件中唯一的特殊的一個

 

在MyService中建立一個MyReceiver的成員變數

在服務建立的時候,註冊廣播接收者,在onCreate()方法裡面

調用registerReceiver(receiver,filter)方法,註冊這個廣播接收者,參數:receiver是BroadcastReceiver對象,通過new MyReceiver()來建立,filter是IntentFilter對象

擷取IntentFilter對象,通過new出來

調用IntentFilter對象的addAction(action)方法,意圖過濾器對象添加監聽動作,上面發送的那個動作

 

當接收者收到訊號的時候,會調用MyReceiver對象的onReceive()方法,在這個方法裡面可以去調用服務的私人方法,比如callServiceMethod()

 

當服務銷毀的時候,解除註冊的廣播接收者,在onDestroy()方法裡面,調用unResisterReceiver(receiver),參數:廣播接收者對象

 

MainActivity.java

 

package com.tsh.broadcallservice;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //開啟服務        Intent intent=new Intent(this,MyService.class);        startService(intent);    }    //廣播調用服務    public void call(View v){        Intent intent=new Intent();        intent.setAction("com.tsh.broadcallservice");        sendBroadcast(intent);    }}

 

MyService.java

package com.tsh.broadcallservice;import android.app.Service;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.IBinder;import android.widget.Toast;public class MyService extends Service {    private MyReceiver receiver;    @Override    public IBinder onBind(Intent intent) {        return null;    }    //服務建立的時候註冊廣播接收者    @Override    public void onCreate() {        super.onCreate();        receiver=new MyReceiver();        IntentFilter filter=new IntentFilter();        filter.addAction("com.tsh.broadcallservice");        registerReceiver(receiver, filter);    }    //服務裡面的一個方法    public void callServiceMethod(){        Toast.makeText(getApplicationContext(), "廣播調用服務啦", 0).show();    }        //內部類實現廣播接收者    private class MyReceiver extends BroadcastReceiver{        @Override        public void onReceive(Context context, Intent intent) {            callServiceMethod();        }    } }

 

[android] 代碼註冊廣播接收者&利用廣播調用服務的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.