欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Android學(xué)習(xí)之Broadcast的簡(jiǎn)單使用

本文實(shí)例為大家分享了Android學(xué)習(xí)之Broadcast的使用方法,供大家參考,具體內(nèi)容如下

網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)的關(guān)注點(diǎn)不是能為您做些什么網(wǎng)站,而是怎么做網(wǎng)站,有沒有做好網(wǎng)站,給成都創(chuàng)新互聯(lián)公司一個(gè)展示的機(jī)會(huì)來證明自己,這并不會(huì)花費(fèi)您太多時(shí)間,或許會(huì)給您帶來新的靈感和驚喜。面向用戶友好,注重用戶體驗(yàn),一切以用戶為中心。

實(shí)現(xiàn)開機(jī)啟動(dòng)提示網(wǎng)絡(luò)的廣播

package com.example.luobo.broadcasttest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

 private IntentFilter intentFilter;
 private NetworkChangeReceiver networkChangeReceiver;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  intentFilter = new IntentFilter();//創(chuàng)建一個(gè)過濾器實(shí)例
  intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");//添加接收CONNECTIVITY_CHANGE消息
  networkChangeReceiver = new NetworkChangeReceiver();
  registerReceiver(networkChangeReceiver,intentFilter);
 }

 @Override
 protected void onDestroy() {
  super.onDestroy();
  unregisterReceiver(networkChangeReceiver);
 }

 class NetworkChangeReceiver extends BroadcastReceiver{
  @Override
  public void onReceive(Context context, Intent intent) {
   ConnectivityManager connectivityManager = (ConnectivityManager)
     getSystemService(Context.CONNECTIVITY_SERVICE);//通過此方法獲取ConnectivityManager實(shí)例
   NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();//調(diào)用實(shí)例connectivityManager的getActiveNetworkInfo()方法獲取NetworkInfo實(shí)例
   if (networkInfo != null && networkInfo.isAvailable()){
    Toast.makeText(context,"Network is available",Toast.LENGTH_SHORT).show();
   }else {
    Toast.makeText(context,"Network is unavailable",Toast.LENGTH_SHORT).show();
   }
  }
 }
}

創(chuàng)建BootCompleteReceiver類

右擊com.example.luobo.broadcasttest,New->Other->Broadcast,輸入名字BootCompleteReceiver,勾選Enable,Exported,重寫onReceive()方法。由于使用的是快捷方式創(chuàng)建的類,所需權(quán)限會(huì)在AndroidManifest.xml中自動(dòng)注冊(cè)。標(biāo)簽為receiver,但是還不夠修改。

package com.example.luobo.broadcasttest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class BootCompleteReceiver extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {
  Toast.makeText(context,"Boot Complete",Toast.LENGTH_SHORT).show();
 }
}

在AndroidMaifest.xml注冊(cè)權(quán)限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.luobo.broadcasttest">

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />//注冊(cè)接收網(wǎng)絡(luò)消息廣播
 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>//注冊(cè)接收開機(jī)啟動(dòng)廣播
 <application
  android: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">
  <activity android:name=".MainActivity">
   <intent-filter>
    <action android:name="android.intent.action.MAIN" />

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

  <receiver
   android:name=".BootCompleteReceiver"
   android:enabled="true"
   android:exported="true">
   <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED"/>//開機(jī)時(shí)系統(tǒng)會(huì)發(fā)一條此廣播
   </intent-filter>
  </receiver>
 </application>

</manifest>

上述在AndroidMainfest.xml中注冊(cè)接收廣播消息屬于靜態(tài)注冊(cè),在OnCreate()中注冊(cè)的接收廣播屬于動(dòng)態(tài)注冊(cè)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

標(biāo)題名稱:Android學(xué)習(xí)之Broadcast的簡(jiǎn)單使用
文章分享:http://chinadenli.net/article24/gepjce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航ChatGPT搜索引擎優(yōu)化微信公眾號(hào)App設(shè)計(jì)關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司