最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android接收和發(fā)送短信處理

 更新時間:2016年01月15日 10:37:10   作者:xu佳佳  
這篇文章主要介紹了Android接收和發(fā)送短信處理的相關(guān)資料,具有一定的參考價值,需要的朋友可以參考下

關(guān)于短信接收處理方面,當(dāng)前已經(jīng)有一些app做的比較好了,比如發(fā)給手機發(fā)驗證碼驗證的問題,很多app在手機接收到驗證碼后,不需要輸入,就直接可以跳過驗證界面,這就是用到了對接收到的短信的處理。至于短信的發(fā)送,也沒什么好說的了。在此也只是附上一個小實例。

效果圖:

MainActivity:

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.telephony.SmsMessage; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 
 
public class MainActivity extends Activity { 
 
  private TextView sender; 
  private TextView content; 
  private IntentFilter receiveFilter; 
  private MessageReceiver messageReceiver; 
 
 
  private EditText to; 
  private EditText msgInput; 
  private Button send; 
  private IntentFilter sendFilter; 
  private SendStatusReceiver sendStatusReceiver; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    sender = (TextView) findViewById(R.id.sender); 
    content = (TextView) findViewById(R.id.content); 
    to = (EditText) findViewById(R.id.to); 
    msgInput = (EditText) findViewById(R.id.msg_input); 
    send = (Button) findViewById(R.id.send); 
 
    //為接收短信設(shè)置要監(jiān)聽的廣播 
    receiveFilter = new IntentFilter(); 
    receiveFilter.addAction("android.provider.Telephony.SMS_RECEIVED"); 
    messageReceiver = new MessageReceiver(); 
    registerReceiver(messageReceiver, receiveFilter); 
 
    //為發(fā)送短信設(shè)置要監(jiān)聽的廣播 
    sendFilter = new IntentFilter(); 
    sendFilter.addAction("SENT_SMS_ACTION"); 
    sendStatusReceiver = new SendStatusReceiver(); 
    registerReceiver(sendStatusReceiver, sendFilter); 
 
    send.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        //發(fā)送短信 
        //并使用sendTextMessage的第四個參數(shù)對短信的發(fā)送狀態(tài)進行監(jiān)控 
        SmsManager smsManager = SmsManager.getDefault(); 
        Intent sentIntent = new Intent("SENT_SMS_ACTION"); 
        PendingIntent pi = PendingIntent.getBroadcast( 
            MainActivity.this, 0, sentIntent, 0); 
        smsManager.sendTextMessage(to.getText().toString(), null, 
            msgInput.getText().toString(), pi, null); 
      } 
    }); 
  } 
 
  @Override 
  protected void onDestroy() { 
    super.onDestroy(); 
    //在Activity摧毀的時候停止監(jiān)聽 
    unregisterReceiver(messageReceiver); 
    unregisterReceiver(sendStatusReceiver); 
  } 
 
  class MessageReceiver extends BroadcastReceiver { 
 
    @Override 
    public void onReceive(Context context, Intent intent) { 
      Bundle bundle = intent.getExtras(); 
      //使用pdu秘鑰來提取一個pdus數(shù)組 
      Object[] pdus = (Object[]) bundle.get("pdus"); 
 
      SmsMessage[] messages = new SmsMessage[pdus.length]; 
      for (int i = 0; i < messages.length; i++) { 
        messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); 
      } 
 
      //獲取發(fā)送方號碼 
      String address = messages[0].getOriginatingAddress(); 
 
      //獲取短信內(nèi)容 
      String fullMessage = ""; 
      for (SmsMessage message : messages) { 
        fullMessage += message.getMessageBody(); 
      } 
      sender.setText(address); 
      content.setText(fullMessage); 
    } 
 
  } 
 
  class SendStatusReceiver extends BroadcastReceiver { 
 
    @Override 
    public void onReceive(Context context, Intent intent) { 
      if (getResultCode() == RESULT_OK) { 
        //發(fā)送成功 
        Toast.makeText(context, "Send succeeded", Toast.LENGTH_LONG) 
            .show(); 
      } else { 
        //發(fā)送失敗 
        Toast.makeText(context, "Send failed", Toast.LENGTH_LONG) 
            .show(); 
      } 
    } 
 
  } 
 
} 

activity_main:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:orientation="vertical" > 
 
  <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="50dp" > 
 
    <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:padding="10dp" 
      android:text="From:" /> 
 
    <TextView 
      android:id="@+id/sender" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" /> 
  </LinearLayout> 
 
  <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="50dp" > 
 
    <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:padding="10dp" 
      android:text="Content:" /> 
 
    <TextView 
      android:id="@+id/content" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" /> 
  </LinearLayout> 
 
  <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="50dp" > 
 
    <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:padding="10dp" 
      android:text="To:" /> 
 
    <EditText 
      android:id="@+id/to" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_weight="1" /> 
  </LinearLayout> 
 
  <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="50dp" > 
 
    <EditText 
      android:id="@+id/msg_input" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_weight="1" /> 
 
    <Button 
      android:id="@+id/send" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:text="Send" /> 
  </LinearLayout> 
 
</LinearLayout> 

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  package="com.example.smstest" 
  android:versionCode="1" 
  android:versionName="1.0" > 
 
  <uses-sdk 
    android:minSdkVersion="14" 
    android:targetSdkVersion="17" /> 
 
  //接受短信 
  <uses-permission android:name="android.permission.RECEIVE_SMS" /> 
  //發(fā)送短信 
  <uses-permission android:name="android.permission.SEND_SMS" /> 
 
  <application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
      android:name="com.example.smstest.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> 
  </application> 
 
</manifest> 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。

相關(guān)文章

  • Android實現(xiàn)京東秒殺界面

    Android實現(xiàn)京東秒殺界面

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)京東秒殺界面,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Android 中基于TabLayout+ViewPager實現(xiàn)標(biāo)簽卡效果

    Android 中基于TabLayout+ViewPager實現(xiàn)標(biāo)簽卡效果

    這篇文章主要介紹了Android 中基于TabLayout+ViewPager實現(xiàn)標(biāo)簽卡效果,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-12-12
  • 使用Jetpack Compose實現(xiàn)翻轉(zhuǎn)卡片效果流程詳解

    使用Jetpack Compose實現(xiàn)翻轉(zhuǎn)卡片效果流程詳解

    Jetpack Compose 是一款基于 Kotlin 的聲明式 UI 工具包,可以方便地創(chuàng)建漂亮的用戶界面。使用 Compose 的動畫 API 和可繪制 API,可以輕松實現(xiàn)翻轉(zhuǎn)卡片效果。通過設(shè)置旋轉(zhuǎn)角度和透明度等屬性,可以使卡片沿著 Y 軸翻轉(zhuǎn),并實現(xiàn)翻頁效果
    2023-05-05
  • Android利用ViewDragHelper輕松實現(xiàn)拼圖游戲的示例

    Android利用ViewDragHelper輕松實現(xiàn)拼圖游戲的示例

    本篇文章主要介紹了Android利用ViewDragHelper輕松實現(xiàn)拼圖游戲的示例,非常具有實用價值,需要的朋友可以參考下
    2017-11-11
  • Android?Studio使用自定義對話框效果

    Android?Studio使用自定義對話框效果

    這篇文章主要為大家詳細(xì)介紹了Android?Studio使用自定義對話框效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Kotlin對象的懶加載方式by?lazy?與?lateinit?異同詳解

    Kotlin對象的懶加載方式by?lazy?與?lateinit?異同詳解

    這篇文章主要為大家介紹了Kotlin對象的懶加載方式by?lazy?與?lateinit?異同詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • android第三方分享方式的簡單實現(xiàn)

    android第三方分享方式的簡單實現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了android第三方分享方式的簡單實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • Android自定義控件ListView下拉刷新的代碼

    Android自定義控件ListView下拉刷新的代碼

    今天小編就為大家分享一篇關(guān)于Android自定義控件ListView下拉刷新的代碼,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • Android實現(xiàn)圖片文字輪播特效

    Android實現(xiàn)圖片文字輪播特效

    這篇文章主要介紹了Android圖片文字輪播效果,分別實現(xiàn)圖片和文字自動滾動,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-01-01
  • Android中實現(xiàn)WebView和JavaScript的互相調(diào)用詳解

    Android中實現(xiàn)WebView和JavaScript的互相調(diào)用詳解

    這篇文章主要給大家介紹了關(guān)于Android中實現(xiàn)WebView和JavaScript的互相調(diào)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友下面來一起看看吧。
    2018-03-03

最新評論

新郑市| 阿拉善左旗| 灌云县| 巫山县| 丰顺县| 乌鲁木齐市| 环江| 株洲市| 辉南县| 汝城县| 邢台县| 淄博市| 吉木萨尔县| 九江市| 界首市| 桃江县| 都兰县| 五台县| 河间市| 收藏| 额济纳旗| 平安县| 二连浩特市| 永州市| 肇东市| 朝阳区| 大英县| 三明市| 林芝县| 万州区| 吉首市| 海兴县| 永宁县| 汝城县| 墨玉县| 东莞市| 民勤县| 苗栗市| 鄂尔多斯市| 秀山| 乌审旗|