随笔 - 23  文章 - 0 评论 - 14 trackbacks - 0

废话少说,直奔主题~!

结构如图:

 

Sms_Message_AopActivity.java代码:

public class Sms_Message_AopActivity extends Activity {

	private Button submit;

	private static final String RECEIVE_SMS_ACTION = "android.provider.Telephony.SMS_RECEIVED";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		findAll();
		bind();
	}

	public void findAll() {
		submit = (Button) this.findViewById(R.id.submit);
	}

	public void bind() {
		submit.setOnClickListener(mylistener);
		IntentFilter inf = new IntentFilter(RECEIVE_SMS_ACTION);
		registerReceiver(mybroad, inf);

	}

	private View.OnClickListener mylistener = new OnClickListener() {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			System.exit(0);
		}
	};

	private BroadcastReceiver mybroad= new BroadcastReceiver(){

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			Bundle bundle = intent.getExtras();
			Object messages[] = (Object[]) bundle.get("pdus");
			SmsMessage smsMessage[] = new SmsMessage[messages.length];
			for (int i = 0; i < messages.length; i++) {
				smsMessage[i] = SmsMessage.createFromPdu((byte[]) messages[i]);
				String sender = smsMessage[i].getOriginatingAddress();
				String content = smsMessage[i].getMessageBody();
				Date date = new Date(smsMessage[i].getTimestampMillis());
				SimpleDateFormat format = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				String sendtime = format.format(date);
				/*可以自动发送,不用人工控制
				SmsManager sms = SmsManager.getDefault();
					Intent it=new Intent();
					it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				PendingIntent sentIntent = PendingIntent.getBroadcast(context, 0, it, 0);
	            sms.sendTextMessage("5556", null, sender+":"+content+":"+sendtime, sentIntent, null);
	           */
				//这里不会自动发送,只会跳到发送页面,人工发送		
				Uri smsToUri = Uri.parse("smsto:5556"); 
				Intent sendIntent = new Intent(Intent.ACTION_VIEW, smsToUri);
				sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				sendIntent.putExtra("sms_body", sender+":"+content+":"+sendtime); 
				sendIntent.setType("vnd.android-dir/mms-sms"); 
				startActivity(sendIntent); 
			
			}
		}

	};

}

 

AndroidManifest.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="Sms_Message_Aop.Jason"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="4" />
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Sms_Message_AopActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>
posted on 2011-09-16 17:32 Jwc 阅读(...) 评论(...) 编辑 收藏