본문 바로가기
안드로이드+자바

문story) 안드로이드 BroadcastReceiver 사용하는 간단한 코드

by 문자메일 2017. 1. 7.
package app.app1;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

TextView t1;
Button button;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);



button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("firebase");
sendBroadcast(intent);
}
});

}


}


package app.app1;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
* Created by Byun YB on 2017-01-07.
*/

public class BroadCast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

Intent intent1 = new Intent(context, WorkActivity.class);
intent1.putExtra("firebase", "방송을 받았다");
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //이게 꼭 필요하다,, 새로운 액티비를 연다? 이런 문장인듯
context.startActivity(intent1);
}
}


package app.app1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

/**
* Created by Byun YB on 2017-01-07.
*/

public class WorkActivity extends Activity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);

Intent intent = getIntent();
textView.setText(intent.getStringExtra("firebase"));
}
}


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="app.app1">

<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>
<activity android:name=".WorkActivity"></activity>
<receiver android:name=".BroadCast">
<intent-filter>
<action android:name="firebase"></action>
</intent-filter>
</receiver>
</application>

</manifest>


<?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:id="@+id/activity_main"
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="app.app1.MainActivity">

<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="76dp"
android:id="@+id/textView" />

<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:id="@+id/button"
android:layout_below="@+id/textView"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView" />
</RelativeLayout>


댓글