Android 接收短信
启动程序时启动一个service,在service里注册接收短信的广播,当手机收到短信里,打印出短信内容跟电话号码。
package com.lmy.SmsListener; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class SmsListenerActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.main); TextView tv = new TextView(this); tv.setText("Hello. I started!"); setContentView(tv); Intent service = new Intent(this, MyService.class); this.startService(service); } }
当service被kill后,我们可以在开机时自动启动service。
开机自动启动一个service,在service里注册接收短信的广播,当手机收到短信里,打印出短信内容跟电话号码。
开机启动后系统会发出一个Standard Broadcast Action,名字叫android.intent.action.BOOT_COMPLETED,这个Action只会发出一次。
创建一个类继承BroadcastReceiver,在onReceive(Context context, Intent intent)里面启动service。
package com.lmy.SmsListener; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class MyBrocast extends BroadcastReceiver { static final String ACTION = "android.intent.action.BOOT_COMPLETED"; @Override public void onReceive(Context context, Intent intent) { Log.v("dimos", "MyBrocast"); if (intent.getAction().equals(ACTION)) { Intent service = new Intent(context, MyService.class); context.startService(service); } } }
在service中注册一个接收短信的广播:
package com.lmy.SmsListener; import android.app.Service; import android.content.Intent; import android.content.IntentFilter; import android.os.IBinder; import android.util.Log; public class MyService extends Service { @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); IntentFilter localIntentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED"); localIntentFilter.setPriority(2147483647); SmsRecevier localMessageReceiver = new SmsRecevier(); Log.v("dimos", "MyService"); registerReceiver(localMessageReceiver, localIntentFilter); } }
广播接收到短信:
package com.lmy.SmsListener; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class SmsRecevier extends BroadcastReceiver { public SmsRecevier() { super(); Log.v("dimos", "SmsRecevier create"); } @Override public void onReceive(Context context, Intent intent) { String dString = SmsHelper.getSmsBody(intent); String address = SmsHelper.getSmsAddress(intent); Log.i("dimos", dString+","+address); //阻止广播继续传递,如果该receiver比系统的级别高, //那么系统就不会收到短信通知了 abortBroadcast(); } }
获得短信内容跟短信地址:
package com.lmy.SmsListener; import android.content.Intent; import android.os.Bundle; import android.telephony.SmsMessage; public class SmsHelper { /** * 获得短信内容 * */ public static String getSmsBody(Intent intent) { String tempString = ""; Bundle bundle = intent.getExtras(); Object messages[] = (Object[]) bundle.get("pdus"); SmsMessage[] smsMessage = new SmsMessage[messages.length]; for (int n = 0; n < messages.length; n++) { smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]); // 短信有可能因为使用了回车而导致分为多条,所以要加起来接受 tempString += smsMessage[n].getDisplayMessageBody(); } return tempString; } /** * 获得短信地址 * */ public static String getSmsAddress(Intent intent) { Bundle bundle = intent.getExtras(); Object messages[] = (Object[]) bundle.get("pdus"); return SmsMessage.createFromPdu((byte[]) messages[0]) .getDisplayOriginatingAddress(); } }
在AndroidManifest.xml里声明并加权限:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.lmy.SmsListener" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="7" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".SmsListenerActivity" 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="MyBrocast" android:enabled="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> <service android:name="MyService"></service> </application> <uses-permission android:name="android.permission.RECEIVE_SMS" /><!-- 接收短信权限 --> <!-- 添加接收系统启动消息(用于开机启动)权限 --> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> </manifest>
这样就可以获得接收到的短信了,赶快试下吧!