먼저 AndroidManifest.xml 에 추가 해 줍니다.
// AndroidManifest.xml<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.msi.manning.chapter8″>
<uses-permission android:name=”android.permission.RECEIVE_SMS” />
<application android:icon=”@drawable/chat”>
<activity android:name=”.SMSNotifyActivity” 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 android:name=”.SMSNotifyExample”>
<intent-filter>
<action android:name=”android.provider.Telephony.SMS_RECEIVED” />
</intent-filter>
</receiver>
</application>
</manifest>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.msi.manning.chapter8″>
<uses-permission android:name=”android.permission.RECEIVE_SMS” />
<application android:icon=”@drawable/chat”>
<activity android:name=”.SMSNotifyActivity” 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 android:name=”.SMSNotifyExample”>
<intent-filter>
<action android:name=”android.provider.Telephony.SMS_RECEIVED” />
</intent-filter>
</receiver>
</application>
</manifest>
// SMSNotifyActivity.java
package com.msi.manning.chapter8;
import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
public class SMSNotifyActivity extends Activity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
// look up the notification manager service
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// cancel the notification that we started in SMSNotifyExample
nm.cancel(R.string.app_name);
}
}
// SMSNotifyExample.java
import android.app.Notification;
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.provider.Telephony.Sms;
import android.telephony.gsm.SmsMessage;
import android.util.Log;
public class SMSNotifyExample extends IntentReceiver {
/** TAG used for Debug-Logging */
private static final String LOG_TAG = "SMSReceiver";
// public static final int LENGTH_LONG = 10;
/* 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";
private CharSequence from = null;
private CharSequence tickerMessage = null;
public void onReceiveIntent(Context context, Intent intent) {
NotificationManager nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
if (intent.getAction().equals(ACTION)) {
// if(message starts with SMStretcher recognize BYTE)
StringBuilder sb = new StringBuilder();
/* Logger Debug-Output */
Log.i(LOG_TAG, "[SMSApp] onReceiveIntent: " + sb);
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());
// set
from = currentMessage.getDisplayOriginatingAddress();
sb.append("\n?-Message?-\n");
/* Actual Message-Content */
sb.append(currentMessage.getDisplayMessageBody());
}
}
this.abortBroadcast();
/* Start the Main-Activity */
Intent i = new Intent(context, SMSNotifyActivity.class);
i.setLaunchFlags(Intent.NEW_TASK_LAUNCH);
context.startActivity(i);
// CharSequence from =
// currentMessage.getDisplayOriginatingAddress();
CharSequence appName = "SMSNotifyExample";
tickerMessage = sb.toString();
Long theWhen = System.currentTimeMillis();
Intent appIntent = new Intent(Intent.VIEW_ACTION,
Sms.Inbox.CONTENT_URI);
Notification notif = new Notification(context, // our context
R.drawable.incoming, // the icon for the status bar
tickerMessage, // the text to display in the ticker
theWhen, // the timestamp for the notification
from, // the title for the notification
tickerMessage, // the details to display in the notification
i, // the contentIntent (see above)
R.drawable.chat, // the app icon
appName, // the name of the app
appIntent); // intent that shows the inbox when you click on
// icon
notif.vibrate = new long[] { 100, 250, 100, 500 };
nm.notify(R.string.alert_message, notif);
}
}
}
'IT > Android' 카테고리의 다른 글
안드로이드 버튼에 텍스트와 이미지 같이 출력하기 (0) | 2012.02.16 |
---|---|
안드로이드 SharedPreferences 변화 감지하기 (0) | 2012.02.16 |
안드로이드 TextView 글자 흐르기와 .... 표시 (0) | 2012.02.16 |
안드로이드 Keyguard 끄기 (0) | 2012.02.16 |
안드로이드 화면회전시 설정주기(애니메이션 주기) (0) | 2012.02.16 |