OpenFrameWork
오픈프레임워크_Day79
px
2015. 7. 6. 14:58
### : 목차 구분 기호
--- : 목차 내에 항목 구분 기호
@@@ : 태그 용도
--- : 목차 내에 항목 구분 기호
@@@ : 태그 용도
,,, : 같은 목차 내에 구분 기호
목차
1. 이론 및 정보
2. 설정 및 그 밖에
3. 소스코드 또는 실습
4. 과제
###################################
1. 이론 및 정보
-----------------------------------
* Activity 실행시 반드시 무언가 실행 시키고 싶으면
LifeCycle에 따라 onResume()
-----------------------------------
앞으로 배울 내용은?
브로드케스트 리시버, Content Provider, xml파싱, db?
브로드케스트 리시버, Content Provider, xml파싱, db?
-----------------------------------
* Main View에 Thread가 접근을 못하게 막아 둠
동기화 문제가 발생하기 때문에
그래서 아래와 같은 해결방법이 있음
핸들러
루퍼
1. handler
2. post - 핸들러를 생략해서 만들 수 있는 거, runnable 상속
3. looper - Run 메서드 안에서만 쓰임, 자식 쓰레드에는 만들어져 있지 않음, Main 쓰레드에는 있음
4. AsyncTask - 복잡한 Thread 코드를 간단하게 구현할 수 있게 해줌, 어려움
-----------------------------------
* Broadcast Receiver
모두에게 전달해주는데 받고 싶은 사람이 받음
이러한 방송 메시지를 직접 만들어서 사용가능
ACTION_XXX 메시지 들이 있음
이런 메시지들이 안드로이드 시스템에서 준비해 놓은 메시지
이런거 말고 직접 만드는게 가능
-----------------------------------
* XML 파싱
주로 SAX를 씀
서버로 부터 필요한 데이터를 받아옴
DOM
SAX
SAX를 좀 더 쉽게
SAX
SAX를 좀 더 쉽게
버스노선 정보 등 LBS
공공기관 API - data.go.kr
-----------------------------------
###################################
2. 설정 및 그 밖에
-----------------------------------
2. 설정 및 그 밖에
-----------------------------------
* 프로젝트 생성 - BroadCastReceiverApp
android.permission.RECEIVE_SMS
-----------------------------------
* Receiver 생성
* Permission 추가
android.permission.RECEIVE_SMS
-----------------------------------
* 에뮬레이터에서 메시지 전송
* 에뮬레이터에서 메시지 전송
-----------------------------------
###################################
3. 소스코드 또는 실습
-----------------------------------
3-1
WorkSpace : ~\study\AndroidWork
/ThreadApp/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.threadapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="FirstActivity"></activity>
<activity android:name="SecondActivity"></activity>
</application>
</manifest>
4. 과제
-----------------------------------
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.threadapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="FirstActivity"></activity>
<activity android:name="SecondActivity"></activity>
</application>
</manifest>
-----------------------------------
3-2
WorkSpace : ~\study\AndroidWork
/ThreadApp/res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onNext"
android:text="First Example" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onSecond"
android:text="Second Example" />
</LinearLayout>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onNext"
android:text="First Example" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onSecond"
android:text="Second Example" />
</LinearLayout>
-----------------------------------
3-3
WorkSpace : ~\study\AndroidWork
/ThreadApp/res/layout/activity_second.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvMainValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MainValue : 0" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onIncrement"
android:text="Increment" />
<EditText
android:id="@+id/etNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:text="5" />
<TextView
android:id="@+id/tvBackValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BackValue : 0" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onSquare"
android:text="Square" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onRoot"
android:text="Root" />
</LinearLayout>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvMainValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MainValue : 0" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onIncrement"
android:text="Increment" />
<EditText
android:id="@+id/etNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:text="5" />
<TextView
android:id="@+id/tvBackValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BackValue : 0" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onSquare"
android:text="Square" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onRoot"
android:text="Root" />
</LinearLayout>
-----------------------------------
3-4
WorkSpace : ~\study\AndroidWork
/ThreadApp/res/layout/activity_first.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onViewCounter"
android:text="View Counter" />
<TextView
android:id="@+id/tvViewCounter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="30dp" />
</LinearLayout>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onViewCounter"
android:text="View Counter" />
<TextView
android:id="@+id/tvViewCounter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="30dp" />
</LinearLayout>
-----------------------------------
3-5
WorkSpace : ~\study\AndroidWork
/ThreadApp/src/com/example/threadapp/MainActivity.java
package com.example.threadapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onNext(View v) {
startActivity(new Intent(getApplicationContext(), FirstActivity.class));
}
public void onSecond(View v){
startActivity(new Intent(getApplicationContext(), SecondActivity.class));
}
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onNext(View v) {
startActivity(new Intent(getApplicationContext(), FirstActivity.class));
}
public void onSecond(View v){
startActivity(new Intent(getApplicationContext(), SecondActivity.class));
}
}
-----------------------------------
3-6
WorkSpace : ~\study\AndroidWork
/ThreadApp/src/com/example/threadapp/FirstActivity.java
package com.example.threadapp;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
public class FirstActivity extends Activity {
private int value = 0;
private boolean isRunning = false;
private TextView tvViewCounter;
Handler handler = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
tvViewCounter = (TextView) findViewById(R.id.tvViewCounter);
handler = new counterHandler();
}
// 택배회사인 핸들러를 만듬
// 무명 클래스로 바꿀 수 있음
class counterHandler extends Handler {
@Override
public void handleMessage(Message msg) {
// 매개변수 Message msg가 메시지 큐임
// 꺼내온 값이 msg임
// super.handleMessage(msg);
if (msg.what == 0) {
tvViewCounter.setText("Current Counter : " + value);
}
}
}
@Override
protected void onResume() {
super.onResume();
CounterThread ct = new CounterThread();
isRunning = true;
ct.start();
}
public void onViewCounter(View v) {
}
@Override
protected void onPause() {
super.onPause();
isRunning = false;
value = 0;
}
@Override
protected void onStop() {
super.onStop();
}
class CounterThread extends Thread {
@Override
public void run() {
while (isRunning) {
try {
sleep(1000);
value++;
// 여기에 코딩하면 에러가 남
// 이런 문법이 지원이 안됨, 시스템 문제임
// 이유는? 서로 다른 클래스가 View에는 접근할 수 없음
// tvViewCounter.setText("Current Counter : " + value);
// 메시지 큐에서 빈 상자를 하나 받아옴
// Message msg = Message.obtain();
// 값을 증가했다는 소식만 알려주면되, 값은 FirstActivity에 인스턴스 변수라 안넘겨도됨
// 이 메시지가 어떤 메시지인지 구별하기 위한 변수
// msg.what = 0;
// 전달할 값이 있다면, 최대 2개의 값을 전달 가능
// msg.arg1 = 1;
// msg.arg2 = 3;
// 메시지 큐에 다시 돌려보내기
// 핸들러에게 물건을 가져가라고 알려줌
// handler.sendMessage(msg);
// 간단하게 보낼때, Empty의 내용을 보냄
handler.sendEmptyMessage(0);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
public class FirstActivity extends Activity {
private int value = 0;
private boolean isRunning = false;
private TextView tvViewCounter;
Handler handler = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
tvViewCounter = (TextView) findViewById(R.id.tvViewCounter);
handler = new counterHandler();
}
// 택배회사인 핸들러를 만듬
// 무명 클래스로 바꿀 수 있음
class counterHandler extends Handler {
@Override
public void handleMessage(Message msg) {
// 매개변수 Message msg가 메시지 큐임
// 꺼내온 값이 msg임
// super.handleMessage(msg);
if (msg.what == 0) {
tvViewCounter.setText("Current Counter : " + value);
}
}
}
@Override
protected void onResume() {
super.onResume();
CounterThread ct = new CounterThread();
isRunning = true;
ct.start();
}
public void onViewCounter(View v) {
}
@Override
protected void onPause() {
super.onPause();
isRunning = false;
value = 0;
}
@Override
protected void onStop() {
super.onStop();
}
class CounterThread extends Thread {
@Override
public void run() {
while (isRunning) {
try {
sleep(1000);
value++;
// 여기에 코딩하면 에러가 남
// 이런 문법이 지원이 안됨, 시스템 문제임
// 이유는? 서로 다른 클래스가 View에는 접근할 수 없음
// tvViewCounter.setText("Current Counter : " + value);
// 메시지 큐에서 빈 상자를 하나 받아옴
// Message msg = Message.obtain();
// 값을 증가했다는 소식만 알려주면되, 값은 FirstActivity에 인스턴스 변수라 안넘겨도됨
// 이 메시지가 어떤 메시지인지 구별하기 위한 변수
// msg.what = 0;
// 전달할 값이 있다면, 최대 2개의 값을 전달 가능
// msg.arg1 = 1;
// msg.arg2 = 3;
// 메시지 큐에 다시 돌려보내기
// 핸들러에게 물건을 가져가라고 알려줌
// handler.sendMessage(msg);
// 간단하게 보낼때, Empty의 내용을 보냄
handler.sendEmptyMessage(0);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
-----------------------------------
3-7
WorkSpace : ~\study\AndroidWork
/ThreadApp/src/com/example/threadapp/SecondActivity.java
package com.example.threadapp;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class SecondActivity extends Activity {
private int m_MainValue;
private TextView tvMain, tvBack;
private EditText etNum;
private SquareRootThread thread;
public Handler m_mainHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// super.handleMessage(msg);
if (msg.what == 0) {
tvBack.setText("Square Result : " + msg.arg1);
} else if (msg.what == 1) {
// tvBack.setText("Root Result : "+msg.obj.toString());
tvBack.setText("Root Result : " + ((Double)msg.obj).doubleValue());
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
tvMain = (TextView) findViewById(R.id.tvMainValue);
tvBack = (TextView) findViewById(R.id.tvBackValue);
etNum = (EditText) findViewById(R.id.etNumber);
thread = new SquareRootThread(m_mainHandler);
thread.start();
}
public void onIncrement(View v) {
m_MainValue++;
tvMain.setText("MainValue : " + m_MainValue);
}
public void onSquare(View v) {
Message msg = Message.obtain();
msg.what = 0;
msg.arg1 = Integer.parseInt(etNum.getText() + "");
thread.m_backHandler.sendMessage(msg);
}
public void onRoot(View v) {
Message msg = Message.obtain();
msg.what = 1;
msg.arg1 = Integer.parseInt(etNum.getText().toString());
thread.m_backHandler.sendMessage(msg);
}
}
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class SecondActivity extends Activity {
private int m_MainValue;
private TextView tvMain, tvBack;
private EditText etNum;
private SquareRootThread thread;
public Handler m_mainHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// super.handleMessage(msg);
if (msg.what == 0) {
tvBack.setText("Square Result : " + msg.arg1);
} else if (msg.what == 1) {
// tvBack.setText("Root Result : "+msg.obj.toString());
tvBack.setText("Root Result : " + ((Double)msg.obj).doubleValue());
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
tvMain = (TextView) findViewById(R.id.tvMainValue);
tvBack = (TextView) findViewById(R.id.tvBackValue);
etNum = (EditText) findViewById(R.id.etNumber);
thread = new SquareRootThread(m_mainHandler);
thread.start();
}
public void onIncrement(View v) {
m_MainValue++;
tvMain.setText("MainValue : " + m_MainValue);
}
public void onSquare(View v) {
Message msg = Message.obtain();
msg.what = 0;
msg.arg1 = Integer.parseInt(etNum.getText() + "");
thread.m_backHandler.sendMessage(msg);
}
public void onRoot(View v) {
Message msg = Message.obtain();
msg.what = 1;
msg.arg1 = Integer.parseInt(etNum.getText().toString());
thread.m_backHandler.sendMessage(msg);
}
}
-----------------------------------
3-8
WorkSpace : ~\study\AndroidWork
/ThreadApp/src/com/example/threadapp/SquareRootThread.java
package com.example.threadapp;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
public class SquareRootThread extends Thread {
private Handler m_mainHandler;
public Handler m_backHandler;
public SquareRootThread(Handler hMain) {
this.m_mainHandler = hMain;
}
@Override
public void run() {
// 루퍼 준비
Looper.prepare();
m_backHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Message sendMsg = Message.obtain();
if (msg.what == 0) {
int result = msg.arg1 * msg.arg1;
sendMsg.what = 0;
sendMsg.arg1 = result;
} else if (msg.what == 1) {
double result = Math.sqrt((double) msg.arg1);
sendMsg.what = 1;
sendMsg.obj = new Double(result);
}
m_mainHandler.sendMessage(sendMsg);
}
};
// 반복되는 코드 뒤에
Looper.loop();
}
// Thread가 필요 없는 상황에서는 루퍼가 필요 없는 듯
// public Handler m_backHandler = new Handler() {
// @Override
// public void handleMessage(Message msg) {
// Message sendMsg = Message.obtain();
// // super.handleMessage(msg);
// if (msg.what == 0) {
// int result = msg.arg1 * msg.arg1;
// sendMsg.what = 0;
// sendMsg.arg1 = result;
//
// } else if (msg.what == 1) {
// double result = Math.sqrt((double) msg.arg1);
// sendMsg.what = 1;
// sendMsg.obj = new Double(result);
// }
// m_mainHandler.sendMessage(sendMsg);
// }
// };
}
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
public class SquareRootThread extends Thread {
private Handler m_mainHandler;
public Handler m_backHandler;
public SquareRootThread(Handler hMain) {
this.m_mainHandler = hMain;
}
@Override
public void run() {
// 루퍼 준비
Looper.prepare();
m_backHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Message sendMsg = Message.obtain();
if (msg.what == 0) {
int result = msg.arg1 * msg.arg1;
sendMsg.what = 0;
sendMsg.arg1 = result;
} else if (msg.what == 1) {
double result = Math.sqrt((double) msg.arg1);
sendMsg.what = 1;
sendMsg.obj = new Double(result);
}
m_mainHandler.sendMessage(sendMsg);
}
};
// 반복되는 코드 뒤에
Looper.loop();
}
// Thread가 필요 없는 상황에서는 루퍼가 필요 없는 듯
// public Handler m_backHandler = new Handler() {
// @Override
// public void handleMessage(Message msg) {
// Message sendMsg = Message.obtain();
// // super.handleMessage(msg);
// if (msg.what == 0) {
// int result = msg.arg1 * msg.arg1;
// sendMsg.what = 0;
// sendMsg.arg1 = result;
//
// } else if (msg.what == 1) {
// double result = Math.sqrt((double) msg.arg1);
// sendMsg.what = 1;
// sendMsg.obj = new Double(result);
// }
// m_mainHandler.sendMessage(sendMsg);
// }
// };
}
-----------------------------------
3-9
WorkSpace : ~\study\AndroidWork
/BroadCastReceiverApp/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastreceiverapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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="TestReceiver" >
<intent-filter>
<action android:name="com.study.GOGOGO" />
</intent-filter>
</receiver>
<receiver android:name="SmsReceiver" >
<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.example.broadcastreceiverapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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="TestReceiver" >
<intent-filter>
<action android:name="com.study.GOGOGO" />
</intent-filter>
</receiver>
<receiver android:name="SmsReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
-----------------------------------
3-10
WorkSpace : ~\study\AndroidWork
/BroadCastReceiverApp/res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Broadcast send from App"
android:onClick="onSend" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SMS Arraived..." />
</LinearLayout>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Broadcast send from App"
android:onClick="onSend" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SMS Arraived..." />
</LinearLayout>
-----------------------------------
3-11
WorkSpace : ~\study\AndroidWork
/BroadCastReceiverApp/src/com/example/broadcastreceiverapp/MainActivity.java
package com.example.broadcastreceiverapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SmsReceiver rev = new SmsReceiver();
}
public void onSend(View v){
Intent intent = new Intent("com.study.GOGOGO");
sendBroadcast(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SmsReceiver rev = new SmsReceiver();
}
public void onSend(View v){
Intent intent = new Intent("com.study.GOGOGO");
sendBroadcast(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
-----------------------------------
3-12
WorkSpace : ~\study\AndroidWork
/BroadCastReceiverApp/src/com/example/broadcastreceiverapp/TestReceiver.java
package com.example.broadcastreceiverapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class TestReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String name = intent.getAction();
if (name.equals("com.study.GOGOGO")) {
Toast.makeText(context.getApplicationContext(), "receive success",
Toast.LENGTH_SHORT).show();
}
}
}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class TestReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String name = intent.getAction();
if (name.equals("com.study.GOGOGO")) {
Toast.makeText(context.getApplicationContext(), "receive success",
Toast.LENGTH_SHORT).show();
}
}
}
-----------------------------------
3-13
WorkSpace : ~\study\AndroidWork
/BroadCastReceiverApp/src/com/example/broadcastreceiverapp/SmsReceiver.java
package com.example.broadcastreceiverapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.sax.StartElementListener;
import android.util.Log;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// Log.i("SmsReceiver", "sms receive success");
// 만약 다른 앱에게 방송이 가지 않게 하려면
abortBroadcast();
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Intent myIntent = new Intent(context, MainActivity.class);
//
// 만약 어플이 종료되어있다면 새로 생성 하겠다
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.sax.StartElementListener;
import android.util.Log;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// Log.i("SmsReceiver", "sms receive success");
// 만약 다른 앱에게 방송이 가지 않게 하려면
abortBroadcast();
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Intent myIntent = new Intent(context, MainActivity.class);
//
// 만약 어플이 종료되어있다면 새로 생성 하겠다
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
}
-----------------------------------
###################################4. 과제
-----------------------------------
-----------------------------------
###################################
5. 과제 해결
-----------------------------------
5. 과제 해결
-----------------------------------
-----------------------------------
###################################
6. 기타
----------------------------------- -----------------------------------
###################################