1) activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.rock.mybcastsender.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="송신기"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="메세지보내기"
android:id="@+id/button1"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp" />
</RelativeLayout>
2) MainActivity.java
package com.example.rock.mybcastsender;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button sendButton = (Button) findViewById(R.id.button1);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.example.rock.BROADCAST_TEST"); // 수신기에서 받을 인텐트 필터 지정. 수신기의 Manifest에서 이 필터를 지정해준다.
intent.putExtra("testValue", "가나다");
sendBroadcast(intent);
Log.d("MYTEST", "보낸메시지:" + "가나다");
}
});
}
}
2 수신 : MyBcastReceiver
1) AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rock.mybcastreceiver">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.rock.BROADCAST_TEST" /> // 송신자에서 보낸 인텐를 받기 위한 필터 지정
</intent-filter>
</receiver>
</application>
</manifest>
또는 아래와 같이 인텐트필터를 지정하지 않고 MyReceiver 에서 프로그램상으로 필터링 할 수도 있다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rock.mybcastreceiver">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
</receiver>
</application>
</manifest>
2) activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.rock.mybcastreceiver.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="수신기"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
3) MainActivity.java
package com.example.rock.mybcastreceiver;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
4) MyReceiver.java
package com.example.rock.mybcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
//throw new UnsupportedOperationException("Not yet implemented");
String name = intent.getAction();
// AndroidManifest.xml 에서 인텐트필터를 지정하지 않은 경우 아래와 같이 if 문으로 필터링
// 인텐트필터를 지정했으면 if 문 생략
if (name.equals("com.example.rock.BROADCAST_TEST")) {
String msg = intent.getStringExtra("testValue");
Log.d("MYTEST", "받은메시지:" + msg);
}
}
}
Android Studio 에서 위의 Broadcast Receiver 추가방법
* 테스트
1) MyBcastReceiver 를 먼저 실행한 다음 MyBcastSender 를 실행한다.
아래 사진에서 왼쪽은 에물레이터 상에서 프로그램 두개를 모두 띄운 상태.
2) MyBcastSender에서 '메시지보내기' 버튼을 클릭하면 Android Studio 하단에 있는 logcat에서 메세지가 전달되는 것을 확인할 수 있다.
MyBcastReceiver 가 한번 실행되어 브로드캐스트를 받을 수 있는 상태가 되었으므로 이제 MyBcastReceiver 가 실행되지 않은 상태에서도 메시지를 받을 수 있다.
MyBcastReceiver 에서 메시지를 받았을때 notification 이나 메시지를 받았을때 실행할 activity를 구현하면 됨.
- 송신기(MyBcastSender) 쪽 로그
- 수신기(MyBcastReceiver) 쪽 로그
댓글 없음 :
댓글 쓰기