Android廣播(BroacastReceiver)與服務(Service)

來源:互聯網
上載者:User

標籤:main   string   建立   代碼   服務   let   實現   roi   short   

BroadcastReceiver可以理解成是一種組件,是默默的在改後台啟動並執行,用於在不同軟體和不同組件之間的傳遞,無法被使用者感知,因為他在系統的內部工作,BroadcastReceiver被稱為廣播。

  一、廣播的註冊有兩種方式: 

    1、動態註冊,使用Java代碼進行註冊

    2、靜態註冊,需要在AndroidMainfest進行註冊

  二、我們先來講解下如何靜態註冊:

    1、我們先來建立一個繼承BroadcastReceiver的類,實現onReceive方法,為了更好的體驗,我們在這個方法裡面彈一個多士

     onReceive在接受到廣播後會觸發

 1 public class MsgService extends BroadcastReceiver { 2  3     private String ACTION_SGC = "android.intent.action.BOOT_COMPleTED"; 4  5     @Override 6     public void onReceive(Context context, Intent intent) { 7         if(ACTION_SGC.equals(intent.getAction())){ 8             Toast.makeText(context,"靜態廣播",Toast.LENGTH_SHORT).show(); 9         }10     }11 }

    2、這是需要在AndroidMainfest中進行註冊

 1 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission> 2      3     <application 4         android:allowBackup="true" 5         android:icon="@mipmap/ic_launcher" 6         android:label="@string/app_name" 7         android:roundIcon="@mipmap/ic_launcher_round" 8         android:supportsRtl="true" 9         android:theme="@style/AppTheme">10         <activity android:name=".MainActivity">11             <intent-filter>12                 <action android:name="android.intent.action.MAIN" />13 14                 <category android:name="android.intent.category.LAUNCHER" />15             </intent-filter>16         </activity>17         <receiver android:name=".MsgService">18             <intent-filter>19                 <action android:name="android.intent.action.BOOT_COMPleTED"></action>20             </intent-filter>21         </receiver>22     </application>

    註冊的代碼是在17行到20行,別忘了還要寫上許可權,許可權就是第一個行的許可權

    3、如何使用廣播

 1 public class MainActivity extends AppCompatActivity { 2  3     @Override 4     protected void onCreate(Bundle savedInstanceState) { 5         super.onCreate(savedInstanceState); 6         setContentView(R.layout.activity_main); 7         init(); 8     } 9 10     private void init() {11         findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {12             @Override13             public void onClick(View v) {14                 //這裡的意圖必須使用和其他一樣的意圖android.intent.action.BOOT_COMPleTED 如果使用的不一樣,將會出現錯誤15                 sendBroadcast(new Intent("android.intent.action.BOOT_COMPleTED"));16             }17         });18     }19 }

 

 

      

  

  

Android廣播(BroacastReceiver)與服務(Service)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.