📄 smsreceiver.java
字号:
package com.android.smsReceiver;/* import android.app.Activity;import android.os.Bundle;public class SMSReceiver extends Activity { /** Called when the activity is first created. */ /* public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); }}*/import android.app.NotificationManager;import android.content.Context;import android.content.Intent;import android.content.IntentReceiver;import android.os.Bundle;import android.provider.Telephony;import android.telephony.gsm.SmsMessage;import android.util.Log;public class SMSReceiver extends IntentReceiver { /** TAG used for Debug-Logging */ private static final String LOG_TAG = "SMSReceiver"; /* A Random IDs used for the Notification */ public static final int NOTIFICATION_ID_RECEIVED = 0x1221; /** The Action fired by the Android-System when a SMS was received. * We are using the Default Package-Visibility */ static final String ACTION = "android.provider.Telephony.SMS_RECEIVED"; // @Override public void onReceiveIntent(Context context, Intent intent) { /* As we want to display a Notification, we the NotificationManager */ NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); if (intent.getAction().equals(ACTION)) { // if(message starts with SMStretcher recognize BYTE) StringBuilder sb = new StringBuilder(); /* The SMS-Messages are 'hiding' within the extras of the Intent. */ Bundle bundle = intent.getExtras(); if (bundle != null) { /* Get all messages contained in the Intent*/ SmsMessage[] messages = Telephony.Sms.Intents.getMessagesFromIntent(intent); /* Feed the StringBuilder with all Messages found. */ for (SmsMessage currentMessage : messages){ sb.append("Received compressed SMS\nFrom: "); /* Sender-Number */ sb.append(currentMessage.getDisplayOriginatingAddress()); sb.append("\n----Message----\n"); /* Actual Message-Content */ sb.append(currentMessage.getDisplayMessageBody()); } } /* Logger Debug-Output */ Log.i(LOG_TAG, "[SMSApp] onReceiveIntent: " + sb); /* Show the Notification containing the Message. */ nm.notifyWithText(NOTIFICATION_ID_RECEIVED, sb.toString(), NotificationManager.LENGTH_LONG, null); /* Consume this intent, that no other application will notice it. */ this.abortBroadcast(); /* Start the Main-Activity */ Intent i = new Intent(context, SMSActivity.class); i.setLaunchFlags(Intent.NEW_TASK_LAUNCH); context.startActivity(i); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -