青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

xiaoguozi's Blog
Pay it forword - 我并不覺的自豪,我所嘗試的事情都失敗了······習慣原本生活的人不容易改變,就算現狀很糟,他們也很難改變,在過程中,他們還是放棄了······他們一放棄,大家就都是輸家······讓愛傳出去,很困難,也無法預料,人們需要更細心的觀察別人,要隨時注意才能保護別人,因為他們未必知道自己要什么·····

廣播接收器:

 

廣播接收者(BroadcastReceiver)用于監聽系統事件或應用程序事件,通過調用Context.sendBroadcast()Context.sendOrderedBroadcast()可以向系統發送廣播意圖,通過廣播一個意圖(Intent)可以被多個廣播接收者所接收,從而可以在不用修改原始的應用程序的情況下,讓你對事件作出反應。

       其中Context.sendBroad()主要是用來廣播無序事件(也被稱為有序廣播 Normal broadcast),即所有的接收者在理論上是同時接收到事件,同時執行的,對消息傳遞的效率而言這是比較好的做法。而Context.sendOrderedBroadcast()方法用來向系統廣播有序事件(Ordered broadcast),接收者按照在Manifest.xml文件中設置的接收順序依次接收Intent,順序執行的,接收的優先級可以在系統配置文件中設置(聲明在intent-filter元素的android:priority屬性中,數值越大優先級別越高,其取值范圍為-10001000。當然也可以在調用IntentFilter對象的setPriority()方法進行設置)。對于有序廣播而言,前面的接收者可以對接收到得廣播意圖(Intent)進行處理,并將處理結果放置到廣播意圖中,然后傳遞給下一個接收者,當然前面的接收者有權終止廣播的進一步傳播。如果廣播被前面的接收者終止后,后面的接收器就再也無法接收到廣播了。

 

廣播接收器(Broadcaset)運行的線程:

       無論對于有序廣播還是無序廣播,廣播接收器默認都是運行在主線程中的(main線程,即UI線程)。可以通過在程序中使用registerReceiver(receiver, filter, broadcastPermission, scheduler)方法中的最后一個參數指定要運行的廣播接收器的線程。也可以在Manifest.xml文件中設置(Intent-filter標簽中設置android:process)

 

無序廣播(Normal Broadcast)

       基本步驟:寫一個類繼承BroadcastReceiver,并重寫onReceive方法,而后在AndroidManifest.xml文中中進行配置,或者直接在代碼中注冊。

      

下面是一個廣播接收器的Demo(用于發送和接收短信)

      

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.telephony.SmsMessage;

import android.util.Log;

 

public class ReceivingSMSReceiver extends BroadcastReceiver {

    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

    private static final String TAG = "ReceivingSMSReceiver";

 

    @Override

    public void onReceive(Context context, Intent intent) {

       if (intent.getAction().equals(SMS_RECEIVED)) {

           Bundle bundle = intent.getExtras();

           if (bundle != null) {

              Object[] pdus = (Object[]) bundle.get("pdus");

              SmsMessage[] messages = new SmsMessage[pdus.length];

              for (int i = 0; i < pdus.length; i++)

                  messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);

              for (SmsMessage message : messages) {

                  String msg = message.getMessageBody();

                  Log.i(TAG, msg);

                  String to = message.getOriginatingAddress();

                  Log.i(TAG, to);

              }

           }

       }

    }

}

 

AndroidManifest.xml文件中的<application>節點里對接收到短信的廣播Intent進行注冊:

<receiver android:name=". ReceivingSMSReceiver">

<intent-filter>

<action android:name="android.provider.Telephony.SMS_RECEIVED"/>

</intent-filter>

</receiver>

AndroidManifest.xml文件中添加以下權限:

<uses-permission android:name="android.permission.RECEIVE_SMS"/><!-- 接收短信權限 -->

 

該廣播接收器將首先得到本機收到的短信,可以對短信內容進行過濾。

 

在模擬器中運行該工程。

 

建立一個新的Android工程,新建一個Activity用來發送短信:

 

import android.app.Activity;

import android.app.PendingIntent;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

import android.os.Bundle;

import android.telephony.SmsManager;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

 

public class SMSSender extends Activity {

    private static final String TAG = "SMSSender";

    Button send = null;

    EditText address = null;

    EditText content = null;

    private mServiceReceiver mReceiver01, mReceiver02;

    private static String SEND_ACTIOIN = "SMS_SEND_ACTION";

    private static String DELIVERED_ACTION = "SMS_DELIVERED_ACTION";

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.main);

       address = (EditText) findViewById(R.id.address);

       content = (EditText) findViewById(R.id.content);

       send = (Button) findViewById(R.id.send);

       send.setOnClickListener(new View.OnClickListener() {

 

           @Override

           public void onClick(View v) {

              // TODO Auto-generated method stub

              String mAddress = address.getText().toString().trim();

              String mContent = content.getText().toString().trim();

              if ("".equals(mAddress) || "".equals(mContent)) {

                  Toast.makeText(SMSSender.this, "發送地址為空或內容為空!",

                         Toast.LENGTH_LONG).show();

                  return;

              }

              SmsManager smsManager = SmsManager.getDefault();

              try {

                  Intent send_Intent = new Intent(SEND_ACTIOIN);

                  Intent deliver_Intent = new Intent(DELIVERED_ACTION);

                  PendingIntent mSend = PendingIntent.getBroadcast(

                         getApplicationContext(), 0, send_Intent, 0);

                  PendingIntent mDeliver = PendingIntent.getBroadcast(

                         getApplicationContext(), 0, deliver_Intent, 0);

                  smsManager.sendTextMessage(mAddress, null, mContent, mSend,

                         mDeliver);

              } catch (Exception e) {

                  e.printStackTrace();

              }

           }

       });

    }

 

    @Override

    protected void onResume() {

       // TODO Auto-generated method stub

       IntentFilter mFilter01;

       mFilter01 = new IntentFilter(SEND_ACTIOIN);

       mReceiver01 = new mServiceReceiver();

       registerReceiver(mReceiver01, mFilter01);

       mFilter01 = new IntentFilter(DELIVERED_ACTION);

       mReceiver02 = new mServiceReceiver();

       registerReceiver(mReceiver02, mFilter01);

       super.onResume();

    }

 

    @Override

    protected void onPause() {

       // TODO Auto-generated method stub

       unregisterReceiver(mReceiver01);

       unregisterReceiver(mReceiver02);

       super.onPause();

    }

 

    public class mServiceReceiver extends BroadcastReceiver {

       @Override

       public void onReceive(Context context, Intent intent) {

           // TODO Auto-generated method stub

           if (intent.getAction().equals(SEND_ACTIOIN)) {

              try {

                  switch (getResultCode()) {

                  case Activity.RESULT_OK:

                     break;

                  case SmsManager.RESULT_ERROR_GENERIC_FAILURE:

                     break;

                  case SmsManager.RESULT_ERROR_RADIO_OFF:

                     break;

                  case SmsManager.RESULT_ERROR_NULL_PDU:

                     break;

                  }

              } catch (Exception e) {

                  e.getStackTrace();

              }

           } else if (intent.getAction().equals(DELIVERED_ACTION)) {

              try {

                  switch (getResultCode()) {

                  case Activity.RESULT_OK:

                     break;

                  case SmsManager.RESULT_ERROR_GENERIC_FAILURE:

                     break;

                  case SmsManager.RESULT_ERROR_RADIO_OFF:

                     break;

                  case SmsManager.RESULT_ERROR_NULL_PDU:

                     break;

                  }

              } catch (Exception e) {

                  e.getStackTrace();

              }

           }

       }

    }

}

 

界面布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical" android:layout_width="fill_parent"

    android:layout_height="fill_parent">

    <TextView android:layout_width="fill_parent"

       android:layout_height="wrap_content" android:text="發送短信" />

    <TextView android:text="收信人地址:" android:id="@+id/textView1"

       android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>

    <EditText android:text="" android:layout_width="match_parent"

       android:id="@+id/address" android:layout_height="wrap_content"></EditText>

    <TextView android:text="短信內容:" android:id="@+id/textView2"

       android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>

    <EditText android:text="" android:layout_width="match_parent"

       android:id="@+id/content" android:layout_height="wrap_content"></EditText>

    <Button android:text="發送" android:id="@+id/send"

       android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

</LinearLayout>

 

AndroidManifest.xml文件中增加對該Activity的配置:

<activity android:name=".SMSSender" android:label="@string/app_name">

           <intent-filter>

              <action android:name="android.intent.action.MAIN" />

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

           </intent-filter>

       </activity>

AndroidManifest.xml文件中添加以下權限:

<uses-permission android:name="android.permission.SEND_SMS"/><!—發送短信權限 -->

 

在另一個模擬器中運行該工程。

 

由于本文的重點內容并不是發送和接收短信,所以,對短信發送和接收的內容并沒有詳細解釋。如果對短信收發內容不熟悉的朋友,可以查閱相關文檔。

 

 

有序廣播:

 

接收器1

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

 

public class BroadcastTest2 extends BroadcastReceiver {

 

    @Override

    public void onReceive(Context arg0, Intent arg1) {

       System.out.println("運行線程:    " + Thread.currentThread().getId() + "   "

              + Thread.currentThread().getName());

       System.out.println("廣播意圖的動作: " + arg1.getAction());

       Bundle bundle = new Bundle();

       bundle.putString("test", "zhongnan");

       setResultExtras(bundle);

    }

}

 

接收器2

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

 

public class BroadcastTest1 extends BroadcastReceiver {

 

    @Override

    public void onReceive(Context arg0, Intent arg1) {

       System.out.println("運行線程:    " + Thread.currentThread().getId() + "   "

              + Thread.currentThread().getName());

       System.out.println("廣播意圖的動作: " + arg1.getAction());

       Bundle bundle = getResultExtras(false);

      

       if (bundle == null) {

           System.out.println("沒有得到上次傳遞的數據");

       } else {

           System.out.println("測試: " + bundle.getString("test"));

       }

    }

}

 

Activity,用來發送廣播:

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

 

public class MainActivity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.main);

       this.sendOrderedBroadcast(new Intent(

              "android.provier.zhongnan.broadcast"), null);

    }

}

 

AndroidManifest.xml文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.zhongnan.bc" android:versionCode="1" android:versionName="1.0">

    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">

       <activity android:name=".MainActivity" android:label="@string/app_name">

           <intent-filter>

              <action android:name="android.intent.action.MAIN" />

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

           </intent-filter>

       </activity>

       <receiver android:name=".BroadcastTest1">

           <intent-filter>

              <action android:name="android.provier.zhongnan.broadcast"

                  android:priority="400" />

           </intent-filter>

       </receiver>

       <receiver android:name=".BroadcastTest2">

           <intent-filter>

              <action android:name="android.provier.zhongnan.broadcast"

                  android:priority="500" />

           </intent-filter>

       </receiver>

    </application>

</manifest>

 

MainActivity中發送有序廣播Context.sendOrderedBroadcast(),由于BroadcastTest2中設置的接收優先級比較高,所以在BroadcastTest2中將首先接收到廣播意圖,可以在BroadcastTest2中對該廣播意圖進行處理,可以加入處理后的數據給后面的接收器使用,也可以在該接收器中終止廣播的進一步傳遞。在廣播中加入處理后的數據使用setResultExtras(Bundle bundle)方法,關于Bundle類,類似于HashMap,不熟悉的朋友可以參考文檔,或者查看我的另一篇博客。在后面的接收器中使用getResultExtras(boolean flag)接收前面的接收器存放的數據,其中的boolean參數含義為:true代表如果前面的接收器沒有存放數據,則自動創建一個空的Bundle對象,false則表示如果前面的接收器如果沒有存放任何數據則返回null

 

廣播接收器中權限的定義:

    在發送廣播時,無論是無序廣播(Normal Broadcast)還是有序廣播(Ordered Broadcast)都有類似的方法: sendBroadcast (Intent intent, String receiverPermission), sendOrderedBroadcast (Intent intent, String receiverPermission)。其中第二個參數是設置權限,即接收器必須具有相應的權限才能正常接收到廣播。

 

下面是在上述例子的基礎上添加自定義權限的例子:

         接收器1

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

 

public class BroadcastTest2 extends BroadcastReceiver {

 

    @Override

    public void onReceive(Context arg0, Intent arg1) {

       System.out.println("運行線程:    " + Thread.currentThread().getId() + "   "

              + Thread.currentThread().getName());

       System.out.println("廣播意圖的動作: " + arg1.getAction());

       Bundle bundle = new Bundle();

       bundle.putString("test", "zhongnan");

       setResultExtras(bundle);

    }

}

 

接收器2

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

 

public class BroadcastTest1 extends BroadcastReceiver {

 

    @Override

    public void onReceive(Context arg0, Intent arg1) {

       System.out.println("運行線程:    " + Thread.currentThread().getId() + "   "

              + Thread.currentThread().getName());

       System.out.println("廣播意圖的動作: " + arg1.getAction());

       Bundle bundle = getResultExtras(false);

      

       if (bundle == null) {

           System.out.println("沒有得到上次傳遞的數據");

       } else {

           System.out.println("測試: " + bundle.getString("test"));

       }

    }

}

 

Activity,用來發送廣播:

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

 

public class MainActivity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.main);

       this.sendOrderedBroadcast(new Intent(

              "android.provier.zhongnan.broadcast"), "xzq.zhongnan.test");

    }

}

代碼中與上述例子最大的差別在于MainActivity中發送廣播的代碼: this.sendOrderedBroadcast(new Intent(

              "android.provier.zhongnan.broadcast"), "xzq.zhongnan.test")增加了自定義的一個權限。

AndroidManifest文件中配置自定義的權限:

    <permission android:protectionLevel="normal" android:name="xzq.zhongnan.test"></permission>

關于如何在工程中自定義權限請查閱相關文檔,或查看我的另一篇博客。

相應的,接收器中必須設置接收權限:

<uses-permission android:name="xzq.zhongnan.test"></uses-permission>

這樣,接收器就可以正確接收到廣播了。

 

 

 

 

另外,上述程序已講到,BroadcastReceiver是允許在主線程中的,所以,在onReceive方法中執行的代碼,運行時間不能超過5s,否則將報出程序沒有相應的異常,如果要執行的代碼運行的時間比較長,可以使用Service組件。

轉自:
http://blog.csdn.net/zhongnan09/article/details/6552632

posted on 2012-06-07 23:09 小果子 閱讀(2851) 評論(0)  編輯 收藏 引用 所屬分類: Android & Ios
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美高清在线视频观看不卡| 亚洲欧洲视频在线| 正在播放日韩| 亚洲综合色在线| 欧美电影电视剧在线观看| 欧美福利网址| 欧美一区二区精品| 欧美精品一区二区久久婷婷| 在线高清一区| 国产精品一区二区三区观看| 久久一区二区三区四区| 欧美图区在线视频| 亚洲第一福利视频| 亚洲精品日产精品乱码不卡| 午夜精品免费在线| 亚洲一区自拍| 卡一卡二国产精品| 国产亚洲福利社区一区| 在线视频你懂得一区| 亚洲伊人伊色伊影伊综合网| 亚洲第一久久影院| 久久久久88色偷偷免费| 91久久极品少妇xxxxⅹ软件| 午夜在线一区| 久久综合电影一区| 国产一区二区三区在线观看免费视频| 一本色道久久88精品综合| 亚洲美女尤物影院| 美女图片一区二区| 亚洲新中文字幕| 久久亚洲精选| 国产一区二区三区在线观看视频 | 亚洲黑丝在线| 久久av资源网站| 久久亚洲欧美| 欧美一区2区三区4区公司二百 | 久久久人成影片一区二区三区 | 香蕉乱码成人久久天堂爱免费| 亚洲精品乱码久久久久久按摩观| 久久免费国产| 亚洲国产成人午夜在线一区| 久久免费视频在线观看| 欧美一级日韩一级| 国产精品福利网站| 欧美日韩天堂| 欧美少妇一区二区| 一区二区免费在线视频| 亚洲精品少妇网址| 欧美日韩黄色大片| 亚洲日本免费| 亚洲精品日韩一| 欧美性一区二区| 亚洲欧美欧美一区二区三区| 欧美高清视频www夜色资源网| 美国三级日本三级久久99| 亚洲片在线资源| 久久综合伊人77777尤物| 国产美女精品在线| 午夜久久电影网| 在线视频你懂得一区| 亚洲免费影视| 欧美va日韩va| 一区二区不卡在线视频 午夜欧美不卡在| 亚洲第一页自拍| 亚洲欧美视频一区二区三区| 亚洲丝袜av一区| 影音先锋亚洲精品| 中日韩高清电影网| 亚洲高清成人| 亚洲综合社区| 宅男66日本亚洲欧美视频| 欧美在线视频一区| 亚洲视频网站在线观看| 老司机精品视频网站| 亚洲一区在线播放| 欧美人在线视频| 老司机精品视频网站| 国产精品久久久久毛片软件| 亚洲大胆美女视频| 黄色亚洲大片免费在线观看| 亚洲午夜视频在线| 国产精品99久久久久久白浆小说| 久久综合久久综合久久综合| 久久激情五月激情| 国产精品一区二区男女羞羞无遮挡| 91久久久久久久久| 亚洲国产视频a| 久久久精品性| 久久久亚洲精品一区二区三区 | 久久久成人精品| 欧美亚男人的天堂| 亚洲六月丁香色婷婷综合久久| 亚洲国产成人av好男人在线观看| 欧美中文字幕在线视频| 久久久99国产精品免费| 国产欧美一区二区精品性色| 亚洲永久免费视频| 欧美亚洲尤物久久| 国产欧美日韩精品专区| 午夜精品福利视频| 久久久久在线| 亚洲福利国产精品| 麻豆精品在线播放| 亚洲经典在线看| 中文亚洲字幕| 国产精品免费视频xxxx| 亚洲欧美自拍偷拍| 性久久久久久久| 国产一区香蕉久久| 久久免费黄色| 亚洲精品一区二区网址| 亚洲欧美国产毛片在线| 国产精品一区免费观看| 欧美一区二区在线免费播放| 欧美精品尤物在线| 亚洲特级毛片| 欧美午夜理伦三级在线观看| 亚洲一区二区三区四区五区黄 | 亚洲欧美日韩中文视频| 久久久精品动漫| 亚洲欧洲一区二区天堂久久| 欧美日韩xxxxx| 亚洲男女自偷自拍| 免费在线视频一区| 在线亚洲观看| 国产真实乱偷精品视频免| 免费久久99精品国产自| 亚洲调教视频在线观看| 六月天综合网| 亚洲免费在线精品一区| 韩国免费一区| 欧美色欧美亚洲另类二区 | 国产综合精品一区| 你懂的亚洲视频| 亚洲综合社区| 亚洲国产精品成人精品| 欧美一区二区三区日韩视频| 亚洲成色999久久网站| 欧美日韩调教| 欧美一级片一区| 亚洲精品视频免费观看| 久久国产手机看片| aⅴ色国产欧美| 激情成人综合网| 欧美日韩国产首页| 久久综合激情| 亚洲欧美日韩在线不卡| 最新精品在线| 免费观看在线综合色| 午夜精品一区二区三区四区| 亚洲国产一区二区三区青草影视 | 欧美视频二区| 久久精品国产精品亚洲| 日韩图片一区| 欧美黄色免费网站| 久久精品国产亚洲一区二区| 亚洲视频在线二区| 亚洲国产高潮在线观看| 国产午夜精品麻豆| 欧美私人啪啪vps| 欧美激情亚洲| 欧美成人在线影院| 麻豆成人小视频| 久久久www成人免费精品| 亚洲一区二区三区免费在线观看| 亚洲欧洲精品成人久久奇米网 | 久久久www免费人成黑人精品| 9色国产精品| 亚洲精品精选| 亚洲二区免费| 欧美成人一区二区三区在线观看 | 久久av一区二区三区漫画| 在线视频精品一区| 一区二区三区视频在线| 亚洲免费观看视频| 亚洲国产日韩综合一区| 亚洲电影网站| 欧美成人在线免费视频| 免费高清在线一区| 欧美大香线蕉线伊人久久国产精品| 亚洲永久精品国产| 国产精品久久7| 欧美午夜电影在线| 欧美亚韩一区| 国产精品视频xxx| 国产精品亚洲成人| 国产日韩欧美在线看| 国产日韩一区| 国产亚洲欧洲997久久综合| 国产欧美一区二区精品秋霞影院| 国产精品国产福利国产秒拍| 国产精品久久久久影院亚瑟 | 久久国产精品久久久| 欧美一级视频| 久久国产精品久久w女人spa| 久久亚洲精品网站| 欧美成人一区二区在线| 亚洲精品看片| 午夜激情久久久| 久久精品夜色噜噜亚洲aⅴ|