1)创建类文件SMSReceiver.java继承自BroadcastReceiver,聆听系统服务广播的信息,具体代码如下
package com.example.smsreceiverapp;
import java.sql.Date;
import java.text.SimpleDateFormat;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
/*自定义继承自BroadcastReceiver类,聆听系统服务广播的信息*/
public class SMSReceiver extends BroadcastReceiver {
/* 申明静态字符串,并使用android.provider.Telephony.SMS_RECEIVED
* 作为Action为短信的依据*/
private static final String SMS_STRING="android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context context,Intent intent){
/*判断是否为短信*/
if(intent.getAction().equals(SMS_STRING)){
/*返回主窗体*/
Intent mainIntent=new Intent(context,MainActivity.class);
/*获取当前时间并设置样式*/
SimpleDateFormat formatter=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date currentDate=new Date(System.currentTimeMillis());
String currentDateString=formatter.format(currentDate);
/*创建Bundle对象,并将要传递的数据传入*/
Bundle bundle=new Bundle();
bundle.putString("currentDate", currentDateString);
mainIntent.putExtras(bundle);
/*设置让以一个全新的task来运行*/
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mainIntent);
}
}
}
2)修改MainActivity.java,具体代码如下
package com.example.smsreceiverapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView1=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1=(TextView)findViewById(R.id.textView1);
/*获取Bundle对象*/
Bundle bundle=this.getIntent().getExtras();
if(bundle!=null){
/*获取Bundle对象中的数据*/
String currentDateString=bundle.getString("currentDate");
textView1.setText(currentDateString);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
3)修改AndroidManifest.xml
创建receiver聆听系统广播信息,具体代码如下
<!-- 创建receiver聆听系统广播信息 -->
<receiver android:name=".SMSReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
添加系统接收短信权限,具体代码如下
<!-- 添加系统接收短信权限 -->
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
修改后的AndroidManifest.xml,具体代码如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.smsreceiverapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.smsreceiverapp.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聆听系统广播信息 -->
<receiver android:name=".SMSReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
<!-- 添加系统接收短信权限 -->
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
</manifest>