Android studio 廣播的簡(jiǎn)單使用代碼詳解
1.在布局文件里面加入按鈕,等會(huì)發(fā)送廣播
<?xml version='1.0' encoding='utf-8'?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:app='http://schemas.android.com/apk/res-auto' xmlns:tools='http://schemas.android.com/tools' android:layout_width='match_parent' android:layout_height='match_parent' android:gravity='center' tools:context='.MainActivity3'><Buttonandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:text='發(fā)送廣播'></Button></LinearLayout>
2.使用廣播的第一步當(dāng)然是創(chuàng)建一個(gè)廣播接受者
public class MyBrodestReciver extends BroadcastReceiver{@Overridepublic void onReceive(Context context, Intent intent) { //判斷action是否為添加的action,如果是則toast String action = intent.getAction(); if (action.equals('one_brodest')){Toast.makeText(context, '發(fā)送了一個(gè)廣播', Toast.LENGTH_SHORT).show(); }} }
3.創(chuàng)建完廣播接受者以后注冊(cè)廣播,并且添加一個(gè)action
//新建intentFilter對(duì)象 通過(guò)addAction添加廣播 IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction('one_brodest');
4.然后注冊(cè)一個(gè)廣播
//注冊(cè)廣播 MyBrodestReciver myBrodestReciver = new MyBrodestReciver(); registerReceiver(myBrodestReciver,intentFilter);
5.到這里廣播的注冊(cè)已經(jīng)完成接下來(lái)就是使用了
//做一個(gè)點(diǎn)擊事件發(fā)送一個(gè)廣播 send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction('one_brodest'); sendBroadcast(intent); } });
6.這就是點(diǎn)擊之后的效果,成功發(fā)送了一個(gè)廣播!!!!!!!!!!!!!!!
7.最后一步,銷毀廣播
@Override protected void onDestroy() {super.onDestroy();//銷毀廣播unregisterReceiver(brodestReciver); }
到此這篇關(guān)于Android studio 廣播的簡(jiǎn)單使用的文章就介紹到這了,更多相關(guān)Android studio 廣播內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP常用日期格式化函數(shù) FormatDate()2. ASP新手必備的基礎(chǔ)知識(shí)3. CSS 使用Sprites技術(shù)實(shí)現(xiàn)圓角效果4. chat.asp聊天程序的編寫方法5. 詳解瀏覽器的緩存機(jī)制6. ASP中if語(yǔ)句、select 、while循環(huán)的使用方法7. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?8. HTML中的XML數(shù)據(jù)島記錄編輯與添加9. SXNA RSS Blog 聚合器程序10. 推薦一個(gè)好看Table表格的css樣式代碼詳解
