編輯:關於Android編程
要點
1.在Manifest.xml裡加"接收"SMS的權限
<uses-permission Android:name="android.permission.RECEIVE_SMS"></uses-permission>
2.在Manifest.xml裡注冊一個receive
<!-- 注冊Receiver,並且設置優先級 -->
<receiver android:name=".AutoSMS" android:exported="false">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
3.定義一個短信接收類,並且重寫onReceive
//繼承BroadcastReceiver
public class AutoSMS extends BroadcastReceiver
{
private String TAG="AutSMS";
//廣播消息類型
public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";
//覆蓋onReceive方法
@Override
public void onReceive(Context context, Intent intent)
{
.....
實例
下面是完整的代碼:
Manifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxh.autosms"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 注冊Receiver,並且設置優先級 -->
<receiver android:name=".AutoSMS" android:exported="false">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
</manifest>
AutoSMS.java:
package com.xxh.autosms;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
//繼承BroadcastReceiver
public class AutoSMS extends BroadcastReceiver
{
private String TAG="AutSMS";
//廣播消息類型
public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";
//覆蓋onReceive方法
@Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
Log.i(TAG, "引發接收事件");
//StringBuilder body=new StringBuilder("");//短信內容
//StringBuilder sender=new StringBuilder("");//發件人
//先判斷廣播消息
String action = intent.getAction();
if (SMS_RECEIVED_ACTION.equals(action))
{
//獲取intent參數
Bundle bundle=intent.getExtras();
//判斷bundle內容
if (bundle!=null)
{
//取pdus內容,轉換為Object[]
Object[] pdus=(Object[])bundle.get("pdus");
//解析短信
SmsMessage[] messages = new SmsMessage[pdus.length];
for(int i=0;i<messages.length;i++)
{
byte[] pdu=(byte[])pdus[i];
messages[i]=SmsMessage.createFromPdu(pdu);
}
//解析完內容後分析具體參數
for(SmsMessage msg:messages)
{
//獲取短信內容
String content=msg.getMessageBody();
String sender=msg.getOriginatingAddress();
Date date = new Date(msg.getTimestampMillis());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sendTime = sdf.format(date);
//TODO:根據條件判斷,然後進一般處理
if ("10060".equals(sender))
{
// 屏蔽手機號為10060的短信,這裡還可以時行一些處理,如把這個信息發送到第三人的手機等等。
//TODO:測試
Toast.makeText(context, "收到10060的短信"+"內容:"+content, Toast.LENGTH_LONG).show();
//對於特定的內容,取消廣播
abortBroadcast();
}
else
{
Toast.makeText(context, "收到:"+sender+"內容:"+content+"時間:"+sendTime.toString(), Toast.LENGTH_LONG).show();
}
}
}
}//if 判斷廣播消息結束
}
}
Android實現手勢滑動多點觸摸縮放平移圖片效果
現在app中,圖片預覽功能肯定是少不了的,用戶基本已經形成條件反射,看到小圖,點擊看大圖,看到大圖兩個手指開始進行放大,放大後,開始移動到指定部位。一、概述想要做到圖片支
android電池管理系統
1、概述 隨著移動智能設備的快速發屏,電池的續航能力在很大情況下誘導了大眾消費者的購買選擇,android系統對電源管理的合理與否直接影響到電池的續航能力,
Android文字轉語音
雖然視覺上的反饋通常是給用戶提供信息最快的方式,但這要求用戶把注意力設備上。當用戶不能查看設備時,則需要一些其他通信的方法。Android提供了強大的文字轉語音Text-
Android Scroller完全解析
在Android中,任何一個控件都是可以滾動的,因為在View類當中有scrollTo()和scrollBy()這兩個方法,如下圖所示: 這兩個方法的主要作用是將View