Android-广播接收者简介
生活随笔
收集整理的這篇文章主要介紹了
Android-广播接收者简介
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目? ?錄
廣播接收者簡介
廣播接收者入門
廣播接收者的創建
?靜態注冊
動態注冊
實戰演練——攔截史迪仔電話
自定義廣播
實戰演練——拯救史迪仔
廣播類型
實戰演練——攔截史迪仔廣播
廣播接收者簡介
廣播接收者入門
廣播接收者的創建
package cn.lwx.broadcastreceiver;import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent;public class MyReceiver extends BroadcastReceiver {@Override // 在該方法中實現廣播接收者的相關操作public void onReceive(Context context, Intent intent) {// TODO: This method is called when the BroadcastReceiver is receiving// an Intent broadcast.throw new UnsupportedOperationException("Not yet implemented");}}?靜態注冊
動態注冊
package cn.lwx.broadcastreceiver;import androidx.appcompat.app.AppCompatActivity;import android.content.IntentFilter; import android.os.Bundle;public class MainActivity extends AppCompatActivity {private MyReceiver receiver;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);receiver = new MyReceiver(); // ctrl+alt+f 快速將局部變量提升為 成員變量//實例化過濾器并設置要過濾的廣播String action = "android.provider.Telephony.SMS_RECEIVED";IntentFilter intentFilter = new IntentFilter(action);registerReceiver(receiver, intentFilter); // 注冊廣播}//當Activity銷毀時,取消注冊protected void onDestroy() {super.onDestroy();unregisterReceiver(receiver);}}實戰演練——攔截史迪仔電話
項目工程---源碼:https://gitee.com/lwx001/BroadcastReceiver
可用gitee直接拷貝。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="cn.lwx.broadcastreceiver"><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><!--注冊廣播接收者--><receiverandroid:name=".MyReceiver"android:enabled="true"android:exported="true"><intent-filter><action android:name="android.intent.action.NEW_OUTGOING_CALL"/></intent-filter></receiver><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application><uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/> </manifest>自定義廣播
實戰演練——拯救史迪仔
廣播類型
實戰演練——攔截史迪仔廣播
總結
以上是生活随笔為你收集整理的Android-广播接收者简介的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows 使用小技巧
- 下一篇: 数学建模论文写作小技巧分享