1. 程式人生 > >Android Studio 學習(三) 廣播

Android Studio 學習(三) 廣播

else set local activity .... order ... void test

動態註冊監聽網絡變化

  • 創建intentFilter 並addAction 代表了監聽哪個廣播
  • 然後使用registerReceiver()方法 將intentFilter 與 自己創建的監聽器 傳進去
  • 動態註冊的 需要unregisterReciver()
  • 權限需要在Mainfest中聲明 <uses-permission android :name "android.permission.ACCESS_NETWORK.STATE"/>

public class MainActivity extends AppCompatActivity {
private IntentFilter intentFilter;
private NetworkChangedReceiver networkChangedReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentFilter = new IntentFilter();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
networkChangedReceiver = new NetworkChangedReceiver();
registerReceiver(networkChangedReceiver,intentFilter);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(networkChangedReceiver);
}
class NetworkChangedReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive (Context context,Intent intent)
    {
        ConnectivityManager connectivityManager =
                (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if(networkInfo!=null && networkInfo.isAvailable())
        {
            Toast.makeText(context,"good",Toast.LENGTH_LONG).show();
        }
        else
        {
            Toast.makeText(context,"bad",Toast.LENGTH_LONG).show();
        }

    }
}

}

靜態註冊實現開機啟動

  • new-> other->broadcast receiver 手動創建廣播
  • exported 允許此廣播接收本程序外的廣播

之後再Mainfest中聲明權限
並說明要監聽的廣播

發送廣播

  • Intent intent = new Intent("com....")
  • sendBroadcast(intent);
  • sendOrderedBroadcast(intent,null); 發送有序廣播

  • <intent- filter android :priority="100">//設置廣播優先級
  • abortBroadcast() 截斷廣播

本地廣播

public class MainActivity extends AppCompatActivity {
private IntentFilter intentFilter;
private LocalReceiver localReceiver;
private LocalBroadcastManager localBroadcastManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //得到本地廣播管理器的實例
    localBroadcastManager = LocalBroadcastManager.getInstance(this);

    //動態註冊本地廣播接收器
    intentFilter = new IntentFilter();
    intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST");
    localReceiver = new LocalReceiver();
    localBroadcastManager.registerReceiver(localReceiver, intentFilter);

    //發送本地廣播
    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
            localBroadcastManager.sendBroadcast(intent);
        }
    });
}

@Override
protected void onDestroy() {
    super.onDestroy();

    //取消動態網絡變化廣播接收器的註冊
    localBroadcastManager.unregisterReceiver(localReceiver);
}


class LocalReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "received local broadcast", Toast.LENGTH_SHORT).show();
    }
}

}

Android Studio 學習(三) 廣播