问题描述
packagemars.ToastText;importandroid.app.Activity;importandroid.os.Bundle;importandroid.util.Log;importandroid.view.View;importandroid.widget.Button;importandroid.widget.TextView;importandroid.widget.Toast;publicclassToastTextActivityextendsActivity{/**Calledwhentheactivityisfirstcreated.*/privatestaticfinalStringTAG="SMSReceive";@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);Log.i(TAG,"INFO");TextViewtext=newTextView(this);Buttonbutton=newButton(this);text=(TextView)findViewById(R.id.text);button=(Button)findViewById(R.id.button);button.setOnClickListener(newButton.OnClickListener(){@OverridepublicvoidonClick(Viewv){//TODOAuto-generatedmethodstubDisplayToast("短信内容显示:");}});}publicvoidDisplayToast(Stringstr){Toast.makeText(this,str,Toast.LENGTH_LONG).show();}}
packagemars.ToastText;importandroid.content.BroadcastReceiver;importandroid.content.Context;importandroid.content.Intent;importandroid.os.Bundle;importandroid.telephony.SmsMessage;importandroid.util.Log;importandroid.view.Gravity;importandroid.widget.Toast;publicclassSMSReceiveextendsBroadcastReceiver{@OverridepublicvoidonReceive(Contextcontext,Intentintent){//TODOAuto-generatedmethodstubBundlebundle=intent.getExtras();Objectmessages[]=(Object[])bundle.get("pdus");SmsMessagesmsMessage[]=newSmsMessage[messages.length];for(intn=0;n<messages.length;n++){smsMessage[n]=SmsMessage.createFromPdu((byte[])messages[n]);}Toasttoast=Toast.makeText(context,"短信内容"+smsMessage[0].getMessageBody(),Toast.LENGTH_LONG);toast.setGravity(Gravity.TOP|Gravity.LEFT,0,200);toast.show();}}
<?xmlversion="1.0"encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="mars.ToastText"android:versionCode="1"android:versionName="1.0"><uses-sdkandroid:minSdkVersion="8"/><applicationandroid:icon="@drawable/icon"android:label="@string/app_name"><activityandroid:name=".ToastTextActivity"android:label="@string/app_name"><intent-filter><actionandroid:name="android.intent.action.MAIN"/><categoryandroid:name="android.intent.category.LAUNCHER"/></intent-filter></activity><receiverandroid:enabled="true"android:permission="android.permission.RECEIVE_SMS"android:name=".SMSReceive"><intent-filter><actionandroid:name="android.provider.Telephony.SMS_RECEIVED"></action></intent-filter></receiver></application></manifest>本人初学,请高手指教一下。。