笔者是在学校用电信宽带的,有好基友向我要电信wifi Chinanet的密码用用,自己偷空写了一个安卓程序,用来自动判断接受到的短信;
MainActivity.java
package com.yinghualuo.getnewsms;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity {
private Intent intent;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent(MainActivity.this,PassSMSService.class);
startService(intent);
}
}
BootReceiver.java
package com.yinghualuo.getnewsms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootReceiver extends BroadcastReceiver {
public void onReceive(Context arg0, Intent arg1) {
Intent startServiceIntent = new Intent(arg0,PassSMSService.class);
arg0.startService(startServiceIntent);
}
}
PassSMSService.java
package com.yinghualuo.getnewsms;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class PassSMSService extends Service {
public void onCreate() {
new SmsRecevier();
Log.i("Service", "onCreate");
super.onCreate();
}
public IBinder onBind(Intent intent) {
return null;
}
}
SmsReceiver.java
package com.yinghualuo.getnewsms;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
public class SmsRecevier extends BroadcastReceiver{
//朋友号码
private static final String mobile = "15555555555";
private String receivedMobile = null;
private String receivedContent = null;
private String sendContent = null;
public void onReceive(Context context, Intent intent) {
// 获取新短信广播内容
Bundle bundle=intent.getExtras();
if (bundle != null){
Object[] messages=(Object[])bundle.get("pdus");
SmsMessage[] smsMessages=new SmsMessage[messages.length];
for (int i = 0; i < smsMessages.length; i++) {
smsMessages[i]=SmsMessage.createFromPdu((byte[])messages[i]);
}
receivedMobile = smsMessages[0].getOriginatingAddress();
receivedContent = smsMessages[0].getMessageBody();
// 显示短信内容
Log.i("receivedMobile", receivedMobile);
Log.i("receivedContent length", String.valueOf(receivedContent.length()));
Log.i("receivedContent", receivedContent);
if (isChinanetSMS(context, receivedContent, receivedMobile)) {
//下面的转发给朋友
sendContent = receivedContent.substring(27,39) + " 转发自:屌爆的HTC T328d";
sendContent = sendContent.trim();
sendSMS(context, sendContent, mobile);
Log.i("sendmsmContent", sendContent);
Log.i("sendmsmMobile", mobile);
//提示发送短信
Toast.makeText(context, "sendmsmTo "+mobile, Toast.LENGTH_LONG).show();
}
}
}
// 判断是否为Chinanet发来的密码短信
private boolean isChinanetSMS(Context context, String content, String mobile){
boolean result = false;
if(mobile.equals("10001")){
if(content.length() > 39){
Log.i("SMS judge", content.substring(0, 15));
if (content.substring(0, 15).equals(context.getString(R.string.sms10001))){
result = true ;
}
}
}
return result;
}
// 发送短信
private void sendSMS(Context context,String content,String mobile){
SmsManager smsManager = SmsManager.getDefault();
PendingIntent sentIntent = PendingIntent.getBroadcast(context, 0, new Intent(), 0);
smsManager.sendTextMessage(mobile, null, content, sentIntent, null);
}
}
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">10001密码守护精灵</string>
<string name="tips">这个程序自动转发来自10001的无线网密码</string>
<string name="sms10001">尊敬的天翼用户,您的上网账号为</string>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yinghualuo.getnewsms"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" android:maxSdkVersion="18"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="SmsRecevier" android:enabled="true">
<intent-filter >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<receiver android:name="BootReceiver" android:enabled="true">
<intent-filter>
<!-- 系统启动完成后会调用-->
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity
android:name="com.yinghualuo.getnewsms.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>
<service android:name="com.yinghualuo.getnewsms.PassSMSService" android:enabled="true" ></service>
</application>
</manifest>
这个只是我小试身手写的,分享自己的功能实现,如果程序严谨性和规范性有带提高的地方,欢迎向我吐槽 QQ:496761858