▶ 브로드캐스트 수신자
Braadcasting 이란 메시지를 여러 객체에게 전달하는 방법을 의미 합니다. 결국, 이러한 이벤트는 단말 전체에 적용될 수 있는 것으로 '글로벌 이벤트(Global Event)'라 합니다. 이렇게 전달되는 브로드캐스팅 메시지는 브로드 캐스트 수신자(Broadcast Receiver)라는 애플리케이션 구성요소를 이용해 받을 수 잇습니다.
브로드캐스트 메시지는 크게 두 가지 종류가 있습니다. 하나는 '순차 브로드캐스트(Ordered Broadcast)' 이고 다른 하나는 '일반 브로드캐스트' 메시지 입니다. 두 브로드캐스트 메시지의 차이점은 일반 브로드캐스트 메시지의 경우 순서 없이 모든 브로드캐스트 수신자에게 메시지를 전달하지만 순차 브로드캐스트는 순서대로 전달하는 것입니다.
▶ SMSBroadcastReceiver.java
import java.util.Date;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
public class SMSBroadcastReceiver extends BroadcastReceiver {
public static final String TAG = "SMSBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i(TAG, "onReceive() called.");
// check if SMS global event is received
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Log.i(TAG, "SMS received.");
// abort this one
abortBroadcast();
// parse SMS message
Bundle bundle = intent.getExtras();
Object messages[] = (Object[])bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
int smsPieces = messages.length;
for(int n=0;n<smsPieces; n++)
{
smsMessage[n] = SmsMessage.createFromPdu((byte[])messages[n]);
}
Date curDate = new Date(smsMessage[0].getTimestampMillis());
Log.i(TAG, "SMS Timestamp : " + curDate.toString());
String origNumber = smsMessage[0].getOriginatingAddress();
String message = smsMessage[0].getMessageBody().toString();
Log.i(TAG, "SMS : " + origNumber + ", " + message);
// launch another activity
Intent myIntent = new Intent(context, SMSDisplayActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
myIntent.putExtra("number", origNumber);
myIntent.putExtra("message", message);
myIntent.putExtra("timestamp", curDate.toString());
context.startActivity(myIntent);
}
}
}
▶ SMSDisplayActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class SMSDisplayActivity extends Activity {
Button titleButton;
Button closeButton;
TextView messageTextView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.sms_display);
// refer instances
titleButton = (Button) findViewById(R.id.titleButton);
ScrollView scroll01 = (ScrollView) findViewById(R.id.scroll01);
closeButton = (Button) findViewById(R.id.closeButton);
messageTextView = (TextView) findViewById(R.id.messageTextView);
closeButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
int spec = View.MeasureSpec.UNSPECIFIED;
titleButton.measure(spec, spec);
int button01Height = titleButton.getMeasuredHeight();
closeButton.measure(spec, spec);
int button02Height = closeButton.getMeasuredHeight();
Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
//DisplayMetrics metrics = getResources().getDisplayMetrics();
//Log.d("SIZE TAG", "widthPixels : " + metrics.widthPixels + ", heightPixels : " + metrics.heightPixels);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.height = screenHeight - button01Height - button02Height - 20;
scroll01.setLayoutParams(params);
// process intent
Intent passedIntent = getIntent();
if (passedIntent != null) {
processIntent(passedIntent);
}
}
protected void onNewIntent(Intent intent) {
processIntent(intent);
super.onNewIntent(intent);
}
private void processIntent(Intent intent) {
String number = intent.getStringExtra("number");
String message = intent.getStringExtra("message");
String timestamp = intent.getStringExtra("timestamp");
if (number != null) {
titleButton.setText(number + " 에서 문자 수신");
messageTextView.setText("[" + timestamp + "] " + message);
messageTextView.invalidate();
}
}
}
▶ sms_display.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/titleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SMS TITLE"
/>
<ScrollView
android:id="@+id/scroll01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff222288"
>
<TextView
android:id="@+id/messageTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SMS CONTENTS"
/>
</ScrollView>
<Button
android:id="@+id/closeButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Close"
/>
</LinearLayout>
▶ AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.samplebroadcastreceiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!--
<activity
android:name=".SMSReceiverActivity"
android:label="@string/title_activity_smsreceiver" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
-->
<activity android:name=".SMSDisplayActivity"
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=".SMSBroadcastReceiver">
<intent-filter android:priority="10001">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
</manifest>
[Android]Toast - 01 (0) | 2012.08.15 |
---|---|
[Android] 리소스와 메니페스트 (0) | 2012.08.14 |
[Andrid] Service 예제 (1) | 2012.08.12 |
[Android] Activity 수명주기 (0) | 2012.08.12 |
[Android] Parcelable 인터페이스 (0) | 2012.08.11 |
댓글 영역