2.主程序代码:
public class EX06_01 extends Activity
{
private TextView mTextView1;
private TextView textView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*透过findViewById建构巳建立TextView对象*/
textView=(TextView)findViewById(R.id.myTextView2);
mTextView1 = (TextView) findViewById(R.id.myTextView1);
mTextView1.setText("等待接收短信...");
Bundle bundle=getIntent().getBundleExtra("SMSS");
//初始时并没有短信的内容(因为没有发送),若没有判断会造成空指针异常
if(bundle!=null){
String smsString=bundle.getString("SMS");
textView.setText(smsString);
}
}
}
3.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="irdc.EX06_01"
android:versionCode="1"
android:versionName="1.0.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".EX06_01"
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="EX06_01_SMSreceiver">
<!-- 設定要捕捉的訊息名稱為provider中Telephony.SMS_RECEIVED -->
<intent-filter>
<action
android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
</manifest>
更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11
